xref: /freebsd/sys/net80211/ieee80211_power.c (revision f126890ac5386406dadf7c4cfa9566cbb56537c5)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <sys/cdefs.h>
29 /*
30  * IEEE 802.11 power save support.
31  */
32 #include "opt_wlan.h"
33 
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/malloc.h>
38 
39 #include <sys/socket.h>
40 
41 #include <net/if.h>
42 #include <net/if_var.h>
43 #include <net/if_media.h>
44 #include <net/ethernet.h>
45 
46 #include <net80211/ieee80211_var.h>
47 
48 #include <net/bpf.h>
49 
50 static void ieee80211_update_ps(struct ieee80211vap *, int);
51 static int ieee80211_set_tim(struct ieee80211_node *, int);
52 
53 static MALLOC_DEFINE(M_80211_POWER, "80211power", "802.11 power save state");
54 
55 void
56 ieee80211_power_attach(struct ieee80211com *ic)
57 {
58 }
59 
60 void
61 ieee80211_power_detach(struct ieee80211com *ic)
62 {
63 }
64 
65 void
66 ieee80211_power_vattach(struct ieee80211vap *vap)
67 {
68 	if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
69 	    vap->iv_opmode == IEEE80211_M_IBSS) {
70 		/* NB: driver should override */
71 		vap->iv_update_ps = ieee80211_update_ps;
72 		vap->iv_set_tim = ieee80211_set_tim;
73 	}
74 	vap->iv_node_ps = ieee80211_node_pwrsave;
75 	vap->iv_sta_ps = ieee80211_sta_pwrsave;
76 }
77 
78 void
79 ieee80211_power_latevattach(struct ieee80211vap *vap)
80 {
81 	/*
82 	 * Allocate these only if needed.  Beware that we
83 	 * know adhoc mode doesn't support ATIM yet...
84 	 */
85 	if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
86 		vap->iv_tim_len = howmany(vap->iv_max_aid,8) * sizeof(uint8_t);
87 		vap->iv_tim_bitmap = (uint8_t *) IEEE80211_MALLOC(vap->iv_tim_len,
88 			M_80211_POWER,
89 			IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
90 		if (vap->iv_tim_bitmap == NULL) {
91 			printf("%s: no memory for TIM bitmap!\n", __func__);
92 			/* XXX good enough to keep from crashing? */
93 			vap->iv_tim_len = 0;
94 		}
95 	}
96 }
97 
98 void
99 ieee80211_power_vdetach(struct ieee80211vap *vap)
100 {
101 	if (vap->iv_tim_bitmap != NULL) {
102 		IEEE80211_FREE(vap->iv_tim_bitmap, M_80211_POWER);
103 		vap->iv_tim_bitmap = NULL;
104 	}
105 }
106 
107 void
108 ieee80211_psq_init(struct ieee80211_psq *psq, const char *name)
109 {
110 	memset(psq, 0, sizeof(*psq));
111 	psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE;
112 	IEEE80211_PSQ_INIT(psq, name);		/* OS-dependent setup */
113 }
114 
115 void
116 ieee80211_psq_cleanup(struct ieee80211_psq *psq)
117 {
118 #if 0
119 	psq_drain(psq);				/* XXX should not be needed? */
120 #else
121 	KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len));
122 #endif
123 	IEEE80211_PSQ_DESTROY(psq);		/* OS-dependent cleanup */
124 }
125 
126 /*
127  * Return the highest priority frame in the ps queue.
128  */
129 struct mbuf *
130 ieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen)
131 {
132 	struct ieee80211_psq *psq = &ni->ni_psq;
133 	struct ieee80211_psq_head *qhead;
134 	struct mbuf *m;
135 
136 	IEEE80211_PSQ_LOCK(psq);
137 	qhead = &psq->psq_head[0];
138 again:
139 	if ((m = qhead->head) != NULL) {
140 		if ((qhead->head = m->m_nextpkt) == NULL)
141 			qhead->tail = NULL;
142 		KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
143 		qhead->len--;
144 		KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
145 		psq->psq_len--;
146 		m->m_nextpkt = NULL;
147 	}
148 	if (m == NULL && qhead == &psq->psq_head[0]) {
149 		/* Algol-68 style for loop */
150 		qhead = &psq->psq_head[1];
151 		goto again;
152 	}
153 	if (qlen != NULL)
154 		*qlen = psq->psq_len;
155 	IEEE80211_PSQ_UNLOCK(psq);
156 	return m;
157 }
158 
159 /*
160  * Reclaim an mbuf from the ps q.  If marked with M_ENCAP
161  * we assume there is a node reference that must be relcaimed.
162  */
163 static void
164 psq_mfree(struct mbuf *m)
165 {
166 	if (m->m_flags & M_ENCAP) {
167 		struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif;
168 		ieee80211_free_node(ni);
169 	}
170 	m->m_nextpkt = NULL;
171 	m_freem(m);
172 }
173 
174 /*
175  * Clear any frames queued in the power save queue.
176  * The number of frames that were present is returned.
177  */
178 static int
179 psq_drain(struct ieee80211_psq *psq)
180 {
181 	struct ieee80211_psq_head *qhead;
182 	struct mbuf *m;
183 	int qlen;
184 
185 	IEEE80211_PSQ_LOCK(psq);
186 	qlen = psq->psq_len;
187 	qhead = &psq->psq_head[0];
188 again:
189 	while ((m = qhead->head) != NULL) {
190 		qhead->head = m->m_nextpkt;
191 		psq_mfree(m);
192 	}
193 	qhead->tail = NULL;
194 	qhead->len = 0;
195 	if (qhead == &psq->psq_head[0]) {	/* Algol-68 style for loop */
196 		qhead = &psq->psq_head[1];
197 		goto again;
198 	}
199 	psq->psq_len = 0;
200 	IEEE80211_PSQ_UNLOCK(psq);
201 
202 	return qlen;
203 }
204 
205 /*
206  * Clear any frames queued in the power save queue.
207  * The number of frames that were present is returned.
208  */
209 int
210 ieee80211_node_psq_drain(struct ieee80211_node *ni)
211 {
212 	return psq_drain(&ni->ni_psq);
213 }
214 
215 /*
216  * Age frames on the power save queue. The aging interval is
217  * 4 times the listen interval specified by the station.  This
218  * number is factored into the age calculations when the frame
219  * is placed on the queue.  We store ages as time differences
220  * so we can check and/or adjust only the head of the list.
221  * If a frame's age exceeds the threshold then discard it.
222  * The number of frames discarded is returned so the caller
223  * can check if it needs to adjust the tim.
224  */
225 int
226 ieee80211_node_psq_age(struct ieee80211_node *ni)
227 {
228 	struct ieee80211_psq *psq = &ni->ni_psq;
229 	int discard = 0;
230 
231 	if (psq->psq_len != 0) {
232 #ifdef IEEE80211_DEBUG
233 		struct ieee80211vap *vap = ni->ni_vap;
234 #endif
235 		struct ieee80211_psq_head *qhead;
236 		struct mbuf *m;
237 
238 		IEEE80211_PSQ_LOCK(psq);
239 		qhead = &psq->psq_head[0];
240 	again:
241 		while ((m = qhead->head) != NULL &&
242 		    M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
243 			IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
244 			     "discard frame, age %u", M_AGE_GET(m));
245 			if ((qhead->head = m->m_nextpkt) == NULL)
246 				qhead->tail = NULL;
247 			KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
248 			qhead->len--;
249 			KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
250 			psq->psq_len--;
251 			psq_mfree(m);
252 			discard++;
253 		}
254 		if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
255 			qhead = &psq->psq_head[1];
256 			goto again;
257 		}
258 		if (m != NULL)
259 			M_AGE_SUB(m, IEEE80211_INACT_WAIT);
260 		IEEE80211_PSQ_UNLOCK(psq);
261 
262 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
263 		    "discard %u frames for age", discard);
264 		IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
265 	}
266 	return discard;
267 }
268 
269 /*
270  * Handle a change in the PS station occupancy.
271  */
272 static void
273 ieee80211_update_ps(struct ieee80211vap *vap, int nsta)
274 {
275 
276 	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
277 		vap->iv_opmode == IEEE80211_M_IBSS,
278 		("operating mode %u", vap->iv_opmode));
279 }
280 
281 /*
282  * Indicate whether there are frames queued for a station in power-save mode.
283  */
284 static int
285 ieee80211_set_tim(struct ieee80211_node *ni, int set)
286 {
287 	struct ieee80211vap *vap = ni->ni_vap;
288 	struct ieee80211com *ic = ni->ni_ic;
289 	uint16_t aid;
290 	int changed;
291 
292 	KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
293 		vap->iv_opmode == IEEE80211_M_IBSS,
294 		("operating mode %u", vap->iv_opmode));
295 
296 	aid = IEEE80211_AID(ni->ni_associd);
297 	KASSERT(aid < vap->iv_max_aid,
298 		("bogus aid %u, max %u", aid, vap->iv_max_aid));
299 
300 	IEEE80211_LOCK(ic);
301 	changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0));
302 	if (changed) {
303 		if (set) {
304 			setbit(vap->iv_tim_bitmap, aid);
305 			vap->iv_ps_pending++;
306 		} else {
307 			clrbit(vap->iv_tim_bitmap, aid);
308 			vap->iv_ps_pending--;
309 		}
310 		/* NB: we know vap is in RUN state so no need to check */
311 		vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM);
312 	}
313 	IEEE80211_UNLOCK(ic);
314 
315 	return changed;
316 }
317 
318 /*
319  * Save an outbound packet for a node in power-save sleep state.
320  * The new packet is placed on the node's saved queue, and the TIM
321  * is changed, if necessary.
322  */
323 int
324 ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
325 {
326 	struct ieee80211_psq *psq = &ni->ni_psq;
327 	struct ieee80211vap *vap = ni->ni_vap;
328 	struct ieee80211com *ic = ni->ni_ic;
329 	struct ieee80211_psq_head *qhead;
330 	int qlen, age;
331 
332 	IEEE80211_PSQ_LOCK(psq);
333 	if (psq->psq_len >= psq->psq_maxlen) {
334 		psq->psq_drops++;
335 		IEEE80211_PSQ_UNLOCK(psq);
336 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
337 		    "pwr save q overflow, drops %d (size %d)",
338 		    psq->psq_drops, psq->psq_len);
339 #ifdef IEEE80211_DEBUG
340 		if (ieee80211_msg_dumppkts(vap))
341 			ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t),
342 			    m->m_len, -1, -1);
343 #endif
344 		psq_mfree(m);
345 		return ENOSPC;
346 	}
347 	/*
348 	 * Tag the frame with it's expiry time and insert it in
349 	 * the appropriate queue.  The aging interval is 4 times
350 	 * the listen interval specified by the station. Frames
351 	 * that sit around too long are reclaimed using this
352 	 * information.
353 	 */
354 	/* TU -> secs.  XXX handle overflow? */
355 	age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
356 	/*
357 	 * Encapsulated frames go on the high priority queue,
358 	 * other stuff goes on the low priority queue.  We use
359 	 * this to order frames returned out of the driver
360 	 * ahead of frames we collect in ieee80211_start.
361 	 */
362 	if (m->m_flags & M_ENCAP)
363 		qhead = &psq->psq_head[0];
364 	else
365 		qhead = &psq->psq_head[1];
366 	if (qhead->tail == NULL) {
367 		struct mbuf *mh;
368 
369 		qhead->head = m;
370 		/*
371 		 * Take care to adjust age when inserting the first
372 		 * frame of a queue and the other queue already has
373 		 * frames.  We need to preserve the age difference
374 		 * relationship so ieee80211_node_psq_age works.
375 		 */
376 		if (qhead == &psq->psq_head[1]) {
377 			mh = psq->psq_head[0].head;
378 			if (mh != NULL)
379 				age-= M_AGE_GET(mh);
380 		} else {
381 			mh = psq->psq_head[1].head;
382 			if (mh != NULL) {
383 				int nage = M_AGE_GET(mh) - age;
384 				/* XXX is clamping to zero good 'nuf? */
385 				M_AGE_SET(mh, nage < 0 ? 0 : nage);
386 			}
387 		}
388 	} else {
389 		qhead->tail->m_nextpkt = m;
390 		age -= M_AGE_GET(qhead->head);
391 	}
392 	KASSERT(age >= 0, ("age %d", age));
393 	M_AGE_SET(m, age);
394 	m->m_nextpkt = NULL;
395 	qhead->tail = m;
396 	qhead->len++;
397 	qlen = ++(psq->psq_len);
398 	IEEE80211_PSQ_UNLOCK(psq);
399 
400 	IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
401 	    "save frame with age %d, %u now queued", age, qlen);
402 
403 	if (qlen == 1 && vap->iv_set_tim != NULL)
404 		vap->iv_set_tim(ni, 1);
405 
406 	return 0;
407 }
408 
409 /*
410  * Move frames from the ps q to the vap's send queue
411  * and/or the driver's send queue; and kick the start
412  * method for each, as appropriate.  Note we're careful
413  * to preserve packet ordering here.
414  */
415 static void
416 pwrsave_flushq(struct ieee80211_node *ni)
417 {
418 	struct ieee80211_psq *psq = &ni->ni_psq;
419 	struct ieee80211com *ic = ni->ni_ic;
420 	struct ieee80211vap *vap = ni->ni_vap;
421 	struct ieee80211_psq_head *qhead;
422 	struct mbuf *parent_q = NULL, *ifp_q = NULL;
423 	struct mbuf *m;
424 
425 	IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
426 	    "flush ps queue, %u packets queued", psq->psq_len);
427 
428 	IEEE80211_PSQ_LOCK(psq);
429 	qhead = &psq->psq_head[0];	/* 802.11 frames */
430 	if (qhead->head != NULL) {
431 		/* XXX could dispatch through vap and check M_ENCAP */
432 		/* XXX need different driver interface */
433 		/* XXX bypasses q max and OACTIVE */
434 		parent_q = qhead->head;
435 		qhead->head = qhead->tail = NULL;
436 		qhead->len = 0;
437 	}
438 
439 	qhead = &psq->psq_head[1];	/* 802.3 frames */
440 	if (qhead->head != NULL) {
441 		/* XXX need different driver interface */
442 		/* XXX bypasses q max and OACTIVE */
443 		ifp_q = qhead->head;
444 		qhead->head = qhead->tail = NULL;
445 		qhead->len = 0;
446 	}
447 	psq->psq_len = 0;
448 	IEEE80211_PSQ_UNLOCK(psq);
449 
450 	/* NB: do this outside the psq lock */
451 	/* XXX packets might get reordered if parent is OACTIVE */
452 	/* parent frames, should be encapsulated */
453 	while (parent_q != NULL) {
454 		m = parent_q;
455 		parent_q = m->m_nextpkt;
456 		m->m_nextpkt = NULL;
457 		/* must be encapsulated */
458 		KASSERT((m->m_flags & M_ENCAP),
459 		    ("%s: parentq with non-M_ENCAP frame!\n",
460 		    __func__));
461 		(void) ieee80211_parent_xmitpkt(ic, m);
462 	}
463 
464 	/* VAP frames, aren't encapsulated */
465 	while (ifp_q != NULL) {
466 		m = ifp_q;
467 		ifp_q = m->m_nextpkt;
468 		m->m_nextpkt = NULL;
469 		KASSERT((!(m->m_flags & M_ENCAP)),
470 		    ("%s: vapq with M_ENCAP frame!\n", __func__));
471 		(void) ieee80211_vap_xmitpkt(vap, m);
472 	}
473 }
474 
475 /*
476  * Handle station power-save state change.
477  */
478 void
479 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
480 {
481 	struct ieee80211vap *vap = ni->ni_vap;
482 	int update;
483 
484 	update = 0;
485 	if (enable) {
486 		if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
487 			vap->iv_ps_sta++;
488 			update = 1;
489 		}
490 		ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
491 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
492 		    "power save mode on, %u sta's in ps mode", vap->iv_ps_sta);
493 
494 		if (update)
495 			vap->iv_update_ps(vap, vap->iv_ps_sta);
496 	} else {
497 		if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
498 			vap->iv_ps_sta--;
499 			update = 1;
500 		}
501 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
502 		IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
503 		    "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
504 
505 		/* NB: order here is intentional so TIM is clear before flush */
506 		if (vap->iv_set_tim != NULL)
507 			vap->iv_set_tim(ni, 0);
508 		if (update) {
509 			/* NB if no sta's in ps, driver should flush mc q */
510 			vap->iv_update_ps(vap, vap->iv_ps_sta);
511 		}
512 		if (ni->ni_psq.psq_len != 0)
513 			pwrsave_flushq(ni);
514 	}
515 }
516 
517 /*
518  * Handle power-save state change in station mode.
519  */
520 void
521 ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
522 {
523 	struct ieee80211_node *ni = vap->iv_bss;
524 
525 	if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
526 		return;
527 
528 	IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
529 	    "sta power save mode %s", enable ? "on" : "off");
530 	if (!enable) {
531 		ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
532 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
533 		/*
534 		 * Flush any queued frames; we can do this immediately
535 		 * because we know they'll be queued behind the null
536 		 * data frame we send the ap.
537 		 * XXX can we use a data frame to take us out of ps?
538 		 */
539 		if (ni->ni_psq.psq_len != 0)
540 			pwrsave_flushq(ni);
541 	} else {
542 		ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
543 		ieee80211_send_nulldata(ieee80211_ref_node(ni));
544 	}
545 }
546 
547 /*
548  * Handle being notified that we have data available for us in a TIM/ATIM.
549  *
550  * This may schedule a transition from _SLEEP -> _RUN if it's appropriate.
551  *
552  * In STA mode, we may have put to sleep during scan and need to be dragged
553  * back out of powersave mode.
554  */
555 void
556 ieee80211_sta_tim_notify(struct ieee80211vap *vap, int set)
557 {
558 	struct ieee80211com *ic = vap->iv_ic;
559 
560 	/*
561 	 * Schedule the driver state change.  It'll happen at some point soon.
562 	 * Since the hardware shouldn't know that we're running just yet
563 	 * (and thus tell the peer that we're awake before we actually wake
564 	 * up said hardware), we leave the actual node state transition
565 	 * up to the transition to RUN.
566 	 *
567 	 * XXX TODO: verify that the transition to RUN will wake up the
568 	 * BSS node!
569 	 */
570 	IEEE80211_LOCK(vap->iv_ic);
571 	if (set == 1 && vap->iv_state == IEEE80211_S_SLEEP) {
572 		ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
573 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
574 		    "%s: TIM=%d; wakeup\n", __func__, set);
575 	} else if ((set == 1) && (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN)) {
576 		/*
577 		 * XXX only do this if we're in RUN state?
578 		 */
579 		IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
580 		    "%s: wake up from bgscan vap sleep\n",
581 		    __func__);
582 		/*
583 		 * We may be in BGSCAN mode - this means the VAP is in STA
584 		 * mode powersave.  If it is, we need to wake it up so we
585 		 * can process outbound traffic.
586 		 */
587 		vap->iv_sta_ps(vap, 0);
588 	}
589 	IEEE80211_UNLOCK(vap->iv_ic);
590 }
591 
592 /*
593  * Timer check on whether the VAP has had any transmit activity.
594  *
595  * This may schedule a transition from _RUN -> _SLEEP if it's appropriate.
596  */
597 void
598 ieee80211_sta_ps_timer_check(struct ieee80211vap *vap)
599 {
600 	struct ieee80211com *ic = vap->iv_ic;
601 
602 	/* XXX lock assert */
603 
604 	/* For no, only do this in STA mode */
605 	if (! (vap->iv_caps & IEEE80211_C_SWSLEEP))
606 		goto out;
607 
608 	if (vap->iv_opmode != IEEE80211_M_STA)
609 		goto out;
610 
611 	/* If we're not at run state, bail */
612 	if (vap->iv_state != IEEE80211_S_RUN)
613 		goto out;
614 
615 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
616 	    "%s: lastdata=%llu, ticks=%llu\n",
617 	    __func__, (unsigned long long) ic->ic_lastdata,
618 	    (unsigned long long) ticks);
619 
620 	/* If powersave is disabled on the VAP, don't bother */
621 	if (! (vap->iv_flags & IEEE80211_F_PMGTON))
622 		goto out;
623 
624 	/* If we've done any data within our idle interval, bail */
625 	/* XXX hard-coded to one second for now, ew! */
626 	if (ieee80211_time_after(ic->ic_lastdata + 500, ticks))
627 		goto out;
628 
629 	/*
630 	 * Signify we're going into power save and transition the
631 	 * node to powersave.
632 	 */
633 	if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
634 		vap->iv_sta_ps(vap, 1);
635 
636 	/*
637 	 * XXX The driver has to handle the fact that we're going
638 	 * to sleep but frames may still be transmitted;
639 	 * hopefully it and/or us will do the right thing and mark any
640 	 * transmitted frames with PWRMGT set to 1.
641 	 */
642 	ieee80211_new_state_locked(vap, IEEE80211_S_SLEEP, 0);
643 
644 	IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
645 	    "%s: time delta=%d msec\n", __func__,
646 	    (int) ticks_to_msecs(ticks - ic->ic_lastdata));
647 
648 out:
649 	return;
650 }
651