2 * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
30 * IEEE 802.11 power save support.
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
38 #include <sys/socket.h>
41 #include <net/if_var.h>
42 #include <net/if_media.h>
43 #include <net/ethernet.h>
45 #include <net80211/ieee80211_var.h>
49 static void ieee80211_update_ps(struct ieee80211vap *, int);
50 static int ieee80211_set_tim(struct ieee80211_node *, int);
52 static MALLOC_DEFINE(M_80211_POWER, "80211power", "802.11 power save state");
55 ieee80211_power_attach(struct ieee80211com *ic)
60 ieee80211_power_detach(struct ieee80211com *ic)
65 ieee80211_power_vattach(struct ieee80211vap *vap)
67 if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
68 vap->iv_opmode == IEEE80211_M_IBSS) {
69 /* NB: driver should override */
70 vap->iv_update_ps = ieee80211_update_ps;
71 vap->iv_set_tim = ieee80211_set_tim;
73 vap->iv_node_ps = ieee80211_node_pwrsave;
74 vap->iv_sta_ps = ieee80211_sta_pwrsave;
78 ieee80211_power_latevattach(struct ieee80211vap *vap)
81 * Allocate these only if needed. Beware that we
82 * know adhoc mode doesn't support ATIM yet...
84 if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
85 vap->iv_tim_len = howmany(vap->iv_max_aid,8) * sizeof(uint8_t);
86 vap->iv_tim_bitmap = (uint8_t *) IEEE80211_MALLOC(vap->iv_tim_len,
88 IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
89 if (vap->iv_tim_bitmap == NULL) {
90 printf("%s: no memory for TIM bitmap!\n", __func__);
91 /* XXX good enough to keep from crashing? */
98 ieee80211_power_vdetach(struct ieee80211vap *vap)
100 if (vap->iv_tim_bitmap != NULL) {
101 IEEE80211_FREE(vap->iv_tim_bitmap, M_80211_POWER);
102 vap->iv_tim_bitmap = NULL;
107 ieee80211_psq_init(struct ieee80211_psq *psq, const char *name)
109 memset(psq, 0, sizeof(*psq));
110 psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE;
111 IEEE80211_PSQ_INIT(psq, name); /* OS-dependent setup */
115 ieee80211_psq_cleanup(struct ieee80211_psq *psq)
118 psq_drain(psq); /* XXX should not be needed? */
120 KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len));
122 IEEE80211_PSQ_DESTROY(psq); /* OS-dependent cleanup */
126 * Return the highest priority frame in the ps queue.
129 ieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen)
131 struct ieee80211_psq *psq = &ni->ni_psq;
132 struct ieee80211_psq_head *qhead;
135 IEEE80211_PSQ_LOCK(psq);
136 qhead = &psq->psq_head[0];
138 if ((m = qhead->head) != NULL) {
139 if ((qhead->head = m->m_nextpkt) == NULL)
141 KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
143 KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
147 if (m == NULL && qhead == &psq->psq_head[0]) {
148 /* Algol-68 style for loop */
149 qhead = &psq->psq_head[1];
153 *qlen = psq->psq_len;
154 IEEE80211_PSQ_UNLOCK(psq);
159 * Reclaim an mbuf from the ps q. If marked with M_ENCAP
160 * we assume there is a node reference that must be relcaimed.
163 psq_mfree(struct mbuf *m)
165 if (m->m_flags & M_ENCAP) {
166 struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif;
167 ieee80211_free_node(ni);
174 * Clear any frames queued in the power save queue.
175 * The number of frames that were present is returned.
178 psq_drain(struct ieee80211_psq *psq)
180 struct ieee80211_psq_head *qhead;
184 IEEE80211_PSQ_LOCK(psq);
186 qhead = &psq->psq_head[0];
188 while ((m = qhead->head) != NULL) {
189 qhead->head = m->m_nextpkt;
194 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
195 qhead = &psq->psq_head[1];
199 IEEE80211_PSQ_UNLOCK(psq);
205 * Clear any frames queued in the power save queue.
206 * The number of frames that were present is returned.
209 ieee80211_node_psq_drain(struct ieee80211_node *ni)
211 return psq_drain(&ni->ni_psq);
215 * Age frames on the power save queue. The aging interval is
216 * 4 times the listen interval specified by the station. This
217 * number is factored into the age calculations when the frame
218 * is placed on the queue. We store ages as time differences
219 * so we can check and/or adjust only the head of the list.
220 * If a frame's age exceeds the threshold then discard it.
221 * The number of frames discarded is returned so the caller
222 * can check if it needs to adjust the tim.
225 ieee80211_node_psq_age(struct ieee80211_node *ni)
227 struct ieee80211_psq *psq = &ni->ni_psq;
230 if (psq->psq_len != 0) {
231 #ifdef IEEE80211_DEBUG
232 struct ieee80211vap *vap = ni->ni_vap;
234 struct ieee80211_psq_head *qhead;
237 IEEE80211_PSQ_LOCK(psq);
238 qhead = &psq->psq_head[0];
240 while ((m = qhead->head) != NULL &&
241 M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
242 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
243 "discard frame, age %u", M_AGE_GET(m));
244 if ((qhead->head = m->m_nextpkt) == NULL)
246 KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
248 KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
253 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
254 qhead = &psq->psq_head[1];
258 M_AGE_SUB(m, IEEE80211_INACT_WAIT);
259 IEEE80211_PSQ_UNLOCK(psq);
261 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
262 "discard %u frames for age", discard);
263 IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
269 * Handle a change in the PS station occupancy.
272 ieee80211_update_ps(struct ieee80211vap *vap, int nsta)
275 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
276 vap->iv_opmode == IEEE80211_M_IBSS,
277 ("operating mode %u", vap->iv_opmode));
281 * Indicate whether there are frames queued for a station in power-save mode.
284 ieee80211_set_tim(struct ieee80211_node *ni, int set)
286 struct ieee80211vap *vap = ni->ni_vap;
287 struct ieee80211com *ic = ni->ni_ic;
291 KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
292 vap->iv_opmode == IEEE80211_M_IBSS,
293 ("operating mode %u", vap->iv_opmode));
295 aid = IEEE80211_AID(ni->ni_associd);
296 KASSERT(aid < vap->iv_max_aid,
297 ("bogus aid %u, max %u", aid, vap->iv_max_aid));
300 changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0));
303 setbit(vap->iv_tim_bitmap, aid);
304 vap->iv_ps_pending++;
306 clrbit(vap->iv_tim_bitmap, aid);
307 vap->iv_ps_pending--;
309 /* NB: we know vap is in RUN state so no need to check */
310 vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM);
312 IEEE80211_UNLOCK(ic);
318 * Save an outbound packet for a node in power-save sleep state.
319 * The new packet is placed on the node's saved queue, and the TIM
320 * is changed, if necessary.
323 ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
325 struct ieee80211_psq *psq = &ni->ni_psq;
326 struct ieee80211vap *vap = ni->ni_vap;
327 struct ieee80211com *ic = ni->ni_ic;
328 struct ieee80211_psq_head *qhead;
331 IEEE80211_PSQ_LOCK(psq);
332 if (psq->psq_len >= psq->psq_maxlen) {
334 IEEE80211_PSQ_UNLOCK(psq);
335 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
336 "pwr save q overflow, drops %d (size %d)",
337 psq->psq_drops, psq->psq_len);
338 #ifdef IEEE80211_DEBUG
339 if (ieee80211_msg_dumppkts(vap))
340 ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t),
347 * Tag the frame with it's expiry time and insert it in
348 * the appropriate queue. The aging interval is 4 times
349 * the listen interval specified by the station. Frames
350 * that sit around too long are reclaimed using this
353 /* TU -> secs. XXX handle overflow? */
354 age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
356 * Encapsulated frames go on the high priority queue,
357 * other stuff goes on the low priority queue. We use
358 * this to order frames returned out of the driver
359 * ahead of frames we collect in ieee80211_start.
361 if (m->m_flags & M_ENCAP)
362 qhead = &psq->psq_head[0];
364 qhead = &psq->psq_head[1];
365 if (qhead->tail == NULL) {
370 * Take care to adjust age when inserting the first
371 * frame of a queue and the other queue already has
372 * frames. We need to preserve the age difference
373 * relationship so ieee80211_node_psq_age works.
375 if (qhead == &psq->psq_head[1]) {
376 mh = psq->psq_head[0].head;
380 mh = psq->psq_head[1].head;
382 int nage = M_AGE_GET(mh) - age;
383 /* XXX is clamping to zero good 'nuf? */
384 M_AGE_SET(mh, nage < 0 ? 0 : nage);
388 qhead->tail->m_nextpkt = m;
389 age -= M_AGE_GET(qhead->head);
391 KASSERT(age >= 0, ("age %d", age));
396 qlen = ++(psq->psq_len);
397 IEEE80211_PSQ_UNLOCK(psq);
399 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
400 "save frame with age %d, %u now queued", age, qlen);
402 if (qlen == 1 && vap->iv_set_tim != NULL)
403 vap->iv_set_tim(ni, 1);
409 * Move frames from the ps q to the vap's send queue
410 * and/or the driver's send queue; and kick the start
411 * method for each, as appropriate. Note we're careful
412 * to preserve packet ordering here.
415 pwrsave_flushq(struct ieee80211_node *ni)
417 struct ieee80211_psq *psq = &ni->ni_psq;
418 struct ieee80211com *ic = ni->ni_ic;
419 struct ieee80211vap *vap = ni->ni_vap;
420 struct ieee80211_psq_head *qhead;
421 struct mbuf *parent_q = NULL, *ifp_q = NULL;
424 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
425 "flush ps queue, %u packets queued", psq->psq_len);
427 IEEE80211_PSQ_LOCK(psq);
428 qhead = &psq->psq_head[0]; /* 802.11 frames */
429 if (qhead->head != NULL) {
430 /* XXX could dispatch through vap and check M_ENCAP */
431 /* XXX need different driver interface */
432 /* XXX bypasses q max and OACTIVE */
433 parent_q = qhead->head;
434 qhead->head = qhead->tail = NULL;
438 qhead = &psq->psq_head[1]; /* 802.3 frames */
439 if (qhead->head != NULL) {
440 /* XXX need different driver interface */
441 /* XXX bypasses q max and OACTIVE */
443 qhead->head = qhead->tail = NULL;
447 IEEE80211_PSQ_UNLOCK(psq);
449 /* NB: do this outside the psq lock */
450 /* XXX packets might get reordered if parent is OACTIVE */
451 /* parent frames, should be encapsulated */
452 while (parent_q != NULL) {
454 parent_q = m->m_nextpkt;
456 /* must be encapsulated */
457 KASSERT((m->m_flags & M_ENCAP),
458 ("%s: parentq with non-M_ENCAP frame!\n",
460 (void) ieee80211_parent_xmitpkt(ic, m);
463 /* VAP frames, aren't encapsulated */
464 while (ifp_q != NULL) {
466 ifp_q = m->m_nextpkt;
468 KASSERT((!(m->m_flags & M_ENCAP)),
469 ("%s: vapq with M_ENCAP frame!\n", __func__));
470 (void) ieee80211_vap_xmitpkt(vap, m);
475 * Handle station power-save state change.
478 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
480 struct ieee80211vap *vap = ni->ni_vap;
485 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
489 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
490 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
491 "power save mode on, %u sta's in ps mode", vap->iv_ps_sta);
494 vap->iv_update_ps(vap, vap->iv_ps_sta);
496 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
500 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
501 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
502 "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
504 /* NB: order here is intentional so TIM is clear before flush */
505 if (vap->iv_set_tim != NULL)
506 vap->iv_set_tim(ni, 0);
508 /* NB if no sta's in ps, driver should flush mc q */
509 vap->iv_update_ps(vap, vap->iv_ps_sta);
511 if (ni->ni_psq.psq_len != 0)
517 * Handle power-save state change in station mode.
520 ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
522 struct ieee80211_node *ni = vap->iv_bss;
524 if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
527 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
528 "sta power save mode %s", enable ? "on" : "off");
530 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
531 ieee80211_send_nulldata(ieee80211_ref_node(ni));
533 * Flush any queued frames; we can do this immediately
534 * because we know they'll be queued behind the null
535 * data frame we send the ap.
536 * XXX can we use a data frame to take us out of ps?
538 if (ni->ni_psq.psq_len != 0)
541 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
542 ieee80211_send_nulldata(ieee80211_ref_node(ni));
547 * Handle being notified that we have data available for us in a TIM/ATIM.
549 * This may schedule a transition from _SLEEP -> _RUN if it's appropriate.
551 * In STA mode, we may have put to sleep during scan and need to be dragged
552 * back out of powersave mode.
555 ieee80211_sta_tim_notify(struct ieee80211vap *vap, int set)
557 struct ieee80211com *ic = vap->iv_ic;
560 * Schedule the driver state change. It'll happen at some point soon.
561 * Since the hardware shouldn't know that we're running just yet
562 * (and thus tell the peer that we're awake before we actually wake
563 * up said hardware), we leave the actual node state transition
564 * up to the transition to RUN.
566 * XXX TODO: verify that the transition to RUN will wake up the
569 IEEE80211_LOCK(vap->iv_ic);
570 if (set == 1 && vap->iv_state == IEEE80211_S_SLEEP) {
571 ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
572 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
573 "%s: TIM=%d; wakeup\n", __func__, set);
574 } else if ((set == 1) && (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN)) {
576 * XXX only do this if we're in RUN state?
578 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
579 "%s: wake up from bgscan vap sleep\n",
582 * We may be in BGSCAN mode - this means the VAP is is in STA
583 * mode powersave. If it is, we need to wake it up so we
584 * can process outbound traffic.
586 vap->iv_sta_ps(vap, 0);
588 IEEE80211_UNLOCK(vap->iv_ic);
592 * Timer check on whether the VAP has had any transmit activity.
594 * This may schedule a transition from _RUN -> _SLEEP if it's appropriate.
597 ieee80211_sta_ps_timer_check(struct ieee80211vap *vap)
599 struct ieee80211com *ic = vap->iv_ic;
601 /* XXX lock assert */
603 /* For no, only do this in STA mode */
604 if (! (vap->iv_caps & IEEE80211_C_SWSLEEP))
607 if (vap->iv_opmode != IEEE80211_M_STA)
610 /* If we're not at run state, bail */
611 if (vap->iv_state != IEEE80211_S_RUN)
614 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
615 "%s: lastdata=%llu, ticks=%llu\n",
616 __func__, (unsigned long long) ic->ic_lastdata,
617 (unsigned long long) ticks);
619 /* If powersave is disabled on the VAP, don't bother */
620 if (! (vap->iv_flags & IEEE80211_F_PMGTON))
623 /* If we've done any data within our idle interval, bail */
624 /* XXX hard-coded to one second for now, ew! */
625 if (time_after(ic->ic_lastdata + 500, ticks))
629 * Signify we're going into power save and transition the
632 if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
633 vap->iv_sta_ps(vap, 1);
636 * XXX The driver has to handle the fact that we're going
637 * to sleep but frames may still be transmitted;
638 * hopefully it and/or us will do the right thing and mark any
639 * transmitted frames with PWRMGT set to 1.
641 ieee80211_new_state_locked(vap, IEEE80211_S_SLEEP, 0);
643 IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
644 "%s: time delta=%d msec\n", __func__,
645 (int) ticks_to_msecs(ticks - ic->ic_lastdata));