132176cfdSRui Paulo /*-
232176cfdSRui Paulo * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
332176cfdSRui Paulo * All rights reserved.
432176cfdSRui Paulo *
532176cfdSRui Paulo * Redistribution and use in source and binary forms, with or without
632176cfdSRui Paulo * modification, are permitted provided that the following conditions
732176cfdSRui Paulo * are met:
832176cfdSRui Paulo * 1. Redistributions of source code must retain the above copyright
932176cfdSRui Paulo * notice, this list of conditions and the following disclaimer.
1032176cfdSRui Paulo * 2. Redistributions in binary form must reproduce the above copyright
1132176cfdSRui Paulo * notice, this list of conditions and the following disclaimer in the
1232176cfdSRui Paulo * documentation and/or other materials provided with the distribution.
1332176cfdSRui Paulo *
1432176cfdSRui Paulo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1532176cfdSRui Paulo * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1632176cfdSRui Paulo * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1732176cfdSRui Paulo * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1832176cfdSRui Paulo * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1932176cfdSRui Paulo * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2032176cfdSRui Paulo * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2132176cfdSRui Paulo * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2232176cfdSRui Paulo * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2332176cfdSRui Paulo * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2432176cfdSRui Paulo */
2532176cfdSRui Paulo
26085ff963SMatthew Dillon #include <sys/cdefs.h>
27085ff963SMatthew Dillon __FBSDID("$FreeBSD$");
28085ff963SMatthew Dillon
2932176cfdSRui Paulo /*
3032176cfdSRui Paulo * IEEE 802.11 power save support.
3132176cfdSRui Paulo */
3232176cfdSRui Paulo #include "opt_wlan.h"
3332176cfdSRui Paulo
3432176cfdSRui Paulo #include <sys/param.h>
3532176cfdSRui Paulo #include <sys/systm.h>
3632176cfdSRui Paulo #include <sys/kernel.h>
37*4f655ef5SMatthew Dillon #include <sys/malloc.h>
3832176cfdSRui Paulo
3932176cfdSRui Paulo #include <sys/socket.h>
4032176cfdSRui Paulo
4132176cfdSRui Paulo #include <net/if.h>
42085ff963SMatthew Dillon #include <net/if_var.h>
4332176cfdSRui Paulo #include <net/if_media.h>
4432176cfdSRui Paulo #include <net/ethernet.h>
4532176cfdSRui Paulo
4632176cfdSRui Paulo #include <netproto/802_11/ieee80211_var.h>
4732176cfdSRui Paulo
4832176cfdSRui Paulo #include <net/bpf.h>
4932176cfdSRui Paulo
5032176cfdSRui Paulo static void ieee80211_update_ps(struct ieee80211vap *, int);
5132176cfdSRui Paulo static int ieee80211_set_tim(struct ieee80211_node *, int);
5232176cfdSRui Paulo
53085ff963SMatthew Dillon static MALLOC_DEFINE(M_80211_POWER, "80211power", "802.11 power save state");
5432176cfdSRui Paulo
5532176cfdSRui Paulo void
ieee80211_power_attach(struct ieee80211com * ic)5632176cfdSRui Paulo ieee80211_power_attach(struct ieee80211com *ic)
5732176cfdSRui Paulo {
5832176cfdSRui Paulo }
5932176cfdSRui Paulo
6032176cfdSRui Paulo void
ieee80211_power_detach(struct ieee80211com * ic)6132176cfdSRui Paulo ieee80211_power_detach(struct ieee80211com *ic)
6232176cfdSRui Paulo {
6332176cfdSRui Paulo }
6432176cfdSRui Paulo
6532176cfdSRui Paulo void
ieee80211_power_vattach(struct ieee80211vap * vap)6632176cfdSRui Paulo ieee80211_power_vattach(struct ieee80211vap *vap)
6732176cfdSRui Paulo {
6832176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
6932176cfdSRui Paulo vap->iv_opmode == IEEE80211_M_IBSS) {
7032176cfdSRui Paulo /* NB: driver should override */
7132176cfdSRui Paulo vap->iv_update_ps = ieee80211_update_ps;
7232176cfdSRui Paulo vap->iv_set_tim = ieee80211_set_tim;
7332176cfdSRui Paulo }
74d98a0bcfSMatthew Dillon vap->iv_node_ps = ieee80211_node_pwrsave;
75d98a0bcfSMatthew Dillon vap->iv_sta_ps = ieee80211_sta_pwrsave;
7632176cfdSRui Paulo }
7732176cfdSRui Paulo
7832176cfdSRui Paulo void
ieee80211_power_latevattach(struct ieee80211vap * vap)7932176cfdSRui Paulo ieee80211_power_latevattach(struct ieee80211vap *vap)
8032176cfdSRui Paulo {
8132176cfdSRui Paulo /*
8232176cfdSRui Paulo * Allocate these only if needed. Beware that we
8332176cfdSRui Paulo * know adhoc mode doesn't support ATIM yet...
8432176cfdSRui Paulo */
8532176cfdSRui Paulo if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
8632176cfdSRui Paulo vap->iv_tim_len = howmany(vap->iv_max_aid,8) * sizeof(uint8_t);
87*4f655ef5SMatthew Dillon #if defined(__DragonFly__)
8832176cfdSRui Paulo vap->iv_tim_bitmap = (uint8_t *) kmalloc(vap->iv_tim_len,
89fcaa651dSRui Paulo M_80211_POWER, M_INTWAIT | M_ZERO);
90*4f655ef5SMatthew Dillon #else
91*4f655ef5SMatthew Dillon vap->iv_tim_bitmap = (uint8_t *) IEEE80211_MALLOC(vap->iv_tim_len,
92*4f655ef5SMatthew Dillon M_80211_POWER,
93*4f655ef5SMatthew Dillon IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
94*4f655ef5SMatthew Dillon #endif
9532176cfdSRui Paulo if (vap->iv_tim_bitmap == NULL) {
9632176cfdSRui Paulo kprintf("%s: no memory for TIM bitmap!\n", __func__);
9732176cfdSRui Paulo /* XXX good enough to keep from crashing? */
9832176cfdSRui Paulo vap->iv_tim_len = 0;
9932176cfdSRui Paulo }
10032176cfdSRui Paulo }
10132176cfdSRui Paulo }
10232176cfdSRui Paulo
10332176cfdSRui Paulo void
ieee80211_power_vdetach(struct ieee80211vap * vap)10432176cfdSRui Paulo ieee80211_power_vdetach(struct ieee80211vap *vap)
10532176cfdSRui Paulo {
10632176cfdSRui Paulo if (vap->iv_tim_bitmap != NULL) {
107*4f655ef5SMatthew Dillon IEEE80211_FREE(vap->iv_tim_bitmap, M_80211_POWER);
10832176cfdSRui Paulo vap->iv_tim_bitmap = NULL;
10932176cfdSRui Paulo }
11032176cfdSRui Paulo }
11132176cfdSRui Paulo
11232176cfdSRui Paulo void
ieee80211_psq_init(struct ieee80211_psq * psq,const char * name)11332176cfdSRui Paulo ieee80211_psq_init(struct ieee80211_psq *psq, const char *name)
11432176cfdSRui Paulo {
1157c48796cSSascha Wildner memset(psq, 0, sizeof(*psq));
11632176cfdSRui Paulo psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE;
117085ff963SMatthew Dillon IEEE80211_PSQ_INIT(psq, name); /* OS-dependent setup */
11832176cfdSRui Paulo }
11932176cfdSRui Paulo
12032176cfdSRui Paulo void
ieee80211_psq_cleanup(struct ieee80211_psq * psq)12132176cfdSRui Paulo ieee80211_psq_cleanup(struct ieee80211_psq *psq)
12232176cfdSRui Paulo {
12332176cfdSRui Paulo #if 0
12432176cfdSRui Paulo psq_drain(psq); /* XXX should not be needed? */
12532176cfdSRui Paulo #else
12632176cfdSRui Paulo KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len));
12732176cfdSRui Paulo #endif
128085ff963SMatthew Dillon IEEE80211_PSQ_DESTROY(psq); /* OS-dependent cleanup */
12932176cfdSRui Paulo }
13032176cfdSRui Paulo
13132176cfdSRui Paulo /*
13232176cfdSRui Paulo * Return the highest priority frame in the ps queue.
13332176cfdSRui Paulo */
13432176cfdSRui Paulo struct mbuf *
ieee80211_node_psq_dequeue(struct ieee80211_node * ni,int * qlen)13532176cfdSRui Paulo ieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen)
13632176cfdSRui Paulo {
13732176cfdSRui Paulo struct ieee80211_psq *psq = &ni->ni_psq;
13832176cfdSRui Paulo struct ieee80211_psq_head *qhead;
13932176cfdSRui Paulo struct mbuf *m;
14032176cfdSRui Paulo
141085ff963SMatthew Dillon IEEE80211_PSQ_LOCK(psq);
14232176cfdSRui Paulo qhead = &psq->psq_head[0];
14332176cfdSRui Paulo again:
14432176cfdSRui Paulo if ((m = qhead->head) != NULL) {
14532176cfdSRui Paulo if ((qhead->head = m->m_nextpkt) == NULL)
14632176cfdSRui Paulo qhead->tail = NULL;
14732176cfdSRui Paulo KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
14832176cfdSRui Paulo qhead->len--;
14932176cfdSRui Paulo KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
15032176cfdSRui Paulo psq->psq_len--;
15132176cfdSRui Paulo m->m_nextpkt = NULL;
15232176cfdSRui Paulo }
15332176cfdSRui Paulo if (m == NULL && qhead == &psq->psq_head[0]) {
15432176cfdSRui Paulo /* Algol-68 style for loop */
15532176cfdSRui Paulo qhead = &psq->psq_head[1];
15632176cfdSRui Paulo goto again;
15732176cfdSRui Paulo }
15832176cfdSRui Paulo if (qlen != NULL)
15932176cfdSRui Paulo *qlen = psq->psq_len;
160085ff963SMatthew Dillon IEEE80211_PSQ_UNLOCK(psq);
16132176cfdSRui Paulo return m;
16232176cfdSRui Paulo }
16332176cfdSRui Paulo
16432176cfdSRui Paulo /*
16532176cfdSRui Paulo * Reclaim an mbuf from the ps q. If marked with M_ENCAP
16632176cfdSRui Paulo * we assume there is a node reference that must be relcaimed.
16732176cfdSRui Paulo */
16832176cfdSRui Paulo static void
psq_mfree(struct mbuf * m)16932176cfdSRui Paulo psq_mfree(struct mbuf *m)
17032176cfdSRui Paulo {
17132176cfdSRui Paulo if (m->m_flags & M_ENCAP) {
17232176cfdSRui Paulo struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif;
17332176cfdSRui Paulo ieee80211_free_node(ni);
17432176cfdSRui Paulo }
17532176cfdSRui Paulo m->m_nextpkt = NULL;
17632176cfdSRui Paulo m_freem(m);
17732176cfdSRui Paulo }
17832176cfdSRui Paulo
17932176cfdSRui Paulo /*
18032176cfdSRui Paulo * Clear any frames queued in the power save queue.
18132176cfdSRui Paulo * The number of frames that were present is returned.
18232176cfdSRui Paulo */
18332176cfdSRui Paulo static int
psq_drain(struct ieee80211_psq * psq)18432176cfdSRui Paulo psq_drain(struct ieee80211_psq *psq)
18532176cfdSRui Paulo {
18632176cfdSRui Paulo struct ieee80211_psq_head *qhead;
18732176cfdSRui Paulo struct mbuf *m;
18832176cfdSRui Paulo int qlen;
18932176cfdSRui Paulo
190085ff963SMatthew Dillon IEEE80211_PSQ_LOCK(psq);
19132176cfdSRui Paulo qlen = psq->psq_len;
19232176cfdSRui Paulo qhead = &psq->psq_head[0];
19332176cfdSRui Paulo again:
19432176cfdSRui Paulo while ((m = qhead->head) != NULL) {
19532176cfdSRui Paulo qhead->head = m->m_nextpkt;
19632176cfdSRui Paulo psq_mfree(m);
19732176cfdSRui Paulo }
19832176cfdSRui Paulo qhead->tail = NULL;
19932176cfdSRui Paulo qhead->len = 0;
20032176cfdSRui Paulo if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
20132176cfdSRui Paulo qhead = &psq->psq_head[1];
20232176cfdSRui Paulo goto again;
20332176cfdSRui Paulo }
20432176cfdSRui Paulo psq->psq_len = 0;
205085ff963SMatthew Dillon IEEE80211_PSQ_UNLOCK(psq);
20632176cfdSRui Paulo
20732176cfdSRui Paulo return qlen;
20832176cfdSRui Paulo }
20932176cfdSRui Paulo
21032176cfdSRui Paulo /*
21132176cfdSRui Paulo * Clear any frames queued in the power save queue.
21232176cfdSRui Paulo * The number of frames that were present is returned.
21332176cfdSRui Paulo */
21432176cfdSRui Paulo int
ieee80211_node_psq_drain(struct ieee80211_node * ni)21532176cfdSRui Paulo ieee80211_node_psq_drain(struct ieee80211_node *ni)
21632176cfdSRui Paulo {
21732176cfdSRui Paulo return psq_drain(&ni->ni_psq);
21832176cfdSRui Paulo }
21932176cfdSRui Paulo
22032176cfdSRui Paulo /*
22132176cfdSRui Paulo * Age frames on the power save queue. The aging interval is
22232176cfdSRui Paulo * 4 times the listen interval specified by the station. This
22332176cfdSRui Paulo * number is factored into the age calculations when the frame
22432176cfdSRui Paulo * is placed on the queue. We store ages as time differences
22532176cfdSRui Paulo * so we can check and/or adjust only the head of the list.
22632176cfdSRui Paulo * If a frame's age exceeds the threshold then discard it.
22732176cfdSRui Paulo * The number of frames discarded is returned so the caller
22832176cfdSRui Paulo * can check if it needs to adjust the tim.
22932176cfdSRui Paulo */
23032176cfdSRui Paulo int
ieee80211_node_psq_age(struct ieee80211_node * ni)23132176cfdSRui Paulo ieee80211_node_psq_age(struct ieee80211_node *ni)
23232176cfdSRui Paulo {
23332176cfdSRui Paulo struct ieee80211_psq *psq = &ni->ni_psq;
23432176cfdSRui Paulo int discard = 0;
23532176cfdSRui Paulo
23632176cfdSRui Paulo if (psq->psq_len != 0) {
23732176cfdSRui Paulo #ifdef IEEE80211_DEBUG
23832176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
23932176cfdSRui Paulo #endif
24032176cfdSRui Paulo struct ieee80211_psq_head *qhead;
24132176cfdSRui Paulo struct mbuf *m;
24232176cfdSRui Paulo
243085ff963SMatthew Dillon IEEE80211_PSQ_LOCK(psq);
24432176cfdSRui Paulo qhead = &psq->psq_head[0];
24532176cfdSRui Paulo again:
24632176cfdSRui Paulo while ((m = qhead->head) != NULL &&
24732176cfdSRui Paulo M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
24832176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
24932176cfdSRui Paulo "discard frame, age %u", M_AGE_GET(m));
25032176cfdSRui Paulo if ((qhead->head = m->m_nextpkt) == NULL)
25132176cfdSRui Paulo qhead->tail = NULL;
25232176cfdSRui Paulo KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
25332176cfdSRui Paulo qhead->len--;
25432176cfdSRui Paulo KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
25532176cfdSRui Paulo psq->psq_len--;
25632176cfdSRui Paulo psq_mfree(m);
25732176cfdSRui Paulo discard++;
25832176cfdSRui Paulo }
25932176cfdSRui Paulo if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
26032176cfdSRui Paulo qhead = &psq->psq_head[1];
26132176cfdSRui Paulo goto again;
26232176cfdSRui Paulo }
26332176cfdSRui Paulo if (m != NULL)
26432176cfdSRui Paulo M_AGE_SUB(m, IEEE80211_INACT_WAIT);
265085ff963SMatthew Dillon IEEE80211_PSQ_UNLOCK(psq);
26632176cfdSRui Paulo
26732176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
26832176cfdSRui Paulo "discard %u frames for age", discard);
26932176cfdSRui Paulo IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
27032176cfdSRui Paulo }
27132176cfdSRui Paulo return discard;
27232176cfdSRui Paulo }
27332176cfdSRui Paulo
27432176cfdSRui Paulo /*
27532176cfdSRui Paulo * Handle a change in the PS station occupancy.
27632176cfdSRui Paulo */
27732176cfdSRui Paulo static void
ieee80211_update_ps(struct ieee80211vap * vap,int nsta)27832176cfdSRui Paulo ieee80211_update_ps(struct ieee80211vap *vap, int nsta)
27932176cfdSRui Paulo {
28032176cfdSRui Paulo
28132176cfdSRui Paulo KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
28232176cfdSRui Paulo vap->iv_opmode == IEEE80211_M_IBSS,
28332176cfdSRui Paulo ("operating mode %u", vap->iv_opmode));
28432176cfdSRui Paulo }
28532176cfdSRui Paulo
28632176cfdSRui Paulo /*
28732176cfdSRui Paulo * Indicate whether there are frames queued for a station in power-save mode.
28832176cfdSRui Paulo */
28932176cfdSRui Paulo static int
ieee80211_set_tim(struct ieee80211_node * ni,int set)29032176cfdSRui Paulo ieee80211_set_tim(struct ieee80211_node *ni, int set)
29132176cfdSRui Paulo {
29232176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
293085ff963SMatthew Dillon struct ieee80211com *ic = ni->ni_ic;
29432176cfdSRui Paulo uint16_t aid;
29532176cfdSRui Paulo int changed;
29632176cfdSRui Paulo
29732176cfdSRui Paulo KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
29832176cfdSRui Paulo vap->iv_opmode == IEEE80211_M_IBSS,
29932176cfdSRui Paulo ("operating mode %u", vap->iv_opmode));
30032176cfdSRui Paulo
30132176cfdSRui Paulo aid = IEEE80211_AID(ni->ni_associd);
30232176cfdSRui Paulo KASSERT(aid < vap->iv_max_aid,
30332176cfdSRui Paulo ("bogus aid %u, max %u", aid, vap->iv_max_aid));
30432176cfdSRui Paulo
305085ff963SMatthew Dillon IEEE80211_LOCK(ic);
30632176cfdSRui Paulo changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0));
30732176cfdSRui Paulo if (changed) {
30832176cfdSRui Paulo if (set) {
30932176cfdSRui Paulo setbit(vap->iv_tim_bitmap, aid);
31032176cfdSRui Paulo vap->iv_ps_pending++;
31132176cfdSRui Paulo } else {
31232176cfdSRui Paulo clrbit(vap->iv_tim_bitmap, aid);
31332176cfdSRui Paulo vap->iv_ps_pending--;
31432176cfdSRui Paulo }
31532176cfdSRui Paulo /* NB: we know vap is in RUN state so no need to check */
31632176cfdSRui Paulo vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM);
31732176cfdSRui Paulo }
318085ff963SMatthew Dillon IEEE80211_UNLOCK(ic);
31932176cfdSRui Paulo
32032176cfdSRui Paulo return changed;
32132176cfdSRui Paulo }
32232176cfdSRui Paulo
32332176cfdSRui Paulo /*
32432176cfdSRui Paulo * Save an outbound packet for a node in power-save sleep state.
32532176cfdSRui Paulo * The new packet is placed on the node's saved queue, and the TIM
32632176cfdSRui Paulo * is changed, if necessary.
32732176cfdSRui Paulo */
32832176cfdSRui Paulo int
ieee80211_pwrsave(struct ieee80211_node * ni,struct mbuf * m)32932176cfdSRui Paulo ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
33032176cfdSRui Paulo {
33132176cfdSRui Paulo struct ieee80211_psq *psq = &ni->ni_psq;
33232176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
33332176cfdSRui Paulo struct ieee80211com *ic = ni->ni_ic;
33432176cfdSRui Paulo struct ieee80211_psq_head *qhead;
33532176cfdSRui Paulo int qlen, age;
33632176cfdSRui Paulo
337085ff963SMatthew Dillon IEEE80211_PSQ_LOCK(psq);
33832176cfdSRui Paulo if (psq->psq_len >= psq->psq_maxlen) {
33932176cfdSRui Paulo psq->psq_drops++;
340085ff963SMatthew Dillon IEEE80211_PSQ_UNLOCK(psq);
34132176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
34232176cfdSRui Paulo "pwr save q overflow, drops %d (size %d)",
34332176cfdSRui Paulo psq->psq_drops, psq->psq_len);
34432176cfdSRui Paulo #ifdef IEEE80211_DEBUG
34532176cfdSRui Paulo if (ieee80211_msg_dumppkts(vap))
34632176cfdSRui Paulo ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t),
34732176cfdSRui Paulo m->m_len, -1, -1);
34832176cfdSRui Paulo #endif
34932176cfdSRui Paulo psq_mfree(m);
35032176cfdSRui Paulo return ENOSPC;
35132176cfdSRui Paulo }
35232176cfdSRui Paulo /*
35332176cfdSRui Paulo * Tag the frame with it's expiry time and insert it in
35432176cfdSRui Paulo * the appropriate queue. The aging interval is 4 times
35532176cfdSRui Paulo * the listen interval specified by the station. Frames
35632176cfdSRui Paulo * that sit around too long are reclaimed using this
35732176cfdSRui Paulo * information.
35832176cfdSRui Paulo */
35932176cfdSRui Paulo /* TU -> secs. XXX handle overflow? */
36032176cfdSRui Paulo age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
36132176cfdSRui Paulo /*
36232176cfdSRui Paulo * Encapsulated frames go on the high priority queue,
36332176cfdSRui Paulo * other stuff goes on the low priority queue. We use
36432176cfdSRui Paulo * this to order frames returned out of the driver
36532176cfdSRui Paulo * ahead of frames we collect in ieee80211_start.
36632176cfdSRui Paulo */
36732176cfdSRui Paulo if (m->m_flags & M_ENCAP)
36832176cfdSRui Paulo qhead = &psq->psq_head[0];
36932176cfdSRui Paulo else
37032176cfdSRui Paulo qhead = &psq->psq_head[1];
37132176cfdSRui Paulo if (qhead->tail == NULL) {
37232176cfdSRui Paulo struct mbuf *mh;
37332176cfdSRui Paulo
37432176cfdSRui Paulo qhead->head = m;
37532176cfdSRui Paulo /*
37632176cfdSRui Paulo * Take care to adjust age when inserting the first
37732176cfdSRui Paulo * frame of a queue and the other queue already has
37832176cfdSRui Paulo * frames. We need to preserve the age difference
37932176cfdSRui Paulo * relationship so ieee80211_node_psq_age works.
38032176cfdSRui Paulo */
38132176cfdSRui Paulo if (qhead == &psq->psq_head[1]) {
38232176cfdSRui Paulo mh = psq->psq_head[0].head;
38332176cfdSRui Paulo if (mh != NULL)
38432176cfdSRui Paulo age-= M_AGE_GET(mh);
38532176cfdSRui Paulo } else {
38632176cfdSRui Paulo mh = psq->psq_head[1].head;
38732176cfdSRui Paulo if (mh != NULL) {
38832176cfdSRui Paulo int nage = M_AGE_GET(mh) - age;
38932176cfdSRui Paulo /* XXX is clamping to zero good 'nuf? */
39032176cfdSRui Paulo M_AGE_SET(mh, nage < 0 ? 0 : nage);
39132176cfdSRui Paulo }
39232176cfdSRui Paulo }
39332176cfdSRui Paulo } else {
39432176cfdSRui Paulo qhead->tail->m_nextpkt = m;
39532176cfdSRui Paulo age -= M_AGE_GET(qhead->head);
39632176cfdSRui Paulo }
39732176cfdSRui Paulo KASSERT(age >= 0, ("age %d", age));
39832176cfdSRui Paulo M_AGE_SET(m, age);
39932176cfdSRui Paulo m->m_nextpkt = NULL;
40032176cfdSRui Paulo qhead->tail = m;
40132176cfdSRui Paulo qhead->len++;
40232176cfdSRui Paulo qlen = ++(psq->psq_len);
403085ff963SMatthew Dillon IEEE80211_PSQ_UNLOCK(psq);
40432176cfdSRui Paulo
40532176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
40632176cfdSRui Paulo "save frame with age %d, %u now queued", age, qlen);
40732176cfdSRui Paulo
40832176cfdSRui Paulo if (qlen == 1 && vap->iv_set_tim != NULL)
40932176cfdSRui Paulo vap->iv_set_tim(ni, 1);
41032176cfdSRui Paulo
41132176cfdSRui Paulo return 0;
41232176cfdSRui Paulo }
41332176cfdSRui Paulo
41432176cfdSRui Paulo /*
41532176cfdSRui Paulo * Move frames from the ps q to the vap's send queue
41632176cfdSRui Paulo * and/or the driver's send queue; and kick the start
41732176cfdSRui Paulo * method for each, as appropriate. Note we're careful
41832176cfdSRui Paulo * to preserve packet ordering here.
41932176cfdSRui Paulo */
42032176cfdSRui Paulo static void
pwrsave_flushq(struct ieee80211_node * ni)42132176cfdSRui Paulo pwrsave_flushq(struct ieee80211_node *ni)
42232176cfdSRui Paulo {
42332176cfdSRui Paulo struct ieee80211_psq *psq = &ni->ni_psq;
424085ff963SMatthew Dillon struct ieee80211com *ic = ni->ni_ic;
42532176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
42632176cfdSRui Paulo struct ieee80211_psq_head *qhead;
427085ff963SMatthew Dillon struct mbuf *parent_q = NULL, *ifp_q = NULL;
428085ff963SMatthew Dillon struct mbuf *m;
42932176cfdSRui Paulo
43032176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
43132176cfdSRui Paulo "flush ps queue, %u packets queued", psq->psq_len);
43232176cfdSRui Paulo
433085ff963SMatthew Dillon IEEE80211_PSQ_LOCK(psq);
43432176cfdSRui Paulo qhead = &psq->psq_head[0]; /* 802.11 frames */
43532176cfdSRui Paulo if (qhead->head != NULL) {
43632176cfdSRui Paulo /* XXX could dispatch through vap and check M_ENCAP */
43732176cfdSRui Paulo /* XXX need different driver interface */
43832176cfdSRui Paulo /* XXX bypasses q max and OACTIVE */
439085ff963SMatthew Dillon parent_q = qhead->head;
44032176cfdSRui Paulo qhead->head = qhead->tail = NULL;
44132176cfdSRui Paulo qhead->len = 0;
442*4f655ef5SMatthew Dillon }
44332176cfdSRui Paulo
44432176cfdSRui Paulo qhead = &psq->psq_head[1]; /* 802.3 frames */
44532176cfdSRui Paulo if (qhead->head != NULL) {
44632176cfdSRui Paulo /* XXX need different driver interface */
44732176cfdSRui Paulo /* XXX bypasses q max and OACTIVE */
448085ff963SMatthew Dillon ifp_q = qhead->head;
44932176cfdSRui Paulo qhead->head = qhead->tail = NULL;
45032176cfdSRui Paulo qhead->len = 0;
451*4f655ef5SMatthew Dillon }
45232176cfdSRui Paulo psq->psq_len = 0;
453085ff963SMatthew Dillon IEEE80211_PSQ_UNLOCK(psq);
45432176cfdSRui Paulo
45532176cfdSRui Paulo /* NB: do this outside the psq lock */
45632176cfdSRui Paulo /* XXX packets might get reordered if parent is OACTIVE */
457085ff963SMatthew Dillon /* parent frames, should be encapsulated */
458085ff963SMatthew Dillon while (parent_q != NULL) {
459085ff963SMatthew Dillon m = parent_q;
460085ff963SMatthew Dillon parent_q = m->m_nextpkt;
461085ff963SMatthew Dillon m->m_nextpkt = NULL;
462085ff963SMatthew Dillon /* must be encapsulated */
463085ff963SMatthew Dillon KASSERT((m->m_flags & M_ENCAP),
464085ff963SMatthew Dillon ("%s: parentq with non-M_ENCAP frame!\n",
465085ff963SMatthew Dillon __func__));
466*4f655ef5SMatthew Dillon (void) ieee80211_parent_xmitpkt(ic, m);
467085ff963SMatthew Dillon }
468085ff963SMatthew Dillon
469085ff963SMatthew Dillon /* VAP frames, aren't encapsulated */
470085ff963SMatthew Dillon while (ifp_q != NULL) {
471085ff963SMatthew Dillon m = ifp_q;
472085ff963SMatthew Dillon ifp_q = m->m_nextpkt;
473085ff963SMatthew Dillon m->m_nextpkt = NULL;
474085ff963SMatthew Dillon KASSERT((!(m->m_flags & M_ENCAP)),
475085ff963SMatthew Dillon ("%s: vapq with M_ENCAP frame!\n", __func__));
476085ff963SMatthew Dillon (void) ieee80211_vap_xmitpkt(vap, m);
477085ff963SMatthew Dillon }
478085ff963SMatthew Dillon }
47932176cfdSRui Paulo
48032176cfdSRui Paulo /*
48132176cfdSRui Paulo * Handle station power-save state change.
48232176cfdSRui Paulo */
48332176cfdSRui Paulo void
ieee80211_node_pwrsave(struct ieee80211_node * ni,int enable)48432176cfdSRui Paulo ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
48532176cfdSRui Paulo {
48632176cfdSRui Paulo struct ieee80211vap *vap = ni->ni_vap;
48732176cfdSRui Paulo int update;
48832176cfdSRui Paulo
48932176cfdSRui Paulo update = 0;
49032176cfdSRui Paulo if (enable) {
49132176cfdSRui Paulo if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
49232176cfdSRui Paulo vap->iv_ps_sta++;
49332176cfdSRui Paulo update = 1;
49432176cfdSRui Paulo }
49532176cfdSRui Paulo ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
49632176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
49732176cfdSRui Paulo "power save mode on, %u sta's in ps mode", vap->iv_ps_sta);
49832176cfdSRui Paulo
49932176cfdSRui Paulo if (update)
50032176cfdSRui Paulo vap->iv_update_ps(vap, vap->iv_ps_sta);
50132176cfdSRui Paulo } else {
50232176cfdSRui Paulo if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
50332176cfdSRui Paulo vap->iv_ps_sta--;
50432176cfdSRui Paulo update = 1;
50532176cfdSRui Paulo }
50632176cfdSRui Paulo ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
50732176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
50832176cfdSRui Paulo "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
50932176cfdSRui Paulo
51032176cfdSRui Paulo /* NB: order here is intentional so TIM is clear before flush */
51132176cfdSRui Paulo if (vap->iv_set_tim != NULL)
51232176cfdSRui Paulo vap->iv_set_tim(ni, 0);
51332176cfdSRui Paulo if (update) {
51432176cfdSRui Paulo /* NB if no sta's in ps, driver should flush mc q */
51532176cfdSRui Paulo vap->iv_update_ps(vap, vap->iv_ps_sta);
51632176cfdSRui Paulo }
51732176cfdSRui Paulo if (ni->ni_psq.psq_len != 0)
51832176cfdSRui Paulo pwrsave_flushq(ni);
51932176cfdSRui Paulo }
52032176cfdSRui Paulo }
52132176cfdSRui Paulo
52232176cfdSRui Paulo /*
52332176cfdSRui Paulo * Handle power-save state change in station mode.
52432176cfdSRui Paulo */
52532176cfdSRui Paulo void
ieee80211_sta_pwrsave(struct ieee80211vap * vap,int enable)52632176cfdSRui Paulo ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
52732176cfdSRui Paulo {
52832176cfdSRui Paulo struct ieee80211_node *ni = vap->iv_bss;
52932176cfdSRui Paulo
53032176cfdSRui Paulo if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
53132176cfdSRui Paulo return;
53232176cfdSRui Paulo
53332176cfdSRui Paulo IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
53432176cfdSRui Paulo "sta power save mode %s", enable ? "on" : "off");
53532176cfdSRui Paulo if (!enable) {
53632176cfdSRui Paulo ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
53732176cfdSRui Paulo ieee80211_send_nulldata(ieee80211_ref_node(ni));
53832176cfdSRui Paulo /*
53932176cfdSRui Paulo * Flush any queued frames; we can do this immediately
54032176cfdSRui Paulo * because we know they'll be queued behind the null
54132176cfdSRui Paulo * data frame we send the ap.
54232176cfdSRui Paulo * XXX can we use a data frame to take us out of ps?
54332176cfdSRui Paulo */
54432176cfdSRui Paulo if (ni->ni_psq.psq_len != 0)
54532176cfdSRui Paulo pwrsave_flushq(ni);
54632176cfdSRui Paulo } else {
54732176cfdSRui Paulo ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
54832176cfdSRui Paulo ieee80211_send_nulldata(ieee80211_ref_node(ni));
54932176cfdSRui Paulo }
55032176cfdSRui Paulo }
551d98a0bcfSMatthew Dillon
552d98a0bcfSMatthew Dillon /*
553d98a0bcfSMatthew Dillon * Handle being notified that we have data available for us in a TIM/ATIM.
554d98a0bcfSMatthew Dillon *
555d98a0bcfSMatthew Dillon * This may schedule a transition from _SLEEP -> _RUN if it's appropriate.
5564f898719SImre Vadász *
5574f898719SImre Vadász * In STA mode, we may have put to sleep during scan and need to be dragged
5584f898719SImre Vadász * back out of powersave mode.
559d98a0bcfSMatthew Dillon */
560d98a0bcfSMatthew Dillon void
ieee80211_sta_tim_notify(struct ieee80211vap * vap,int set)561d98a0bcfSMatthew Dillon ieee80211_sta_tim_notify(struct ieee80211vap *vap, int set)
562d98a0bcfSMatthew Dillon {
5634f898719SImre Vadász struct ieee80211com *ic = vap->iv_ic;
5644f898719SImre Vadász
565d98a0bcfSMatthew Dillon /*
566d98a0bcfSMatthew Dillon * Schedule the driver state change. It'll happen at some point soon.
567d98a0bcfSMatthew Dillon * Since the hardware shouldn't know that we're running just yet
568d98a0bcfSMatthew Dillon * (and thus tell the peer that we're awake before we actually wake
569d98a0bcfSMatthew Dillon * up said hardware), we leave the actual node state transition
570d98a0bcfSMatthew Dillon * up to the transition to RUN.
571d98a0bcfSMatthew Dillon *
572d98a0bcfSMatthew Dillon * XXX TODO: verify that the transition to RUN will wake up the
573d98a0bcfSMatthew Dillon * BSS node!
574d98a0bcfSMatthew Dillon */
575085ff963SMatthew Dillon IEEE80211_LOCK(vap->iv_ic);
576d98a0bcfSMatthew Dillon if (set == 1 && vap->iv_state == IEEE80211_S_SLEEP) {
577d98a0bcfSMatthew Dillon ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
5784f898719SImre Vadász IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
5794f898719SImre Vadász "%s: TIM=%d; wakeup\n", __func__, set);
5804f898719SImre Vadász } else if ((set == 1) && (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN)) {
5814f898719SImre Vadász /*
5824f898719SImre Vadász * XXX only do this if we're in RUN state?
5834f898719SImre Vadász */
5844f898719SImre Vadász IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
5854f898719SImre Vadász "%s: wake up from bgscan vap sleep\n",
5864f898719SImre Vadász __func__);
5874f898719SImre Vadász /*
5884f898719SImre Vadász * We may be in BGSCAN mode - this means the VAP is is in STA
5894f898719SImre Vadász * mode powersave. If it is, we need to wake it up so we
5904f898719SImre Vadász * can process outbound traffic.
5914f898719SImre Vadász */
5924f898719SImre Vadász vap->iv_sta_ps(vap, 0);
593d98a0bcfSMatthew Dillon }
594085ff963SMatthew Dillon IEEE80211_UNLOCK(vap->iv_ic);
595d98a0bcfSMatthew Dillon }
596d98a0bcfSMatthew Dillon
597d98a0bcfSMatthew Dillon /*
598d98a0bcfSMatthew Dillon * Timer check on whether the VAP has had any transmit activity.
599d98a0bcfSMatthew Dillon *
600d98a0bcfSMatthew Dillon * This may schedule a transition from _RUN -> _SLEEP if it's appropriate.
601d98a0bcfSMatthew Dillon */
602d98a0bcfSMatthew Dillon void
ieee80211_sta_ps_timer_check(struct ieee80211vap * vap)603d98a0bcfSMatthew Dillon ieee80211_sta_ps_timer_check(struct ieee80211vap *vap)
604d98a0bcfSMatthew Dillon {
605d98a0bcfSMatthew Dillon struct ieee80211com *ic = vap->iv_ic;
606d98a0bcfSMatthew Dillon
607d98a0bcfSMatthew Dillon /* XXX lock assert */
608d98a0bcfSMatthew Dillon
609d98a0bcfSMatthew Dillon /* For no, only do this in STA mode */
610d98a0bcfSMatthew Dillon if (! (vap->iv_caps & IEEE80211_C_SWSLEEP))
611d98a0bcfSMatthew Dillon goto out;
612d98a0bcfSMatthew Dillon
613d98a0bcfSMatthew Dillon if (vap->iv_opmode != IEEE80211_M_STA)
614d98a0bcfSMatthew Dillon goto out;
615d98a0bcfSMatthew Dillon
616d98a0bcfSMatthew Dillon /* If we're not at run state, bail */
617d98a0bcfSMatthew Dillon if (vap->iv_state != IEEE80211_S_RUN)
618d98a0bcfSMatthew Dillon goto out;
619d98a0bcfSMatthew Dillon
620d98a0bcfSMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
621d98a0bcfSMatthew Dillon "%s: lastdata=%llu, ticks=%llu\n",
622d98a0bcfSMatthew Dillon __func__, (unsigned long long) ic->ic_lastdata,
623d98a0bcfSMatthew Dillon (unsigned long long) ticks);
624d98a0bcfSMatthew Dillon
625d98a0bcfSMatthew Dillon /* If powersave is disabled on the VAP, don't bother */
626d98a0bcfSMatthew Dillon if (! (vap->iv_flags & IEEE80211_F_PMGTON))
627d98a0bcfSMatthew Dillon goto out;
628d98a0bcfSMatthew Dillon
629d98a0bcfSMatthew Dillon /* If we've done any data within our idle interval, bail */
630d98a0bcfSMatthew Dillon /* XXX hard-coded to one second for now, ew! */
631*4f655ef5SMatthew Dillon if (ieee80211_time_after(ic->ic_lastdata + 500, ticks))
632d98a0bcfSMatthew Dillon goto out;
633d98a0bcfSMatthew Dillon
634d98a0bcfSMatthew Dillon /*
635d98a0bcfSMatthew Dillon * Signify we're going into power save and transition the
636d98a0bcfSMatthew Dillon * node to powersave.
637d98a0bcfSMatthew Dillon */
638d98a0bcfSMatthew Dillon if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
639d98a0bcfSMatthew Dillon vap->iv_sta_ps(vap, 1);
640d98a0bcfSMatthew Dillon
641d98a0bcfSMatthew Dillon /*
642d98a0bcfSMatthew Dillon * XXX The driver has to handle the fact that we're going
643d98a0bcfSMatthew Dillon * to sleep but frames may still be transmitted;
644d98a0bcfSMatthew Dillon * hopefully it and/or us will do the right thing and mark any
645d98a0bcfSMatthew Dillon * transmitted frames with PWRMGT set to 1.
646d98a0bcfSMatthew Dillon */
647d98a0bcfSMatthew Dillon ieee80211_new_state_locked(vap, IEEE80211_S_SLEEP, 0);
648d98a0bcfSMatthew Dillon
649d98a0bcfSMatthew Dillon IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
650d98a0bcfSMatthew Dillon "%s: time delta=%d msec\n", __func__,
651d98a0bcfSMatthew Dillon (int) ticks_to_msecs(ticks - ic->ic_lastdata));
652d98a0bcfSMatthew Dillon
653d98a0bcfSMatthew Dillon out:
654d98a0bcfSMatthew Dillon return;
655d98a0bcfSMatthew Dillon }
656