1170530Ssam/*- 2178354Ssam * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting 3170530Ssam * All rights reserved. 4170530Ssam * 5170530Ssam * Redistribution and use in source and binary forms, with or without 6170530Ssam * modification, are permitted provided that the following conditions 7170530Ssam * are met: 8170530Ssam * 1. Redistributions of source code must retain the above copyright 9170530Ssam * notice, this list of conditions and the following disclaimer. 10170530Ssam * 2. Redistributions in binary form must reproduce the above copyright 11170530Ssam * notice, this list of conditions and the following disclaimer in the 12170530Ssam * documentation and/or other materials provided with the distribution. 13170530Ssam * 14170530Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15170530Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 16170530Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 17170530Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 18170530Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 19170530Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 20170530Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 21170530Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22170530Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 23170530Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24170530Ssam */ 25170530Ssam 26170530Ssam#include <sys/cdefs.h> 27170530Ssam__FBSDID("$FreeBSD$"); 28170530Ssam 29170530Ssam/* 30170530Ssam * IEEE 802.11 power save support. 31170530Ssam */ 32178354Ssam#include "opt_wlan.h" 33178354Ssam 34170530Ssam#include <sys/param.h> 35170530Ssam#include <sys/systm.h> 36170530Ssam#include <sys/kernel.h> 37170530Ssam 38170530Ssam#include <sys/socket.h> 39170530Ssam 40170530Ssam#include <net/if.h> 41170530Ssam#include <net/if_media.h> 42170530Ssam#include <net/ethernet.h> 43170530Ssam 44170530Ssam#include <net80211/ieee80211_var.h> 45170530Ssam 46170530Ssam#include <net/bpf.h> 47170530Ssam 48178354Ssamstatic void ieee80211_update_ps(struct ieee80211vap *, int); 49178354Ssamstatic int ieee80211_set_tim(struct ieee80211_node *, int); 50170530Ssam 51249132Smavstatic MALLOC_DEFINE(M_80211_POWER, "80211power", "802.11 power save state"); 52178354Ssam 53170530Ssamvoid 54170530Ssamieee80211_power_attach(struct ieee80211com *ic) 55170530Ssam{ 56178354Ssam} 57178354Ssam 58178354Ssamvoid 59178354Ssamieee80211_power_detach(struct ieee80211com *ic) 60178354Ssam{ 61178354Ssam} 62178354Ssam 63178354Ssamvoid 64178354Ssamieee80211_power_vattach(struct ieee80211vap *vap) 65178354Ssam{ 66178354Ssam if (vap->iv_opmode == IEEE80211_M_HOSTAP || 67178354Ssam vap->iv_opmode == IEEE80211_M_IBSS) { 68170530Ssam /* NB: driver should override */ 69178354Ssam vap->iv_update_ps = ieee80211_update_ps; 70178354Ssam vap->iv_set_tim = ieee80211_set_tim; 71170530Ssam } 72170530Ssam} 73170530Ssam 74170530Ssamvoid 75178354Ssamieee80211_power_latevattach(struct ieee80211vap *vap) 76170530Ssam{ 77170530Ssam /* 78170530Ssam * Allocate these only if needed. Beware that we 79170530Ssam * know adhoc mode doesn't support ATIM yet... 80170530Ssam */ 81178354Ssam if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 82178354Ssam vap->iv_tim_len = howmany(vap->iv_max_aid,8) * sizeof(uint8_t); 83186302Ssam vap->iv_tim_bitmap = (uint8_t *) malloc(vap->iv_tim_len, 84178354Ssam M_80211_POWER, M_NOWAIT | M_ZERO); 85178354Ssam if (vap->iv_tim_bitmap == NULL) { 86170530Ssam printf("%s: no memory for TIM bitmap!\n", __func__); 87170530Ssam /* XXX good enough to keep from crashing? */ 88178354Ssam vap->iv_tim_len = 0; 89170530Ssam } 90170530Ssam } 91170530Ssam} 92170530Ssam 93170530Ssamvoid 94178354Ssamieee80211_power_vdetach(struct ieee80211vap *vap) 95170530Ssam{ 96178354Ssam if (vap->iv_tim_bitmap != NULL) { 97186302Ssam free(vap->iv_tim_bitmap, M_80211_POWER); 98178354Ssam vap->iv_tim_bitmap = NULL; 99170530Ssam } 100170530Ssam} 101170530Ssam 102184288Ssamvoid 103184288Ssamieee80211_psq_init(struct ieee80211_psq *psq, const char *name) 104184288Ssam{ 105223842Skevlo memset(psq, 0, sizeof(*psq)); 106184288Ssam psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE; 107184288Ssam IEEE80211_PSQ_INIT(psq, name); /* OS-dependent setup */ 108184288Ssam} 109184288Ssam 110184288Ssamvoid 111184288Ssamieee80211_psq_cleanup(struct ieee80211_psq *psq) 112184288Ssam{ 113184288Ssam#if 0 114184288Ssam psq_drain(psq); /* XXX should not be needed? */ 115184288Ssam#else 116184288Ssam KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len)); 117184288Ssam#endif 118184288Ssam IEEE80211_PSQ_DESTROY(psq); /* OS-dependent cleanup */ 119184288Ssam} 120184288Ssam 121170530Ssam/* 122184288Ssam * Return the highest priority frame in the ps queue. 123184288Ssam */ 124184288Ssamstruct mbuf * 125184288Ssamieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen) 126184288Ssam{ 127184288Ssam struct ieee80211_psq *psq = &ni->ni_psq; 128184288Ssam struct ieee80211_psq_head *qhead; 129184288Ssam struct mbuf *m; 130184288Ssam 131184288Ssam IEEE80211_PSQ_LOCK(psq); 132184288Ssam qhead = &psq->psq_head[0]; 133184288Ssamagain: 134184288Ssam if ((m = qhead->head) != NULL) { 135184288Ssam if ((qhead->head = m->m_nextpkt) == NULL) 136184288Ssam qhead->tail = NULL; 137184288Ssam KASSERT(qhead->len > 0, ("qhead len %d", qhead->len)); 138184288Ssam qhead->len--; 139184288Ssam KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len)); 140184288Ssam psq->psq_len--; 141184288Ssam m->m_nextpkt = NULL; 142184288Ssam } 143184288Ssam if (m == NULL && qhead == &psq->psq_head[0]) { 144184288Ssam /* Algol-68 style for loop */ 145184288Ssam qhead = &psq->psq_head[1]; 146184288Ssam goto again; 147184288Ssam } 148184288Ssam if (qlen != NULL) 149184288Ssam *qlen = psq->psq_len; 150184288Ssam IEEE80211_PSQ_UNLOCK(psq); 151184288Ssam return m; 152184288Ssam} 153184288Ssam 154184288Ssam/* 155184288Ssam * Reclaim an mbuf from the ps q. If marked with M_ENCAP 156184288Ssam * we assume there is a node reference that must be relcaimed. 157184288Ssam */ 158184288Ssamstatic void 159184288Ssampsq_mfree(struct mbuf *m) 160184288Ssam{ 161184288Ssam if (m->m_flags & M_ENCAP) { 162184288Ssam struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif; 163184288Ssam ieee80211_free_node(ni); 164184288Ssam } 165184288Ssam m->m_nextpkt = NULL; 166184288Ssam m_freem(m); 167184288Ssam} 168184288Ssam 169184288Ssam/* 170184288Ssam * Clear any frames queued in the power save queue. 171170530Ssam * The number of frames that were present is returned. 172170530Ssam */ 173184288Ssamstatic int 174184288Ssampsq_drain(struct ieee80211_psq *psq) 175170530Ssam{ 176184288Ssam struct ieee80211_psq_head *qhead; 177184288Ssam struct mbuf *m; 178170530Ssam int qlen; 179170530Ssam 180184288Ssam IEEE80211_PSQ_LOCK(psq); 181184288Ssam qlen = psq->psq_len; 182184288Ssam qhead = &psq->psq_head[0]; 183184288Ssamagain: 184184288Ssam while ((m = qhead->head) != NULL) { 185184288Ssam qhead->head = m->m_nextpkt; 186184288Ssam psq_mfree(m); 187184288Ssam } 188184288Ssam qhead->tail = NULL; 189184288Ssam qhead->len = 0; 190184288Ssam if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */ 191184288Ssam qhead = &psq->psq_head[1]; 192184288Ssam goto again; 193184288Ssam } 194184288Ssam psq->psq_len = 0; 195184288Ssam IEEE80211_PSQ_UNLOCK(psq); 196170530Ssam 197170530Ssam return qlen; 198170530Ssam} 199170530Ssam 200170530Ssam/* 201184288Ssam * Clear any frames queued in the power save queue. 202184288Ssam * The number of frames that were present is returned. 203184288Ssam */ 204184288Ssamint 205184288Ssamieee80211_node_psq_drain(struct ieee80211_node *ni) 206184288Ssam{ 207184288Ssam return psq_drain(&ni->ni_psq); 208184288Ssam} 209184288Ssam 210184288Ssam/* 211170530Ssam * Age frames on the power save queue. The aging interval is 212170530Ssam * 4 times the listen interval specified by the station. This 213170530Ssam * number is factored into the age calculations when the frame 214170530Ssam * is placed on the queue. We store ages as time differences 215170530Ssam * so we can check and/or adjust only the head of the list. 216170530Ssam * If a frame's age exceeds the threshold then discard it. 217170530Ssam * The number of frames discarded is returned so the caller 218170530Ssam * can check if it needs to adjust the tim. 219170530Ssam */ 220170530Ssamint 221184288Ssamieee80211_node_psq_age(struct ieee80211_node *ni) 222170530Ssam{ 223184288Ssam struct ieee80211_psq *psq = &ni->ni_psq; 224170530Ssam int discard = 0; 225170530Ssam 226184288Ssam if (psq->psq_len != 0) { 227178354Ssam#ifdef IEEE80211_DEBUG 228178354Ssam struct ieee80211vap *vap = ni->ni_vap; 229178354Ssam#endif 230184288Ssam struct ieee80211_psq_head *qhead; 231170530Ssam struct mbuf *m; 232170530Ssam 233184288Ssam IEEE80211_PSQ_LOCK(psq); 234184288Ssam qhead = &psq->psq_head[0]; 235184288Ssam again: 236184288Ssam while ((m = qhead->head) != NULL && 237184288Ssam M_AGE_GET(m) < IEEE80211_INACT_WAIT) { 238178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 239178354Ssam "discard frame, age %u", M_AGE_GET(m)); 240184288Ssam if ((qhead->head = m->m_nextpkt) == NULL) 241184288Ssam qhead->tail = NULL; 242184288Ssam KASSERT(qhead->len > 0, ("qhead len %d", qhead->len)); 243184288Ssam qhead->len--; 244184288Ssam KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len)); 245184288Ssam psq->psq_len--; 246184288Ssam psq_mfree(m); 247170530Ssam discard++; 248170530Ssam } 249184288Ssam if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */ 250184288Ssam qhead = &psq->psq_head[1]; 251184288Ssam goto again; 252184288Ssam } 253170530Ssam if (m != NULL) 254170530Ssam M_AGE_SUB(m, IEEE80211_INACT_WAIT); 255184288Ssam IEEE80211_PSQ_UNLOCK(psq); 256170530Ssam 257178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 258170530Ssam "discard %u frames for age", discard); 259170530Ssam IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard); 260170530Ssam } 261170530Ssam return discard; 262170530Ssam} 263170530Ssam 264170530Ssam/* 265178354Ssam * Handle a change in the PS station occupancy. 266178354Ssam */ 267178354Ssamstatic void 268178354Ssamieee80211_update_ps(struct ieee80211vap *vap, int nsta) 269178354Ssam{ 270178354Ssam 271178354Ssam KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP || 272178354Ssam vap->iv_opmode == IEEE80211_M_IBSS, 273178354Ssam ("operating mode %u", vap->iv_opmode)); 274178354Ssam} 275178354Ssam 276178354Ssam/* 277170530Ssam * Indicate whether there are frames queued for a station in power-save mode. 278170530Ssam */ 279178354Ssamstatic int 280170530Ssamieee80211_set_tim(struct ieee80211_node *ni, int set) 281170530Ssam{ 282178354Ssam struct ieee80211vap *vap = ni->ni_vap; 283170530Ssam struct ieee80211com *ic = ni->ni_ic; 284170530Ssam uint16_t aid; 285178354Ssam int changed; 286170530Ssam 287178354Ssam KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP || 288178354Ssam vap->iv_opmode == IEEE80211_M_IBSS, 289178354Ssam ("operating mode %u", vap->iv_opmode)); 290170530Ssam 291170530Ssam aid = IEEE80211_AID(ni->ni_associd); 292178354Ssam KASSERT(aid < vap->iv_max_aid, 293178354Ssam ("bogus aid %u, max %u", aid, vap->iv_max_aid)); 294170530Ssam 295178354Ssam IEEE80211_LOCK(ic); 296178354Ssam changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0)); 297178354Ssam if (changed) { 298170530Ssam if (set) { 299178354Ssam setbit(vap->iv_tim_bitmap, aid); 300178354Ssam vap->iv_ps_pending++; 301170530Ssam } else { 302178354Ssam clrbit(vap->iv_tim_bitmap, aid); 303178354Ssam vap->iv_ps_pending--; 304170530Ssam } 305178354Ssam /* NB: we know vap is in RUN state so no need to check */ 306178354Ssam vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM); 307170530Ssam } 308178354Ssam IEEE80211_UNLOCK(ic); 309178354Ssam 310178354Ssam return changed; 311170530Ssam} 312170530Ssam 313170530Ssam/* 314170530Ssam * Save an outbound packet for a node in power-save sleep state. 315170530Ssam * The new packet is placed on the node's saved queue, and the TIM 316170530Ssam * is changed, if necessary. 317170530Ssam */ 318184288Ssamint 319170530Ssamieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m) 320170530Ssam{ 321184288Ssam struct ieee80211_psq *psq = &ni->ni_psq; 322178354Ssam struct ieee80211vap *vap = ni->ni_vap; 323170530Ssam struct ieee80211com *ic = ni->ni_ic; 324184288Ssam struct ieee80211_psq_head *qhead; 325170530Ssam int qlen, age; 326170530Ssam 327184288Ssam IEEE80211_PSQ_LOCK(psq); 328184288Ssam if (psq->psq_len >= psq->psq_maxlen) { 329184288Ssam psq->psq_drops++; 330184288Ssam IEEE80211_PSQ_UNLOCK(psq); 331178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni, 332178354Ssam "pwr save q overflow, drops %d (size %d)", 333184288Ssam psq->psq_drops, psq->psq_len); 334170530Ssam#ifdef IEEE80211_DEBUG 335178354Ssam if (ieee80211_msg_dumppkts(vap)) 336178354Ssam ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t), 337178354Ssam m->m_len, -1, -1); 338170530Ssam#endif 339184288Ssam psq_mfree(m); 340184288Ssam return ENOSPC; 341170530Ssam } 342170530Ssam /* 343184288Ssam * Tag the frame with it's expiry time and insert it in 344184288Ssam * the appropriate queue. The aging interval is 4 times 345184288Ssam * the listen interval specified by the station. Frames 346184288Ssam * that sit around too long are reclaimed using this 347184288Ssam * information. 348170530Ssam */ 349170530Ssam /* TU -> secs. XXX handle overflow? */ 350170530Ssam age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000; 351184288Ssam /* 352184288Ssam * Encapsulated frames go on the high priority queue, 353184288Ssam * other stuff goes on the low priority queue. We use 354184288Ssam * this to order frames returned out of the driver 355184288Ssam * ahead of frames we collect in ieee80211_start. 356184288Ssam */ 357184288Ssam if (m->m_flags & M_ENCAP) 358184288Ssam qhead = &psq->psq_head[0]; 359184288Ssam else 360184288Ssam qhead = &psq->psq_head[1]; 361184288Ssam if (qhead->tail == NULL) { 362184288Ssam struct mbuf *mh; 363170530Ssam 364184288Ssam qhead->head = m; 365184288Ssam /* 366184288Ssam * Take care to adjust age when inserting the first 367184288Ssam * frame of a queue and the other queue already has 368184288Ssam * frames. We need to preserve the age difference 369184288Ssam * relationship so ieee80211_node_psq_age works. 370184288Ssam */ 371184288Ssam if (qhead == &psq->psq_head[1]) { 372184288Ssam mh = psq->psq_head[0].head; 373184288Ssam if (mh != NULL) 374184288Ssam age-= M_AGE_GET(mh); 375184288Ssam } else { 376184288Ssam mh = psq->psq_head[1].head; 377184288Ssam if (mh != NULL) { 378184288Ssam int nage = M_AGE_GET(mh) - age; 379184288Ssam /* XXX is clamping to zero good 'nuf? */ 380184288Ssam M_AGE_SET(mh, nage < 0 ? 0 : nage); 381184288Ssam } 382184288Ssam } 383184288Ssam } else { 384184288Ssam qhead->tail->m_nextpkt = m; 385184288Ssam age -= M_AGE_GET(qhead->head); 386184288Ssam } 387184288Ssam KASSERT(age >= 0, ("age %d", age)); 388184288Ssam M_AGE_SET(m, age); 389184288Ssam m->m_nextpkt = NULL; 390184288Ssam qhead->tail = m; 391184288Ssam qhead->len++; 392184288Ssam qlen = ++(psq->psq_len); 393184288Ssam IEEE80211_PSQ_UNLOCK(psq); 394184288Ssam 395178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 396178354Ssam "save frame with age %d, %u now queued", age, qlen); 397170530Ssam 398178354Ssam if (qlen == 1 && vap->iv_set_tim != NULL) 399178354Ssam vap->iv_set_tim(ni, 1); 400184288Ssam 401184288Ssam return 0; 402170530Ssam} 403170530Ssam 404170530Ssam/* 405184288Ssam * Move frames from the ps q to the vap's send queue 406184288Ssam * and/or the driver's send queue; and kick the start 407184288Ssam * method for each, as appropriate. Note we're careful 408184288Ssam * to preserve packet ordering here. 409170530Ssam */ 410178354Ssamstatic void 411178354Ssampwrsave_flushq(struct ieee80211_node *ni) 412170530Ssam{ 413184288Ssam struct ieee80211_psq *psq = &ni->ni_psq; 414184288Ssam struct ieee80211vap *vap = ni->ni_vap; 415184288Ssam struct ieee80211_psq_head *qhead; 416184288Ssam struct ifnet *parent, *ifp; 417170530Ssam 418184288Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 419184288Ssam "flush ps queue, %u packets queued", psq->psq_len); 420184288Ssam 421184288Ssam IEEE80211_PSQ_LOCK(psq); 422184288Ssam qhead = &psq->psq_head[0]; /* 802.11 frames */ 423184288Ssam if (qhead->head != NULL) { 424184288Ssam /* XXX could dispatch through vap and check M_ENCAP */ 425184288Ssam parent = vap->iv_ic->ic_ifp; 426170530Ssam /* XXX need different driver interface */ 427178354Ssam /* XXX bypasses q max and OACTIVE */ 428184288Ssam IF_PREPEND_LIST(&parent->if_snd, qhead->head, qhead->tail, 429184288Ssam qhead->len); 430184288Ssam qhead->head = qhead->tail = NULL; 431184288Ssam qhead->len = 0; 432184288Ssam } else 433184288Ssam parent = NULL; 434184288Ssam 435184288Ssam qhead = &psq->psq_head[1]; /* 802.3 frames */ 436184288Ssam if (qhead->head != NULL) { 437184288Ssam ifp = vap->iv_ifp; 438184288Ssam /* XXX need different driver interface */ 439184288Ssam /* XXX bypasses q max and OACTIVE */ 440184288Ssam IF_PREPEND_LIST(&ifp->if_snd, qhead->head, qhead->tail, 441184288Ssam qhead->len); 442184288Ssam qhead->head = qhead->tail = NULL; 443184288Ssam qhead->len = 0; 444184288Ssam } else 445184288Ssam ifp = NULL; 446184288Ssam psq->psq_len = 0; 447184288Ssam IEEE80211_PSQ_UNLOCK(psq); 448184288Ssam 449184288Ssam /* NB: do this outside the psq lock */ 450184288Ssam /* XXX packets might get reordered if parent is OACTIVE */ 451184288Ssam if (parent != NULL) 452184288Ssam if_start(parent); 453184288Ssam if (ifp != NULL) 454178354Ssam if_start(ifp); 455170530Ssam} 456170530Ssam 457170530Ssam/* 458178354Ssam * Handle station power-save state change. 459178354Ssam */ 460178354Ssamvoid 461178354Ssamieee80211_node_pwrsave(struct ieee80211_node *ni, int enable) 462178354Ssam{ 463178354Ssam struct ieee80211vap *vap = ni->ni_vap; 464178354Ssam int update; 465178354Ssam 466178354Ssam update = 0; 467178354Ssam if (enable) { 468178354Ssam if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) { 469178354Ssam vap->iv_ps_sta++; 470178354Ssam update = 1; 471178354Ssam } 472178354Ssam ni->ni_flags |= IEEE80211_NODE_PWR_MGT; 473178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 474178354Ssam "power save mode on, %u sta's in ps mode", vap->iv_ps_sta); 475178354Ssam 476178354Ssam if (update) 477178354Ssam vap->iv_update_ps(vap, vap->iv_ps_sta); 478178354Ssam } else { 479178354Ssam if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 480178354Ssam vap->iv_ps_sta--; 481178354Ssam update = 1; 482178354Ssam } 483178354Ssam ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 484178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 485178354Ssam "power save mode off, %u sta's in ps mode", vap->iv_ps_sta); 486178354Ssam 487178354Ssam /* NB: order here is intentional so TIM is clear before flush */ 488178354Ssam if (vap->iv_set_tim != NULL) 489178354Ssam vap->iv_set_tim(ni, 0); 490178354Ssam if (update) { 491178354Ssam /* NB if no sta's in ps, driver should flush mc q */ 492178354Ssam vap->iv_update_ps(vap, vap->iv_ps_sta); 493178354Ssam } 494184288Ssam if (ni->ni_psq.psq_len != 0) 495184288Ssam pwrsave_flushq(ni); 496178354Ssam } 497178354Ssam} 498178354Ssam 499178354Ssam/* 500170530Ssam * Handle power-save state change in station mode. 501170530Ssam */ 502170530Ssamvoid 503178354Ssamieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable) 504170530Ssam{ 505178354Ssam struct ieee80211_node *ni = vap->iv_bss; 506170530Ssam 507170530Ssam if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0))) 508170530Ssam return; 509170530Ssam 510178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 511170530Ssam "sta power save mode %s", enable ? "on" : "off"); 512170530Ssam if (!enable) { 513170530Ssam ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 514170530Ssam ieee80211_send_nulldata(ieee80211_ref_node(ni)); 515170530Ssam /* 516170530Ssam * Flush any queued frames; we can do this immediately 517170530Ssam * because we know they'll be queued behind the null 518170530Ssam * data frame we send the ap. 519170530Ssam * XXX can we use a data frame to take us out of ps? 520170530Ssam */ 521184288Ssam if (ni->ni_psq.psq_len != 0) 522178354Ssam pwrsave_flushq(ni); 523170530Ssam } else { 524170530Ssam ni->ni_flags |= IEEE80211_NODE_PWR_MGT; 525170530Ssam ieee80211_send_nulldata(ieee80211_ref_node(ni)); 526170530Ssam } 527170530Ssam} 528