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))
108 * This code is not prepared to handle more than 2 slots.
110 CTASSERT(TDMA_MAXSLOTS == 2);
112 static void tdma_vdetach(struct ieee80211vap *vap);
113 static int tdma_newstate(struct ieee80211vap *, enum ieee80211_state, int);
114 static void tdma_beacon_miss(struct ieee80211vap *vap);
115 static void tdma_recv_mgmt(struct ieee80211_node *, struct mbuf *,
116 int subtype, int rssi, int nf);
117 static int tdma_update(struct ieee80211vap *vap,
118 const struct ieee80211_tdma_param *tdma, struct ieee80211_node *ni,
120 static int tdma_process_params(struct ieee80211_node *ni,
121 const u_int8_t *ie, int rssi, int nf, const struct ieee80211_frame *wh);
124 settxparms(struct ieee80211vap *vap, enum ieee80211_phymode mode, int rate)
126 vap->iv_txparms[mode].ucastrate = rate;
127 vap->iv_txparms[mode].mcastrate = rate;
131 setackpolicy(struct ieee80211com *ic, int noack)
133 struct ieee80211_wme_state *wme = &ic->ic_wme;
136 for (ac = 0; ac < WME_NUM_AC; ac++) {
137 wme->wme_chanParams.cap_wmeParams[ac].wmep_noackPolicy = noack;
138 wme->wme_wmeChanParams.cap_wmeParams[ac].wmep_noackPolicy = noack;
143 ieee80211_tdma_vattach(struct ieee80211vap *vap)
145 struct ieee80211_tdma_state *ts;
147 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
148 ("not a tdma vap, caps 0x%x", vap->iv_caps));
150 ts = (struct ieee80211_tdma_state *) malloc(
151 sizeof(struct ieee80211_tdma_state), M_80211_VAP, M_NOWAIT | M_ZERO);
153 printf("%s: cannot allocate TDMA state block\n", __func__);
154 /* NB: fall back to adhdemo mode */
155 vap->iv_caps &= ~IEEE80211_C_TDMA;
158 /* NB: default configuration is passive so no beacons */
159 ts->tdma_version = TDMA_VERSION;
160 ts->tdma_slotlen = TDMA_SLOTLEN_DEFAULT;
161 ts->tdma_slotcnt = TDMA_SLOTCNT_DEFAULT;
162 ts->tdma_bintval = TDMA_BINTVAL_DEFAULT;
163 ts->tdma_slot = 1; /* passive operation */
165 /* setup default fixed rates */
166 settxparms(vap, IEEE80211_MODE_11A, TDMA_TXRATE_11A_DEFAULT);
167 settxparms(vap, IEEE80211_MODE_11B, TDMA_TXRATE_11B_DEFAULT);
168 settxparms(vap, IEEE80211_MODE_11G, TDMA_TXRATE_11G_DEFAULT);
169 settxparms(vap, IEEE80211_MODE_TURBO_A, TDMA_TXRATE_TURBO_DEFAULT);
170 settxparms(vap, IEEE80211_MODE_TURBO_G, TDMA_TXRATE_TURBO_DEFAULT);
171 settxparms(vap, IEEE80211_MODE_STURBO_A, TDMA_TXRATE_TURBO_DEFAULT);
172 settxparms(vap, IEEE80211_MODE_11NA, TDMA_TXRATE_11NA_DEFAULT);
173 settxparms(vap, IEEE80211_MODE_11NG, TDMA_TXRATE_11NG_DEFAULT);
174 settxparms(vap, IEEE80211_MODE_HALF, TDMA_TXRATE_HALF_DEFAULT);
175 settxparms(vap, IEEE80211_MODE_QUARTER, TDMA_TXRATE_QUARTER_DEFAULT);
177 setackpolicy(vap->iv_ic, 1); /* disable ACK's */
179 ts->tdma_opdetach = vap->iv_opdetach;
180 vap->iv_opdetach = tdma_vdetach;
181 ts->tdma_newstate = vap->iv_newstate;
182 vap->iv_newstate = tdma_newstate;
183 vap->iv_bmiss = tdma_beacon_miss;
184 ts->tdma_recv_mgmt = vap->iv_recv_mgmt;
185 vap->iv_recv_mgmt = tdma_recv_mgmt;
191 tdma_vdetach(struct ieee80211vap *vap)
193 struct ieee80211_tdma_state *ts = vap->iv_tdma;
196 /* NB: should not have touched any ic state */
199 ts->tdma_opdetach(vap);
200 free(vap->iv_tdma, M_80211_VAP);
203 setackpolicy(vap->iv_ic, 0); /* enable ACK's */
207 sta_leave(void *arg, struct ieee80211_node *ni)
209 struct ieee80211vap *vap = arg;
211 if (ni->ni_vap == vap && ni != vap->iv_bss)
212 ieee80211_node_leave(ni);
216 * TDMA state machine handler.
219 tdma_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg)
221 struct ieee80211_tdma_state *ts = vap->iv_tdma;
222 struct ieee80211com *ic = vap->iv_ic;
223 enum ieee80211_state ostate;
226 IEEE80211_LOCK_ASSERT(ic);
228 ostate = vap->iv_state;
229 IEEE80211_DPRINTF(vap, IEEE80211_MSG_STATE, "%s: %s -> %s (%d)\n",
230 __func__, ieee80211_state_name[ostate],
231 ieee80211_state_name[nstate], arg);
233 if (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS)
234 callout_stop(&vap->iv_swbmiss);
235 if (nstate == IEEE80211_S_SCAN &&
236 (ostate == IEEE80211_S_INIT || ostate == IEEE80211_S_RUN) &&
237 ts->tdma_slot != 0) {
239 * Override adhoc behaviour when operating as a slave;
240 * we need to scan even if the channel is locked.
242 vap->iv_state = nstate; /* state transition */
243 ieee80211_cancel_scan(vap); /* background scan */
244 if (ostate == IEEE80211_S_RUN) {
245 /* purge station table; entries are stale */
246 ieee80211_iterate_nodes(&ic->ic_sta, sta_leave, vap);
248 if (vap->iv_flags_ext & IEEE80211_FEXT_SCANREQ) {
249 ieee80211_check_scan(vap,
250 vap->iv_scanreq_flags,
251 vap->iv_scanreq_duration,
252 vap->iv_scanreq_mindwell,
253 vap->iv_scanreq_maxdwell,
254 vap->iv_scanreq_nssid, vap->iv_scanreq_ssid);
255 vap->iv_flags_ext &= ~IEEE80211_FEXT_SCANREQ;
257 ieee80211_check_scan_current(vap);
260 status = ts->tdma_newstate(vap, nstate, arg);
263 nstate == IEEE80211_S_RUN && ostate != IEEE80211_S_RUN &&
264 (vap->iv_flags_ext & IEEE80211_FEXT_SWBMISS) &&
265 ts->tdma_slot != 0 &&
266 vap->iv_des_chan == IEEE80211_CHAN_ANYC) {
268 * Start s/w beacon miss timer for slave devices w/o
269 * hardware support. Note we do this only if we're
270 * not locked to a channel (i.e. roam to follow the
271 * master). The 2x is a fudge for our doing this in
274 vap->iv_swbmiss_period = IEEE80211_TU_TO_TICKS(
275 2 * vap->iv_bmissthreshold * ts->tdma_bintval *
276 ((ts->tdma_slotcnt * ts->tdma_slotlen) / 1024));
277 vap->iv_swbmiss_count = 0;
278 callout_reset(&vap->iv_swbmiss, vap->iv_swbmiss_period,
279 ieee80211_swbmiss, vap);
285 tdma_beacon_miss(struct ieee80211vap *vap)
287 struct ieee80211_tdma_state *ts = vap->iv_tdma;
289 KASSERT((vap->iv_ic->ic_flags & IEEE80211_F_SCAN) == 0, ("scanning"));
290 KASSERT(vap->iv_state == IEEE80211_S_RUN,
291 ("wrong state %d", vap->iv_state));
293 IEEE80211_DPRINTF(vap,
294 IEEE80211_MSG_STATE | IEEE80211_MSG_TDMA | IEEE80211_MSG_DEBUG,
295 "beacon miss, mode %u state %s\n",
296 vap->iv_opmode, ieee80211_state_name[vap->iv_state]);
298 if (ts->tdma_peer != NULL) { /* XXX? can this be null? */
299 ieee80211_notify_node_leave(vap->iv_bss);
300 ts->tdma_peer = NULL;
302 * Treat beacon miss like an associate failure wrt the
303 * scan policy; this forces the entry in the scan cache
304 * to be ignored after several tries.
306 ieee80211_scan_assoc_fail(vap, vap->iv_bss->ni_macaddr,
307 IEEE80211_STATUS_TIMEOUT);
310 ts->tdma_inuse = 0; /* clear slot usage */
312 ieee80211_new_state(vap, IEEE80211_S_SCAN, 0);
316 tdma_recv_mgmt(struct ieee80211_node *ni, struct mbuf *m0,
317 int subtype, int rssi, int nf)
319 struct ieee80211com *ic = ni->ni_ic;
320 struct ieee80211vap *vap = ni->ni_vap;
321 struct ieee80211_tdma_state *ts = vap->iv_tdma;
323 if (subtype == IEEE80211_FC0_SUBTYPE_BEACON &&
324 (ic->ic_flags & IEEE80211_F_SCAN) == 0) {
325 struct ieee80211_frame *wh = mtod(m0, struct ieee80211_frame *);
326 struct ieee80211_scanparams scan;
328 if (ieee80211_parse_beacon(ni, m0, &scan) != 0)
330 if (scan.tdma == NULL) {
332 * TDMA stations must beacon a TDMA ie; ignore
334 * XXX detect overlapping bss and change channel
336 IEEE80211_DISCARD(vap,
337 IEEE80211_MSG_ELEMID | IEEE80211_MSG_INPUT,
338 wh, ieee80211_mgt_subtype_name[subtype >>
339 IEEE80211_FC0_SUBTYPE_SHIFT],
341 vap->iv_stats.is_rx_mgtdiscard++;
344 if (ni == vap->iv_bss &&
345 !IEEE80211_ADDR_EQ(wh->i_addr2, ni->ni_macaddr)) {
347 * Fake up a node for this newly
348 * discovered member of the IBSS.
350 ni = ieee80211_add_neighbor(vap, wh, &scan);
352 /* NB: stat kept for alloc failure */
357 * Check for state updates.
359 if (IEEE80211_ADDR_EQ(wh->i_addr3, ni->ni_bssid)) {
361 * Count frame now that we know it's to be processed.
363 vap->iv_stats.is_rx_beacon++;
364 IEEE80211_NODE_STAT(ni, rx_beacons);
366 * Record tsf of last beacon. NB: this must be
367 * done before calling tdma_process_params
368 * as deeper routines reference it.
370 memcpy(&ni->ni_tstamp.data, scan.tstamp,
371 sizeof(ni->ni_tstamp.data));
373 * Count beacon frame for s/w bmiss handling.
375 vap->iv_swbmiss_count++;
377 * Process tdma ie. The contents are used to sync
378 * the slot timing, reconfigure the bss, etc.
380 (void) tdma_process_params(ni, scan.tdma, rssi, nf, wh);
384 * NB: defer remaining work to the adhoc code; this causes
385 * 2x parsing of the frame but should happen infrequently
388 ts->tdma_recv_mgmt(ni, m0, subtype, rssi, nf);
392 * Update TDMA state on receipt of a beacon frame with
393 * a TDMA information element. The sender's identity
394 * is provided so we can track who our peer is. If pickslot
395 * is non-zero we scan the slot allocation state in the ie
396 * to locate a free slot for our use.
399 tdma_update(struct ieee80211vap *vap, const struct ieee80211_tdma_param *tdma,
400 struct ieee80211_node *ni, int pickslot)
402 struct ieee80211_tdma_state *ts = vap->iv_tdma;
403 int slot, slotlen, update;
405 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
406 ("not a tdma vap, caps 0x%x", vap->iv_caps));
409 if (tdma->tdma_slotcnt != ts->tdma_slotcnt) {
410 if (!TDMA_SLOTCNT_VALID(tdma->tdma_slotcnt)) {
411 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
412 printf("%s: bad slot cnt %u\n",
413 __func__, tdma->tdma_slotcnt);
416 update |= TDMA_UPDATE_SLOTCNT;
418 slotlen = le16toh(tdma->tdma_slotlen) * 100;
419 if (slotlen != ts->tdma_slotlen) {
420 if (!TDMA_SLOTLEN_VALID(slotlen)) {
421 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
422 printf("%s: bad slot len %u\n",
426 update |= TDMA_UPDATE_SLOTLEN;
428 if (tdma->tdma_bintval != ts->tdma_bintval) {
429 if (!TDMA_BINTVAL_VALID(tdma->tdma_bintval)) {
430 if (ppsratecheck(&ts->tdma_lastprint, &ts->tdma_fails, 1))
431 printf("%s: bad beacon interval %u\n",
432 __func__, tdma->tdma_bintval);
435 update |= TDMA_UPDATE_BINTVAL;
437 slot = ts->tdma_slot;
440 * Pick unoccupied slot. Note we never choose slot 0.
442 for (slot = tdma->tdma_slotcnt-1; slot > 0; slot--)
443 if (isclr(tdma->tdma_inuse, slot))
446 printf("%s: no free slot, slotcnt %u inuse: 0x%x\n",
447 __func__, tdma->tdma_slotcnt,
448 tdma->tdma_inuse[0]);
449 /* XXX need to do something better */
452 if (slot != ts->tdma_slot)
453 update |= TDMA_UPDATE_SLOT;
455 if (ni != ts->tdma_peer) {
456 /* update everything */
457 update = TDMA_UPDATE_SLOT
458 | TDMA_UPDATE_SLOTCNT
459 | TDMA_UPDATE_SLOTLEN
460 | TDMA_UPDATE_BINTVAL;
465 * New/changed parameters; update runtime state.
467 /* XXX overwrites user parameters */
468 if (update & TDMA_UPDATE_SLOTCNT)
469 ts->tdma_slotcnt = tdma->tdma_slotcnt;
470 if (update & TDMA_UPDATE_SLOTLEN)
471 ts->tdma_slotlen = slotlen;
472 if (update & TDMA_UPDATE_SLOT)
473 ts->tdma_slot = slot;
474 if (update & TDMA_UPDATE_BINTVAL)
475 ts->tdma_bintval = tdma->tdma_bintval;
476 /* mark beacon to be updated before next xmit */
477 ieee80211_beacon_notify(vap, IEEE80211_BEACON_TDMA);
479 IEEE80211_DPRINTF(vap, IEEE80211_MSG_TDMA,
480 "%s: slot %u slotcnt %u slotlen %u us bintval %u\n",
481 __func__, ts->tdma_slot, ts->tdma_slotcnt,
482 ts->tdma_slotlen, ts->tdma_bintval);
485 * Notify driver. Note we can be called before
486 * entering RUN state if we scanned and are
487 * joining an existing bss. In that case do not
488 * call the driver because not all necessary state
489 * has been setup. The next beacon will dtrt.
491 if (vap->iv_state == IEEE80211_S_RUN)
492 vap->iv_ic->ic_tdma_update(ni, tdma, update);
494 * Dispatch join event on first beacon from new master.
496 if (ts->tdma_peer != ni) {
497 if (ts->tdma_peer != NULL)
498 ieee80211_notify_node_leave(vap->iv_bss);
499 ieee80211_notify_node_join(ni, 1);
500 /* NB: no reference, we just use the address */
507 * Process received TDMA parameters.
510 tdma_process_params(struct ieee80211_node *ni, const u_int8_t *ie,
511 int rssi, int nf, const struct ieee80211_frame *wh)
513 struct ieee80211vap *vap = ni->ni_vap;
514 struct ieee80211_tdma_state *ts = vap->iv_tdma;
515 const struct ieee80211_tdma_param *tdma =
516 (const struct ieee80211_tdma_param *) ie;
519 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
520 ("not a tdma vap, caps 0x%x", vap->iv_caps));
522 if (len < sizeof(*tdma) - 2) {
523 IEEE80211_DISCARD_IE(vap,
524 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
525 wh, "tdma", "too short, len %u", len);
526 return IEEE80211_REASON_IE_INVALID;
528 if (tdma->tdma_version != ts->tdma_version) {
529 IEEE80211_DISCARD_IE(vap,
530 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
531 wh, "tdma", "bad version %u (ours %u)",
532 tdma->tdma_version, ts->tdma_version);
533 return IEEE80211_REASON_IE_INVALID;
536 * NB: ideally we'd check against tdma_slotcnt, but that
537 * would require extra effort so do this easy check that
538 * covers the work below; more stringent checks are done
539 * before we make more extensive use of the ie contents.
541 if (tdma->tdma_slot >= TDMA_MAXSLOTS) {
542 IEEE80211_DISCARD_IE(vap,
543 IEEE80211_MSG_ELEMID | IEEE80211_MSG_TDMA,
544 wh, "tdma", "invalid slot %u", tdma->tdma_slot);
545 return IEEE80211_REASON_IE_INVALID;
548 * Can reach here while scanning, update
549 * operational state only in RUN state.
551 if (vap->iv_state == IEEE80211_S_RUN) {
552 if (tdma->tdma_slot != ts->tdma_slot &&
553 isclr(ts->tdma_inuse, tdma->tdma_slot)) {
554 IEEE80211_NOTE(vap, IEEE80211_MSG_TDMA, ni,
555 "discovered in slot %u", tdma->tdma_slot);
556 setbit(ts->tdma_inuse, tdma->tdma_slot);
557 /* XXX dispatch event only when operating as master */
558 if (ts->tdma_slot == 0)
559 ieee80211_notify_node_join(ni, 1);
561 setbit(ts->tdma_active, tdma->tdma_slot);
562 if (tdma->tdma_slot == ts->tdma_slot-1) {
564 * Slave tsf synchronization to station
565 * just before us in the schedule. The driver
566 * is responsible for copying the timestamp
567 * of the received beacon into our beacon
568 * frame so the sender can calculate round
569 * trip time. We cannot do that here because
570 * we don't know how to update our beacon frame.
572 (void) tdma_update(vap, tdma, ni, 0);
573 /* XXX reschedule swbmiss timer on parameter change */
574 } else if (tdma->tdma_slot == ts->tdma_slot+1) {
577 uint32_t rstamp = (uint32_t) le64toh(rs->tsf);
581 * Use returned timstamp to calculate the
584 memcpy(&tstamp, tdma->tdma_tstamp, 8);
586 /* XXX use only 15 bits of rstamp */
587 rtt = rstamp - (le64toh(tstamp) & 0x7fff);
590 /* XXX hack to quiet normal use */
591 IEEE80211_DPRINTF(vap, IEEE80211_MSG_DOT1X,
592 "tdma rtt %5u [rstamp %5u tstamp %llu]\n",
594 (unsigned long long) le64toh(tstamp));
596 } else if (tdma->tdma_slot == ts->tdma_slot &&
597 le64toh(ni->ni_tstamp.tsf) > vap->iv_bss->ni_tstamp.tsf) {
599 * Station using the same slot as us and has
600 * been around longer than us; we must move.
601 * Note this can happen if stations do not
602 * see each other while scanning.
604 IEEE80211_DPRINTF(vap, IEEE80211_MSG_TDMA,
605 "slot %u collision rxtsf %llu tsf %llu\n",
607 (unsigned long long) le64toh(ni->ni_tstamp.tsf),
608 vap->iv_bss->ni_tstamp.tsf);
609 setbit(ts->tdma_inuse, tdma->tdma_slot);
611 (void) tdma_update(vap, tdma, ni, 1);
618 ieee80211_tdma_getslot(struct ieee80211vap *vap)
620 struct ieee80211_tdma_state *ts = vap->iv_tdma;
622 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
623 ("not a tdma vap, caps 0x%x", vap->iv_caps));
624 return ts->tdma_slot;
628 * Parse a TDMA ie on station join and use it to setup node state.
631 ieee80211_parse_tdma(struct ieee80211_node *ni, const uint8_t *ie)
633 struct ieee80211vap *vap = ni->ni_vap;
635 if (vap->iv_caps & IEEE80211_C_TDMA) {
636 const struct ieee80211_tdma_param *tdma =
637 (const struct ieee80211_tdma_param *)ie;
638 struct ieee80211_tdma_state *ts = vap->iv_tdma;
640 * Adopt TDMA configuration when joining an
643 setbit(ts->tdma_inuse, tdma->tdma_slot);
644 (void) tdma_update(vap, tdma, ni, 1);
646 * Propagate capabilities based on the local
647 * configuration and the remote station's advertised
648 * capabilities. In particular this permits us to
649 * enable use of QoS to disable ACK's.
651 if ((vap->iv_flags & IEEE80211_F_WME) &&
652 ni->ni_ies.wme_ie != NULL)
653 ni->ni_flags |= IEEE80211_NODE_QOS;
657 #define TDMA_OUI_BYTES 0x00, 0x03, 0x7f
659 * Add a TDMA parameters element to a frame.
662 ieee80211_add_tdma(uint8_t *frm, struct ieee80211vap *vap)
664 #define ADDSHORT(frm, v) do { \
665 frm[0] = (v) & 0xff; \
669 static const struct ieee80211_tdma_param param = {
670 .tdma_id = IEEE80211_ELEMID_VENDOR,
671 .tdma_len = sizeof(struct ieee80211_tdma_param) - 2,
672 .tdma_oui = { TDMA_OUI_BYTES },
673 .tdma_type = TDMA_OUI_TYPE,
674 .tdma_subtype = TDMA_SUBTYPE_PARAM,
675 .tdma_version = TDMA_VERSION,
677 const struct ieee80211_tdma_state *ts = vap->iv_tdma;
680 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
681 ("not a tdma vap, caps 0x%x", vap->iv_caps));
683 memcpy(frm, ¶m, sizeof(param));
684 frm += __offsetof(struct ieee80211_tdma_param, tdma_slot);
685 *frm++ = ts->tdma_slot;
686 *frm++ = ts->tdma_slotcnt;
687 /* NB: convert units to fit in 16-bits */
688 slotlen = ts->tdma_slotlen / 100; /* 100us units */
689 ADDSHORT(frm, slotlen);
690 *frm++ = ts->tdma_bintval;
691 *frm++ = ts->tdma_inuse[0];
692 frm += 10; /* pad+timestamp */
696 #undef TDMA_OUI_BYTES
699 * Update TDMA state at TBTT.
702 ieee80211_tdma_update_beacon(struct ieee80211vap *vap,
703 struct ieee80211_beacon_offsets *bo)
705 struct ieee80211_tdma_state *ts = vap->iv_tdma;
707 KASSERT(vap->iv_caps & IEEE80211_C_TDMA,
708 ("not a tdma vap, caps 0x%x", vap->iv_caps));
710 if (isset(bo->bo_flags, IEEE80211_BEACON_TDMA)) {
711 (void) ieee80211_add_tdma(bo->bo_tdma, vap);
712 clrbit(bo->bo_flags, IEEE80211_BEACON_TDMA);
714 if (ts->tdma_slot != 0) /* only on master */
716 if (ts->tdma_count <= 0) {
718 * Time to update the mask of active/inuse stations.
719 * We track stations that we've received a beacon
720 * frame from and update this mask periodically.
721 * This allows us to miss a few beacons before marking
722 * a slot free for re-use.
724 ts->tdma_inuse[0] = ts->tdma_active[0];
725 ts->tdma_active[0] = 0x01;
726 /* update next time 'round */
727 /* XXX use notify framework */
728 setbit(bo->bo_flags, IEEE80211_BEACON_TDMA);
729 /* NB: use s/w beacon miss threshold; may be too high */
730 ts->tdma_count = vap->iv_bmissthreshold-1;
736 tdma_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
738 struct ieee80211_tdma_state *ts = vap->iv_tdma;
740 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
743 switch (ireq->i_type) {
744 case IEEE80211_IOC_TDMA_SLOT:
745 ireq->i_val = ts->tdma_slot;
747 case IEEE80211_IOC_TDMA_SLOTCNT:
748 ireq->i_val = ts->tdma_slotcnt;
750 case IEEE80211_IOC_TDMA_SLOTLEN:
751 ireq->i_val = ts->tdma_slotlen;
753 case IEEE80211_IOC_TDMA_BINTERVAL:
754 ireq->i_val = ts->tdma_bintval;
761 IEEE80211_IOCTL_GET(tdma, tdma_ioctl_get80211);
764 tdma_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
766 struct ieee80211_tdma_state *ts = vap->iv_tdma;
768 if ((vap->iv_caps & IEEE80211_C_TDMA) == 0)
771 switch (ireq->i_type) {
772 case IEEE80211_IOC_TDMA_SLOT:
773 if (!(0 <= ireq->i_val && ireq->i_val <= ts->tdma_slotcnt))
775 if (ireq->i_val != ts->tdma_slot) {
776 ts->tdma_slot = ireq->i_val;
780 case IEEE80211_IOC_TDMA_SLOTCNT:
781 if (!TDMA_SLOTCNT_VALID(ireq->i_val))
783 if (ireq->i_val != ts->tdma_slotcnt) {
784 ts->tdma_slotcnt = ireq->i_val;
788 case IEEE80211_IOC_TDMA_SLOTLEN:
791 * 150 insures at least 1/8 TU
792 * 0xfffff is the max duration for bursting
793 * (implict by way of 16-bit data type for i_val)
795 if (!TDMA_SLOTLEN_VALID(ireq->i_val))
797 if (ireq->i_val != ts->tdma_slotlen) {
798 ts->tdma_slotlen = ireq->i_val;
802 case IEEE80211_IOC_TDMA_BINTERVAL:
803 if (!TDMA_BINTVAL_VALID(ireq->i_val))
805 if (ireq->i_val != ts->tdma_bintval) {
806 ts->tdma_bintval = ireq->i_val;
815 ieee80211_beacon_notify(vap, IEEE80211_BEACON_TDMA);
818 IEEE80211_IOCTL_SET(tdma, tdma_ioctl_set80211);