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