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