ieee80211_power.c revision 254082
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: head/sys/net80211/ieee80211_power.c 254082 2013-08-08 05:09:35Z adrian $"); 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 51227293Sedstatic 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 } 72241138Sadrian vap->iv_node_ps = ieee80211_node_pwrsave; 73241138Sadrian vap->iv_sta_ps = ieee80211_sta_pwrsave; 74170530Ssam} 75170530Ssam 76170530Ssamvoid 77178354Ssamieee80211_power_latevattach(struct ieee80211vap *vap) 78170530Ssam{ 79170530Ssam /* 80170530Ssam * Allocate these only if needed. Beware that we 81170530Ssam * know adhoc mode doesn't support ATIM yet... 82170530Ssam */ 83178354Ssam if (vap->iv_opmode == IEEE80211_M_HOSTAP) { 84178354Ssam vap->iv_tim_len = howmany(vap->iv_max_aid,8) * sizeof(uint8_t); 85186302Ssam vap->iv_tim_bitmap = (uint8_t *) malloc(vap->iv_tim_len, 86178354Ssam M_80211_POWER, M_NOWAIT | M_ZERO); 87178354Ssam if (vap->iv_tim_bitmap == NULL) { 88170530Ssam printf("%s: no memory for TIM bitmap!\n", __func__); 89170530Ssam /* XXX good enough to keep from crashing? */ 90178354Ssam vap->iv_tim_len = 0; 91170530Ssam } 92170530Ssam } 93170530Ssam} 94170530Ssam 95170530Ssamvoid 96178354Ssamieee80211_power_vdetach(struct ieee80211vap *vap) 97170530Ssam{ 98178354Ssam if (vap->iv_tim_bitmap != NULL) { 99186302Ssam free(vap->iv_tim_bitmap, M_80211_POWER); 100178354Ssam vap->iv_tim_bitmap = NULL; 101170530Ssam } 102170530Ssam} 103170530Ssam 104184288Ssamvoid 105184288Ssamieee80211_psq_init(struct ieee80211_psq *psq, const char *name) 106184288Ssam{ 107223842Skevlo memset(psq, 0, sizeof(*psq)); 108184288Ssam psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE; 109184288Ssam IEEE80211_PSQ_INIT(psq, name); /* OS-dependent setup */ 110184288Ssam} 111184288Ssam 112184288Ssamvoid 113184288Ssamieee80211_psq_cleanup(struct ieee80211_psq *psq) 114184288Ssam{ 115184288Ssam#if 0 116184288Ssam psq_drain(psq); /* XXX should not be needed? */ 117184288Ssam#else 118184288Ssam KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len)); 119184288Ssam#endif 120184288Ssam IEEE80211_PSQ_DESTROY(psq); /* OS-dependent cleanup */ 121184288Ssam} 122184288Ssam 123170530Ssam/* 124184288Ssam * Return the highest priority frame in the ps queue. 125184288Ssam */ 126184288Ssamstruct mbuf * 127184288Ssamieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen) 128184288Ssam{ 129184288Ssam struct ieee80211_psq *psq = &ni->ni_psq; 130184288Ssam struct ieee80211_psq_head *qhead; 131184288Ssam struct mbuf *m; 132184288Ssam 133184288Ssam IEEE80211_PSQ_LOCK(psq); 134184288Ssam qhead = &psq->psq_head[0]; 135184288Ssamagain: 136184288Ssam if ((m = qhead->head) != NULL) { 137184288Ssam if ((qhead->head = m->m_nextpkt) == NULL) 138184288Ssam qhead->tail = NULL; 139184288Ssam KASSERT(qhead->len > 0, ("qhead len %d", qhead->len)); 140184288Ssam qhead->len--; 141184288Ssam KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len)); 142184288Ssam psq->psq_len--; 143184288Ssam m->m_nextpkt = NULL; 144184288Ssam } 145184288Ssam if (m == NULL && qhead == &psq->psq_head[0]) { 146184288Ssam /* Algol-68 style for loop */ 147184288Ssam qhead = &psq->psq_head[1]; 148184288Ssam goto again; 149184288Ssam } 150184288Ssam if (qlen != NULL) 151184288Ssam *qlen = psq->psq_len; 152184288Ssam IEEE80211_PSQ_UNLOCK(psq); 153184288Ssam return m; 154184288Ssam} 155184288Ssam 156184288Ssam/* 157184288Ssam * Reclaim an mbuf from the ps q. If marked with M_ENCAP 158184288Ssam * we assume there is a node reference that must be relcaimed. 159184288Ssam */ 160184288Ssamstatic void 161184288Ssampsq_mfree(struct mbuf *m) 162184288Ssam{ 163184288Ssam if (m->m_flags & M_ENCAP) { 164184288Ssam struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif; 165184288Ssam ieee80211_free_node(ni); 166184288Ssam } 167184288Ssam m->m_nextpkt = NULL; 168184288Ssam m_freem(m); 169184288Ssam} 170184288Ssam 171184288Ssam/* 172184288Ssam * Clear any frames queued in the power save queue. 173170530Ssam * The number of frames that were present is returned. 174170530Ssam */ 175184288Ssamstatic int 176184288Ssampsq_drain(struct ieee80211_psq *psq) 177170530Ssam{ 178184288Ssam struct ieee80211_psq_head *qhead; 179184288Ssam struct mbuf *m; 180170530Ssam int qlen; 181170530Ssam 182184288Ssam IEEE80211_PSQ_LOCK(psq); 183184288Ssam qlen = psq->psq_len; 184184288Ssam qhead = &psq->psq_head[0]; 185184288Ssamagain: 186184288Ssam while ((m = qhead->head) != NULL) { 187184288Ssam qhead->head = m->m_nextpkt; 188184288Ssam psq_mfree(m); 189184288Ssam } 190184288Ssam qhead->tail = NULL; 191184288Ssam qhead->len = 0; 192184288Ssam if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */ 193184288Ssam qhead = &psq->psq_head[1]; 194184288Ssam goto again; 195184288Ssam } 196184288Ssam psq->psq_len = 0; 197184288Ssam IEEE80211_PSQ_UNLOCK(psq); 198170530Ssam 199170530Ssam return qlen; 200170530Ssam} 201170530Ssam 202170530Ssam/* 203184288Ssam * Clear any frames queued in the power save queue. 204184288Ssam * The number of frames that were present is returned. 205184288Ssam */ 206184288Ssamint 207184288Ssamieee80211_node_psq_drain(struct ieee80211_node *ni) 208184288Ssam{ 209184288Ssam return psq_drain(&ni->ni_psq); 210184288Ssam} 211184288Ssam 212184288Ssam/* 213170530Ssam * Age frames on the power save queue. The aging interval is 214170530Ssam * 4 times the listen interval specified by the station. This 215170530Ssam * number is factored into the age calculations when the frame 216170530Ssam * is placed on the queue. We store ages as time differences 217170530Ssam * so we can check and/or adjust only the head of the list. 218170530Ssam * If a frame's age exceeds the threshold then discard it. 219170530Ssam * The number of frames discarded is returned so the caller 220170530Ssam * can check if it needs to adjust the tim. 221170530Ssam */ 222170530Ssamint 223184288Ssamieee80211_node_psq_age(struct ieee80211_node *ni) 224170530Ssam{ 225184288Ssam struct ieee80211_psq *psq = &ni->ni_psq; 226170530Ssam int discard = 0; 227170530Ssam 228184288Ssam if (psq->psq_len != 0) { 229178354Ssam#ifdef IEEE80211_DEBUG 230178354Ssam struct ieee80211vap *vap = ni->ni_vap; 231178354Ssam#endif 232184288Ssam struct ieee80211_psq_head *qhead; 233170530Ssam struct mbuf *m; 234170530Ssam 235184288Ssam IEEE80211_PSQ_LOCK(psq); 236184288Ssam qhead = &psq->psq_head[0]; 237184288Ssam again: 238184288Ssam while ((m = qhead->head) != NULL && 239184288Ssam M_AGE_GET(m) < IEEE80211_INACT_WAIT) { 240178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 241178354Ssam "discard frame, age %u", M_AGE_GET(m)); 242184288Ssam if ((qhead->head = m->m_nextpkt) == NULL) 243184288Ssam qhead->tail = NULL; 244184288Ssam KASSERT(qhead->len > 0, ("qhead len %d", qhead->len)); 245184288Ssam qhead->len--; 246184288Ssam KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len)); 247184288Ssam psq->psq_len--; 248184288Ssam psq_mfree(m); 249170530Ssam discard++; 250170530Ssam } 251184288Ssam if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */ 252184288Ssam qhead = &psq->psq_head[1]; 253184288Ssam goto again; 254184288Ssam } 255170530Ssam if (m != NULL) 256170530Ssam M_AGE_SUB(m, IEEE80211_INACT_WAIT); 257184288Ssam IEEE80211_PSQ_UNLOCK(psq); 258170530Ssam 259178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 260170530Ssam "discard %u frames for age", discard); 261170530Ssam IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard); 262170530Ssam } 263170530Ssam return discard; 264170530Ssam} 265170530Ssam 266170530Ssam/* 267178354Ssam * Handle a change in the PS station occupancy. 268178354Ssam */ 269178354Ssamstatic void 270178354Ssamieee80211_update_ps(struct ieee80211vap *vap, int nsta) 271178354Ssam{ 272178354Ssam 273178354Ssam KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP || 274178354Ssam vap->iv_opmode == IEEE80211_M_IBSS, 275178354Ssam ("operating mode %u", vap->iv_opmode)); 276178354Ssam} 277178354Ssam 278178354Ssam/* 279170530Ssam * Indicate whether there are frames queued for a station in power-save mode. 280170530Ssam */ 281178354Ssamstatic int 282170530Ssamieee80211_set_tim(struct ieee80211_node *ni, int set) 283170530Ssam{ 284178354Ssam struct ieee80211vap *vap = ni->ni_vap; 285170530Ssam struct ieee80211com *ic = ni->ni_ic; 286170530Ssam uint16_t aid; 287178354Ssam int changed; 288170530Ssam 289178354Ssam KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP || 290178354Ssam vap->iv_opmode == IEEE80211_M_IBSS, 291178354Ssam ("operating mode %u", vap->iv_opmode)); 292170530Ssam 293170530Ssam aid = IEEE80211_AID(ni->ni_associd); 294178354Ssam KASSERT(aid < vap->iv_max_aid, 295178354Ssam ("bogus aid %u, max %u", aid, vap->iv_max_aid)); 296170530Ssam 297178354Ssam IEEE80211_LOCK(ic); 298178354Ssam changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0)); 299178354Ssam if (changed) { 300170530Ssam if (set) { 301178354Ssam setbit(vap->iv_tim_bitmap, aid); 302178354Ssam vap->iv_ps_pending++; 303170530Ssam } else { 304178354Ssam clrbit(vap->iv_tim_bitmap, aid); 305178354Ssam vap->iv_ps_pending--; 306170530Ssam } 307178354Ssam /* NB: we know vap is in RUN state so no need to check */ 308178354Ssam vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM); 309170530Ssam } 310178354Ssam IEEE80211_UNLOCK(ic); 311178354Ssam 312178354Ssam return changed; 313170530Ssam} 314170530Ssam 315170530Ssam/* 316170530Ssam * Save an outbound packet for a node in power-save sleep state. 317170530Ssam * The new packet is placed on the node's saved queue, and the TIM 318170530Ssam * is changed, if necessary. 319170530Ssam */ 320184288Ssamint 321170530Ssamieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m) 322170530Ssam{ 323184288Ssam struct ieee80211_psq *psq = &ni->ni_psq; 324178354Ssam struct ieee80211vap *vap = ni->ni_vap; 325170530Ssam struct ieee80211com *ic = ni->ni_ic; 326184288Ssam struct ieee80211_psq_head *qhead; 327170530Ssam int qlen, age; 328170530Ssam 329184288Ssam IEEE80211_PSQ_LOCK(psq); 330184288Ssam if (psq->psq_len >= psq->psq_maxlen) { 331184288Ssam psq->psq_drops++; 332184288Ssam IEEE80211_PSQ_UNLOCK(psq); 333178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni, 334178354Ssam "pwr save q overflow, drops %d (size %d)", 335184288Ssam psq->psq_drops, psq->psq_len); 336170530Ssam#ifdef IEEE80211_DEBUG 337178354Ssam if (ieee80211_msg_dumppkts(vap)) 338178354Ssam ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t), 339178354Ssam m->m_len, -1, -1); 340170530Ssam#endif 341184288Ssam psq_mfree(m); 342184288Ssam return ENOSPC; 343170530Ssam } 344170530Ssam /* 345184288Ssam * Tag the frame with it's expiry time and insert it in 346184288Ssam * the appropriate queue. The aging interval is 4 times 347184288Ssam * the listen interval specified by the station. Frames 348184288Ssam * that sit around too long are reclaimed using this 349184288Ssam * information. 350170530Ssam */ 351170530Ssam /* TU -> secs. XXX handle overflow? */ 352170530Ssam age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000; 353184288Ssam /* 354184288Ssam * Encapsulated frames go on the high priority queue, 355184288Ssam * other stuff goes on the low priority queue. We use 356184288Ssam * this to order frames returned out of the driver 357184288Ssam * ahead of frames we collect in ieee80211_start. 358184288Ssam */ 359184288Ssam if (m->m_flags & M_ENCAP) 360184288Ssam qhead = &psq->psq_head[0]; 361184288Ssam else 362184288Ssam qhead = &psq->psq_head[1]; 363184288Ssam if (qhead->tail == NULL) { 364184288Ssam struct mbuf *mh; 365170530Ssam 366184288Ssam qhead->head = m; 367184288Ssam /* 368184288Ssam * Take care to adjust age when inserting the first 369184288Ssam * frame of a queue and the other queue already has 370184288Ssam * frames. We need to preserve the age difference 371184288Ssam * relationship so ieee80211_node_psq_age works. 372184288Ssam */ 373184288Ssam if (qhead == &psq->psq_head[1]) { 374184288Ssam mh = psq->psq_head[0].head; 375184288Ssam if (mh != NULL) 376184288Ssam age-= M_AGE_GET(mh); 377184288Ssam } else { 378184288Ssam mh = psq->psq_head[1].head; 379184288Ssam if (mh != NULL) { 380184288Ssam int nage = M_AGE_GET(mh) - age; 381184288Ssam /* XXX is clamping to zero good 'nuf? */ 382184288Ssam M_AGE_SET(mh, nage < 0 ? 0 : nage); 383184288Ssam } 384184288Ssam } 385184288Ssam } else { 386184288Ssam qhead->tail->m_nextpkt = m; 387184288Ssam age -= M_AGE_GET(qhead->head); 388184288Ssam } 389184288Ssam KASSERT(age >= 0, ("age %d", age)); 390184288Ssam M_AGE_SET(m, age); 391184288Ssam m->m_nextpkt = NULL; 392184288Ssam qhead->tail = m; 393184288Ssam qhead->len++; 394184288Ssam qlen = ++(psq->psq_len); 395184288Ssam IEEE80211_PSQ_UNLOCK(psq); 396184288Ssam 397178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 398178354Ssam "save frame with age %d, %u now queued", age, qlen); 399170530Ssam 400178354Ssam if (qlen == 1 && vap->iv_set_tim != NULL) 401178354Ssam vap->iv_set_tim(ni, 1); 402184288Ssam 403184288Ssam return 0; 404170530Ssam} 405170530Ssam 406170530Ssam/* 407184288Ssam * Move frames from the ps q to the vap's send queue 408184288Ssam * and/or the driver's send queue; and kick the start 409184288Ssam * method for each, as appropriate. Note we're careful 410184288Ssam * to preserve packet ordering here. 411170530Ssam */ 412178354Ssamstatic void 413178354Ssampwrsave_flushq(struct ieee80211_node *ni) 414170530Ssam{ 415184288Ssam struct ieee80211_psq *psq = &ni->ni_psq; 416248069Sadrian struct ieee80211com *ic = ni->ni_ic; 417184288Ssam struct ieee80211vap *vap = ni->ni_vap; 418184288Ssam struct ieee80211_psq_head *qhead; 419184288Ssam struct ifnet *parent, *ifp; 420245464Sadrian struct mbuf *parent_q = NULL, *ifp_q = NULL; 421245464Sadrian struct mbuf *m; 422170530Ssam 423184288Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 424184288Ssam "flush ps queue, %u packets queued", psq->psq_len); 425184288Ssam 426184288Ssam IEEE80211_PSQ_LOCK(psq); 427184288Ssam qhead = &psq->psq_head[0]; /* 802.11 frames */ 428184288Ssam if (qhead->head != NULL) { 429184288Ssam /* XXX could dispatch through vap and check M_ENCAP */ 430184288Ssam parent = vap->iv_ic->ic_ifp; 431170530Ssam /* XXX need different driver interface */ 432178354Ssam /* XXX bypasses q max and OACTIVE */ 433245464Sadrian parent_q = qhead->head; 434184288Ssam qhead->head = qhead->tail = NULL; 435184288Ssam qhead->len = 0; 436184288Ssam } else 437184288Ssam parent = NULL; 438184288Ssam 439184288Ssam qhead = &psq->psq_head[1]; /* 802.3 frames */ 440184288Ssam if (qhead->head != NULL) { 441184288Ssam ifp = vap->iv_ifp; 442184288Ssam /* XXX need different driver interface */ 443184288Ssam /* XXX bypasses q max and OACTIVE */ 444245464Sadrian ifp_q = qhead->head; 445184288Ssam qhead->head = qhead->tail = NULL; 446184288Ssam qhead->len = 0; 447184288Ssam } else 448184288Ssam ifp = NULL; 449184288Ssam psq->psq_len = 0; 450184288Ssam IEEE80211_PSQ_UNLOCK(psq); 451184288Ssam 452184288Ssam /* NB: do this outside the psq lock */ 453184288Ssam /* XXX packets might get reordered if parent is OACTIVE */ 454245464Sadrian /* parent frames, should be encapsulated */ 455245464Sadrian if (parent != NULL) { 456245464Sadrian while (parent_q != NULL) { 457245464Sadrian m = parent_q; 458245464Sadrian parent_q = m->m_nextpkt; 459245464Sadrian /* must be encapsulated */ 460245464Sadrian KASSERT((m->m_flags & M_ENCAP), 461245464Sadrian ("%s: parentq with non-M_ENCAP frame!\n", 462245464Sadrian __func__)); 463245464Sadrian /* 464245464Sadrian * For encaped frames, we need to free the node 465245464Sadrian * reference upon failure. 466245464Sadrian */ 467254082Sadrian if (ieee80211_parent_xmitpkt(ic, m) != 0) 468245464Sadrian ieee80211_free_node(ni); 469245464Sadrian } 470245464Sadrian } 471245464Sadrian 472245464Sadrian /* VAP frames, aren't encapsulated */ 473245464Sadrian if (ifp != NULL) { 474245464Sadrian while (ifp_q != NULL) { 475245464Sadrian m = ifp_q; 476245464Sadrian ifp_q = m->m_nextpkt; 477245464Sadrian KASSERT((!(m->m_flags & M_ENCAP)), 478245464Sadrian ("%s: vapq with M_ENCAP frame!\n", __func__)); 479254082Sadrian (void) ieee80211_vap_xmitpkt(vap, m); 480245464Sadrian } 481245464Sadrian } 482170530Ssam} 483170530Ssam 484170530Ssam/* 485178354Ssam * Handle station power-save state change. 486178354Ssam */ 487178354Ssamvoid 488178354Ssamieee80211_node_pwrsave(struct ieee80211_node *ni, int enable) 489178354Ssam{ 490178354Ssam struct ieee80211vap *vap = ni->ni_vap; 491178354Ssam int update; 492178354Ssam 493178354Ssam update = 0; 494178354Ssam if (enable) { 495178354Ssam if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) { 496178354Ssam vap->iv_ps_sta++; 497178354Ssam update = 1; 498178354Ssam } 499178354Ssam ni->ni_flags |= IEEE80211_NODE_PWR_MGT; 500178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 501178354Ssam "power save mode on, %u sta's in ps mode", vap->iv_ps_sta); 502178354Ssam 503178354Ssam if (update) 504178354Ssam vap->iv_update_ps(vap, vap->iv_ps_sta); 505178354Ssam } else { 506178354Ssam if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) { 507178354Ssam vap->iv_ps_sta--; 508178354Ssam update = 1; 509178354Ssam } 510178354Ssam ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 511178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 512178354Ssam "power save mode off, %u sta's in ps mode", vap->iv_ps_sta); 513178354Ssam 514178354Ssam /* NB: order here is intentional so TIM is clear before flush */ 515178354Ssam if (vap->iv_set_tim != NULL) 516178354Ssam vap->iv_set_tim(ni, 0); 517178354Ssam if (update) { 518178354Ssam /* NB if no sta's in ps, driver should flush mc q */ 519178354Ssam vap->iv_update_ps(vap, vap->iv_ps_sta); 520178354Ssam } 521184288Ssam if (ni->ni_psq.psq_len != 0) 522184288Ssam pwrsave_flushq(ni); 523178354Ssam } 524178354Ssam} 525178354Ssam 526178354Ssam/* 527170530Ssam * Handle power-save state change in station mode. 528170530Ssam */ 529170530Ssamvoid 530178354Ssamieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable) 531170530Ssam{ 532178354Ssam struct ieee80211_node *ni = vap->iv_bss; 533170530Ssam 534170530Ssam if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0))) 535170530Ssam return; 536170530Ssam 537178354Ssam IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni, 538170530Ssam "sta power save mode %s", enable ? "on" : "off"); 539170530Ssam if (!enable) { 540170530Ssam ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT; 541170530Ssam ieee80211_send_nulldata(ieee80211_ref_node(ni)); 542170530Ssam /* 543170530Ssam * Flush any queued frames; we can do this immediately 544170530Ssam * because we know they'll be queued behind the null 545170530Ssam * data frame we send the ap. 546170530Ssam * XXX can we use a data frame to take us out of ps? 547170530Ssam */ 548184288Ssam if (ni->ni_psq.psq_len != 0) 549178354Ssam pwrsave_flushq(ni); 550170530Ssam } else { 551170530Ssam ni->ni_flags |= IEEE80211_NODE_PWR_MGT; 552170530Ssam ieee80211_send_nulldata(ieee80211_ref_node(ni)); 553170530Ssam } 554170530Ssam} 555