#include <sys/cdefs.h>
#include "opt_wlan.h"
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/kernel.h>
#include <sys/malloc.h>
#include <sys/socket.h>
#include <net/if.h>
#include <net/if_var.h>
#include <net/if_media.h>
#include <net/ethernet.h>
#include <net80211/ieee80211_var.h>
#include <net/bpf.h>
static void ieee80211_update_ps(struct ieee80211vap *, int);
static int ieee80211_set_tim(struct ieee80211_node *, int);
static MALLOC_DEFINE(M_80211_POWER, "80211power", "802.11 power save state");
void
ieee80211_power_attach(struct ieee80211com *ic)
{
}
void
ieee80211_power_detach(struct ieee80211com *ic)
{
}
void
ieee80211_power_vattach(struct ieee80211vap *vap)
{
if (vap->iv_opmode == IEEE80211_M_HOSTAP ||
vap->iv_opmode == IEEE80211_M_IBSS) {
vap->iv_update_ps = ieee80211_update_ps;
vap->iv_set_tim = ieee80211_set_tim;
}
vap->iv_node_ps = ieee80211_node_pwrsave;
vap->iv_sta_ps = ieee80211_sta_pwrsave;
}
void
ieee80211_power_latevattach(struct ieee80211vap *vap)
{
if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
vap->iv_tim_len = howmany(vap->iv_max_aid,8) * sizeof(uint8_t);
vap->iv_tim_bitmap = (uint8_t *) IEEE80211_MALLOC(vap->iv_tim_len,
M_80211_POWER,
IEEE80211_M_NOWAIT | IEEE80211_M_ZERO);
if (vap->iv_tim_bitmap == NULL) {
net80211_vap_printf(vap,
"%s: no memory for TIM bitmap!\n", __func__);
vap->iv_tim_len = 0;
}
}
}
void
ieee80211_power_vdetach(struct ieee80211vap *vap)
{
if (vap->iv_tim_bitmap != NULL) {
IEEE80211_FREE(vap->iv_tim_bitmap, M_80211_POWER);
vap->iv_tim_bitmap = NULL;
}
}
void
ieee80211_psq_init(struct ieee80211_psq *psq, const char *name)
{
memset(psq, 0, sizeof(*psq));
psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE;
IEEE80211_PSQ_INIT(psq, name);
}
void
ieee80211_psq_cleanup(struct ieee80211_psq *psq)
{
#if 0
psq_drain(psq);
#else
KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len));
#endif
IEEE80211_PSQ_DESTROY(psq);
}
struct mbuf *
ieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen)
{
struct ieee80211_psq *psq = &ni->ni_psq;
struct ieee80211_psq_head *qhead;
struct mbuf *m;
IEEE80211_PSQ_LOCK(psq);
qhead = &psq->psq_head[0];
again:
if ((m = qhead->head) != NULL) {
if ((qhead->head = m->m_nextpkt) == NULL)
qhead->tail = NULL;
KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
qhead->len--;
KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
psq->psq_len--;
m->m_nextpkt = NULL;
}
if (m == NULL && qhead == &psq->psq_head[0]) {
qhead = &psq->psq_head[1];
goto again;
}
if (qlen != NULL)
*qlen = psq->psq_len;
IEEE80211_PSQ_UNLOCK(psq);
return m;
}
static void
psq_mfree(struct mbuf *m)
{
if (m->m_flags & M_ENCAP) {
struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif;
ieee80211_free_node(ni);
}
m->m_nextpkt = NULL;
m_freem(m);
}
static int
psq_drain(struct ieee80211_psq *psq)
{
struct ieee80211_psq_head *qhead;
struct mbuf *m;
int qlen;
IEEE80211_PSQ_LOCK(psq);
qlen = psq->psq_len;
qhead = &psq->psq_head[0];
again:
while ((m = qhead->head) != NULL) {
qhead->head = m->m_nextpkt;
psq_mfree(m);
}
qhead->tail = NULL;
qhead->len = 0;
if (qhead == &psq->psq_head[0]) {
qhead = &psq->psq_head[1];
goto again;
}
psq->psq_len = 0;
IEEE80211_PSQ_UNLOCK(psq);
return qlen;
}
int
ieee80211_node_psq_drain(struct ieee80211_node *ni)
{
return psq_drain(&ni->ni_psq);
}
int
ieee80211_node_psq_age(struct ieee80211_node *ni)
{
struct ieee80211_psq *psq = &ni->ni_psq;
int discard = 0;
if (psq->psq_len != 0) {
#ifdef IEEE80211_DEBUG
struct ieee80211vap *vap = ni->ni_vap;
#endif
struct ieee80211_psq_head *qhead;
struct mbuf *m;
IEEE80211_PSQ_LOCK(psq);
qhead = &psq->psq_head[0];
again:
while ((m = qhead->head) != NULL &&
M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"discard frame, age %u", M_AGE_GET(m));
if ((qhead->head = m->m_nextpkt) == NULL)
qhead->tail = NULL;
KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
qhead->len--;
KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
psq->psq_len--;
psq_mfree(m);
discard++;
}
if (qhead == &psq->psq_head[0]) {
qhead = &psq->psq_head[1];
goto again;
}
if (m != NULL)
M_AGE_SUB(m, IEEE80211_INACT_WAIT);
IEEE80211_PSQ_UNLOCK(psq);
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"discard %u frames for age", discard);
IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
}
return discard;
}
static void
ieee80211_update_ps(struct ieee80211vap *vap, int nsta)
{
KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
vap->iv_opmode == IEEE80211_M_IBSS,
("operating mode %u", vap->iv_opmode));
}
static int
ieee80211_set_tim(struct ieee80211_node *ni, int set)
{
struct ieee80211vap *vap = ni->ni_vap;
struct ieee80211com *ic = ni->ni_ic;
uint16_t aid;
int changed;
KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
vap->iv_opmode == IEEE80211_M_IBSS,
("operating mode %u", vap->iv_opmode));
aid = IEEE80211_AID(ni->ni_associd);
KASSERT(aid < vap->iv_max_aid,
("bogus aid %u, max %u", aid, vap->iv_max_aid));
IEEE80211_LOCK(ic);
changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0));
if (changed) {
if (set) {
setbit(vap->iv_tim_bitmap, aid);
vap->iv_ps_pending++;
} else {
clrbit(vap->iv_tim_bitmap, aid);
vap->iv_ps_pending--;
}
vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM);
}
IEEE80211_UNLOCK(ic);
return changed;
}
int
ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
{
struct ieee80211_psq *psq = &ni->ni_psq;
struct ieee80211vap *vap = ni->ni_vap;
struct ieee80211com *ic = ni->ni_ic;
struct ieee80211_psq_head *qhead;
int qlen, age;
IEEE80211_PSQ_LOCK(psq);
if (psq->psq_len >= psq->psq_maxlen) {
psq->psq_drops++;
IEEE80211_PSQ_UNLOCK(psq);
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"pwr save q overflow, drops %d (size %d)",
psq->psq_drops, psq->psq_len);
#ifdef IEEE80211_DEBUG
if (ieee80211_msg_dumppkts(vap))
ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t),
m->m_len, -1, -1);
#endif
psq_mfree(m);
return ENOSPC;
}
age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
if (m->m_flags & M_ENCAP)
qhead = &psq->psq_head[0];
else
qhead = &psq->psq_head[1];
if (qhead->tail == NULL) {
struct mbuf *mh;
qhead->head = m;
if (qhead == &psq->psq_head[1]) {
mh = psq->psq_head[0].head;
if (mh != NULL)
age-= M_AGE_GET(mh);
} else {
mh = psq->psq_head[1].head;
if (mh != NULL) {
int nage = M_AGE_GET(mh) - age;
M_AGE_SET(mh, nage < 0 ? 0 : nage);
}
}
} else {
qhead->tail->m_nextpkt = m;
age -= M_AGE_GET(qhead->head);
}
KASSERT(age >= 0, ("age %d", age));
M_AGE_SET(m, age);
m->m_nextpkt = NULL;
qhead->tail = m;
qhead->len++;
qlen = ++(psq->psq_len);
IEEE80211_PSQ_UNLOCK(psq);
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"save frame with age %d, %u now queued", age, qlen);
if (qlen == 1 && vap->iv_set_tim != NULL)
vap->iv_set_tim(ni, 1);
return 0;
}
static void
pwrsave_flushq(struct ieee80211_node *ni)
{
struct ieee80211_psq *psq = &ni->ni_psq;
struct ieee80211com *ic = ni->ni_ic;
struct ieee80211vap *vap = ni->ni_vap;
struct ieee80211_psq_head *qhead;
struct mbuf *parent_q = NULL, *ifp_q = NULL;
struct mbuf *m;
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"flush ps queue, %u packets queued", psq->psq_len);
IEEE80211_PSQ_LOCK(psq);
qhead = &psq->psq_head[0];
if (qhead->head != NULL) {
parent_q = qhead->head;
qhead->head = qhead->tail = NULL;
qhead->len = 0;
}
qhead = &psq->psq_head[1];
if (qhead->head != NULL) {
ifp_q = qhead->head;
qhead->head = qhead->tail = NULL;
qhead->len = 0;
}
psq->psq_len = 0;
IEEE80211_PSQ_UNLOCK(psq);
while (parent_q != NULL) {
m = parent_q;
parent_q = m->m_nextpkt;
m->m_nextpkt = NULL;
KASSERT((m->m_flags & M_ENCAP),
("%s: parentq with non-M_ENCAP frame!\n",
__func__));
(void) ieee80211_parent_xmitpkt(ic, m);
}
while (ifp_q != NULL) {
m = ifp_q;
ifp_q = m->m_nextpkt;
m->m_nextpkt = NULL;
KASSERT((!(m->m_flags & M_ENCAP)),
("%s: vapq with M_ENCAP frame!\n", __func__));
(void) ieee80211_vap_xmitpkt(vap, m);
}
}
void
ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
{
struct ieee80211vap *vap = ni->ni_vap;
int update;
update = 0;
if (enable) {
if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
vap->iv_ps_sta++;
update = 1;
}
ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"power save mode on, %u sta's in ps mode", vap->iv_ps_sta);
if (update)
vap->iv_update_ps(vap, vap->iv_ps_sta);
} else {
if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
vap->iv_ps_sta--;
update = 1;
}
ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
if (vap->iv_set_tim != NULL)
vap->iv_set_tim(ni, 0);
if (update) {
vap->iv_update_ps(vap, vap->iv_ps_sta);
}
if (ni->ni_psq.psq_len != 0)
pwrsave_flushq(ni);
}
}
void
ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
{
struct ieee80211_node *ni = vap->iv_bss;
if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
return;
IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
"sta power save mode %s", enable ? "on" : "off");
if (!enable) {
ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
ieee80211_send_nulldata(ieee80211_ref_node(ni));
if (ni->ni_psq.psq_len != 0)
pwrsave_flushq(ni);
} else {
ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
ieee80211_send_nulldata(ieee80211_ref_node(ni));
}
}
void
ieee80211_sta_tim_notify(struct ieee80211vap *vap, int set)
{
struct ieee80211com *ic = vap->iv_ic;
IEEE80211_LOCK(vap->iv_ic);
if (set == 1 && vap->iv_state == IEEE80211_S_SLEEP) {
ieee80211_new_state_locked(vap, IEEE80211_S_RUN, 0);
IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
"%s: TIM=%d; wakeup\n", __func__, set);
} else if ((set == 1) && (ic->ic_flags_ext & IEEE80211_FEXT_BGSCAN)) {
IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
"%s: wake up from bgscan vap sleep\n",
__func__);
vap->iv_sta_ps(vap, 0);
}
IEEE80211_UNLOCK(vap->iv_ic);
}
void
ieee80211_sta_ps_timer_check(struct ieee80211vap *vap)
{
struct ieee80211com *ic = vap->iv_ic;
if (! (vap->iv_caps & IEEE80211_C_SWSLEEP))
goto out;
if (vap->iv_opmode != IEEE80211_M_STA)
goto out;
if (vap->iv_state != IEEE80211_S_RUN)
goto out;
IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
"%s: lastdata=%llu, ticks=%llu\n",
__func__, (unsigned long long) ic->ic_lastdata,
(unsigned long long) ticks);
if (! (vap->iv_flags & IEEE80211_F_PMGTON))
goto out;
if (ieee80211_time_after(ic->ic_lastdata + 500, ticks))
goto out;
if ((vap->iv_bss->ni_flags & IEEE80211_NODE_PWR_MGT) == 0)
vap->iv_sta_ps(vap, 1);
ieee80211_new_state_locked(vap, IEEE80211_S_SLEEP, 0);
IEEE80211_DPRINTF(vap, IEEE80211_MSG_POWER,
"%s: time delta=%d msec\n", __func__,
(int) ticks_to_msecs(ticks - ic->ic_lastdata));
out:
return;
}