Merge branch 'master' into net80211-update
[dragonfly.git] / sys / netproto / 802_11 / wlan / ieee80211_power.c
1 /*-
2  * Copyright (c) 2002-2008 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
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.
13  *
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.
24  *
25  * $FreeBSD: head/sys/net80211/ieee80211_power.c 186302 2008-12-18 23:00:09Z sam $
26  * $DragonFly$
27  */
28
29 /*
30  * IEEE 802.11 power save support.
31  */
32 #include "opt_wlan.h"
33
34 #include <sys/param.h>
35 #include <sys/systm.h> 
36 #include <sys/kernel.h>
37  
38 #include <sys/socket.h>
39
40 #include <net/if.h>
41 #include <net/if_media.h>
42 #include <net/ethernet.h>
43 #include <net/route.h>
44
45 #include <netproto/802_11/ieee80211_var.h>
46
47 #include <net/bpf.h>
48
49 static void ieee80211_update_ps(struct ieee80211vap *, int);
50 static int ieee80211_set_tim(struct ieee80211_node *, int);
51
52 MALLOC_DEFINE(M_80211_POWER, "80211power", "802.11 power save state");
53
54 void
55 ieee80211_power_attach(struct ieee80211com *ic)
56 {
57 }
58
59 void
60 ieee80211_power_detach(struct ieee80211com *ic)
61 {
62 }
63
64 void
65 ieee80211_power_vattach(struct ieee80211vap *vap)
66 {
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;
72         }
73 }
74
75 void
76 ieee80211_power_latevattach(struct ieee80211vap *vap)
77 {
78         /*
79          * Allocate these only if needed.  Beware that we
80          * know adhoc mode doesn't support ATIM yet...
81          */
82         if (vap->iv_opmode == IEEE80211_M_HOSTAP) {
83                 vap->iv_tim_len = howmany(vap->iv_max_aid,8) * sizeof(uint8_t);
84                 vap->iv_tim_bitmap = (uint8_t *) kmalloc(vap->iv_tim_len,
85                         M_80211_POWER, M_INTWAIT | M_ZERO);
86                 if (vap->iv_tim_bitmap == NULL) {
87                         kprintf("%s: no memory for TIM bitmap!\n", __func__);
88                         /* XXX good enough to keep from crashing? */
89                         vap->iv_tim_len = 0;
90                 }
91         }
92 }
93
94 void
95 ieee80211_power_vdetach(struct ieee80211vap *vap)
96 {
97         if (vap->iv_tim_bitmap != NULL) {
98                 kfree(vap->iv_tim_bitmap, M_80211_POWER);
99                 vap->iv_tim_bitmap = NULL;
100         }
101 }
102
103 void
104 ieee80211_psq_init(struct ieee80211_psq *psq, const char *name)
105 {
106         memset(psq, 0, sizeof(psq));
107         psq->psq_maxlen = IEEE80211_PS_MAX_QUEUE;
108         IEEE80211_PSQ_INIT(psq, name);          /* OS-dependent setup */
109 }
110
111 void
112 ieee80211_psq_cleanup(struct ieee80211_psq *psq)
113 {
114 #if 0
115         psq_drain(psq);                         /* XXX should not be needed? */
116 #else
117         KASSERT(psq->psq_len == 0, ("%d frames on ps q", psq->psq_len));
118 #endif
119         IEEE80211_PSQ_DESTROY(psq);             /* OS-dependent cleanup */
120 }
121
122 /*
123  * Return the highest priority frame in the ps queue.
124  */
125 struct mbuf *
126 ieee80211_node_psq_dequeue(struct ieee80211_node *ni, int *qlen)
127 {
128         struct ieee80211_psq *psq = &ni->ni_psq;
129         struct ieee80211_psq_head *qhead;
130         struct mbuf *m;
131
132         IEEE80211_PSQ_LOCK(psq);
133         qhead = &psq->psq_head[0];
134 again:
135         if ((m = qhead->head) != NULL) {
136                 if ((qhead->head = m->m_nextpkt) == NULL)
137                         qhead->tail = NULL;
138                 KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
139                 qhead->len--;
140                 KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
141                 psq->psq_len--;
142                 m->m_nextpkt = NULL;
143         }
144         if (m == NULL && qhead == &psq->psq_head[0]) {
145                 /* Algol-68 style for loop */
146                 qhead = &psq->psq_head[1];
147                 goto again;
148         }
149         if (qlen != NULL)
150                 *qlen = psq->psq_len;
151         IEEE80211_PSQ_UNLOCK(psq);
152         return m;
153 }
154
155 /*
156  * Reclaim an mbuf from the ps q.  If marked with M_ENCAP
157  * we assume there is a node reference that must be relcaimed.
158  */
159 static void
160 psq_mfree(struct mbuf *m)
161 {
162         if (m->m_flags & M_ENCAP) {
163                 struct ieee80211_node *ni = (void *) m->m_pkthdr.rcvif;
164                 ieee80211_free_node(ni);
165         }
166         m->m_nextpkt = NULL;
167         m_freem(m);
168 }
169
170 /*
171  * Clear any frames queued in the power save queue.
172  * The number of frames that were present is returned.
173  */
174 static int
175 psq_drain(struct ieee80211_psq *psq)
176 {
177         struct ieee80211_psq_head *qhead;
178         struct mbuf *m;
179         int qlen;
180
181         IEEE80211_PSQ_LOCK(psq);
182         qlen = psq->psq_len;
183         qhead = &psq->psq_head[0];
184 again:
185         while ((m = qhead->head) != NULL) {
186                 qhead->head = m->m_nextpkt;
187                 psq_mfree(m);
188         }
189         qhead->tail = NULL;
190         qhead->len = 0;
191         if (qhead == &psq->psq_head[0]) {       /* Algol-68 style for loop */
192                 qhead = &psq->psq_head[1];
193                 goto again;
194         }
195         psq->psq_len = 0;
196         IEEE80211_PSQ_UNLOCK(psq);
197
198         return qlen;
199 }
200
201 /*
202  * Clear any frames queued in the power save queue.
203  * The number of frames that were present is returned.
204  */
205 int
206 ieee80211_node_psq_drain(struct ieee80211_node *ni)
207 {
208         return psq_drain(&ni->ni_psq);
209 }
210
211 /*
212  * Age frames on the power save queue. The aging interval is
213  * 4 times the listen interval specified by the station.  This
214  * number is factored into the age calculations when the frame
215  * is placed on the queue.  We store ages as time differences
216  * so we can check and/or adjust only the head of the list.
217  * If a frame's age exceeds the threshold then discard it.
218  * The number of frames discarded is returned so the caller
219  * can check if it needs to adjust the tim.
220  */
221 int
222 ieee80211_node_psq_age(struct ieee80211_node *ni)
223 {
224         struct ieee80211_psq *psq = &ni->ni_psq;
225         int discard = 0;
226
227         if (psq->psq_len != 0) {
228 #ifdef IEEE80211_DEBUG
229                 struct ieee80211vap *vap = ni->ni_vap;
230 #endif
231                 struct ieee80211_psq_head *qhead;
232                 struct mbuf *m;
233
234                 IEEE80211_PSQ_LOCK(psq);
235                 qhead = &psq->psq_head[0];
236         again:
237                 while ((m = qhead->head) != NULL &&
238                     M_AGE_GET(m) < IEEE80211_INACT_WAIT) {
239                         IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
240                              "discard frame, age %u", M_AGE_GET(m));
241                         if ((qhead->head = m->m_nextpkt) == NULL)
242                                 qhead->tail = NULL;
243                         KASSERT(qhead->len > 0, ("qhead len %d", qhead->len));
244                         qhead->len--;
245                         KASSERT(psq->psq_len > 0, ("psq len %d", psq->psq_len));
246                         psq->psq_len--;
247                         psq_mfree(m);
248                         discard++;
249                 }
250                 if (qhead == &psq->psq_head[0]) { /* Algol-68 style for loop */
251                         qhead = &psq->psq_head[1];
252                         goto again;
253                 }
254                 if (m != NULL)
255                         M_AGE_SUB(m, IEEE80211_INACT_WAIT);
256                 IEEE80211_PSQ_UNLOCK(psq);
257
258                 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
259                     "discard %u frames for age", discard);
260                 IEEE80211_NODE_STAT_ADD(ni, ps_discard, discard);
261         }
262         return discard;
263 }
264
265 /*
266  * Handle a change in the PS station occupancy.
267  */
268 static void
269 ieee80211_update_ps(struct ieee80211vap *vap, int nsta)
270 {
271
272         KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
273                 vap->iv_opmode == IEEE80211_M_IBSS,
274                 ("operating mode %u", vap->iv_opmode));
275 }
276
277 /*
278  * Indicate whether there are frames queued for a station in power-save mode.
279  */
280 static int
281 ieee80211_set_tim(struct ieee80211_node *ni, int set)
282 {
283         struct ieee80211vap *vap = ni->ni_vap;
284         struct ieee80211com *ic = ni->ni_ic;
285         uint16_t aid;
286         int changed;
287
288         KASSERT(vap->iv_opmode == IEEE80211_M_HOSTAP ||
289                 vap->iv_opmode == IEEE80211_M_IBSS,
290                 ("operating mode %u", vap->iv_opmode));
291
292         aid = IEEE80211_AID(ni->ni_associd);
293         KASSERT(aid < vap->iv_max_aid,
294                 ("bogus aid %u, max %u", aid, vap->iv_max_aid));
295
296         IEEE80211_LOCK(ic);
297         changed = (set != (isset(vap->iv_tim_bitmap, aid) != 0));
298         if (changed) {
299                 if (set) {
300                         setbit(vap->iv_tim_bitmap, aid);
301                         vap->iv_ps_pending++;
302                 } else {
303                         clrbit(vap->iv_tim_bitmap, aid);
304                         vap->iv_ps_pending--;
305                 }
306                 /* NB: we know vap is in RUN state so no need to check */
307                 vap->iv_update_beacon(vap, IEEE80211_BEACON_TIM);
308         }
309         IEEE80211_UNLOCK(ic);
310
311         return changed;
312 }
313
314 /*
315  * Save an outbound packet for a node in power-save sleep state.
316  * The new packet is placed on the node's saved queue, and the TIM
317  * is changed, if necessary.
318  */
319 int
320 ieee80211_pwrsave(struct ieee80211_node *ni, struct mbuf *m)
321 {
322         struct ieee80211_psq *psq = &ni->ni_psq;
323         struct ieee80211vap *vap = ni->ni_vap;
324         struct ieee80211com *ic = ni->ni_ic;
325         struct ieee80211_psq_head *qhead;
326         int qlen, age;
327
328         IEEE80211_PSQ_LOCK(psq);
329         if (psq->psq_len >= psq->psq_maxlen) {
330                 psq->psq_drops++;
331                 IEEE80211_PSQ_UNLOCK(psq);
332                 IEEE80211_NOTE(vap, IEEE80211_MSG_ANY, ni,
333                     "pwr save q overflow, drops %d (size %d)",
334                     psq->psq_drops, psq->psq_len);
335 #ifdef IEEE80211_DEBUG
336                 if (ieee80211_msg_dumppkts(vap))
337                         ieee80211_dump_pkt(ni->ni_ic, mtod(m, caddr_t),
338                             m->m_len, -1, -1);
339 #endif
340                 psq_mfree(m);
341                 return ENOSPC;
342         }
343         /*
344          * Tag the frame with it's expiry time and insert it in
345          * the appropriate queue.  The aging interval is 4 times
346          * the listen interval specified by the station. Frames
347          * that sit around too long are reclaimed using this
348          * information.
349          */
350         /* TU -> secs.  XXX handle overflow? */
351         age = IEEE80211_TU_TO_MS((ni->ni_intval * ic->ic_bintval) << 2) / 1000;
352         /*
353          * Encapsulated frames go on the high priority queue,
354          * other stuff goes on the low priority queue.  We use
355          * this to order frames returned out of the driver
356          * ahead of frames we collect in ieee80211_start.
357          */
358         if (m->m_flags & M_ENCAP)
359                 qhead = &psq->psq_head[0];
360         else
361                 qhead = &psq->psq_head[1];
362         if (qhead->tail == NULL) {
363                 struct mbuf *mh;
364
365                 qhead->head = m;
366                 /*
367                  * Take care to adjust age when inserting the first
368                  * frame of a queue and the other queue already has
369                  * frames.  We need to preserve the age difference
370                  * relationship so ieee80211_node_psq_age works.
371                  */
372                 if (qhead == &psq->psq_head[1]) {
373                         mh = psq->psq_head[0].head;
374                         if (mh != NULL)
375                                 age-= M_AGE_GET(mh);
376                 } else {
377                         mh = psq->psq_head[1].head;
378                         if (mh != NULL) {
379                                 int nage = M_AGE_GET(mh) - age;
380                                 /* XXX is clamping to zero good 'nuf? */
381                                 M_AGE_SET(mh, nage < 0 ? 0 : nage);
382                         }
383                 }
384         } else {
385                 qhead->tail->m_nextpkt = m;
386                 age -= M_AGE_GET(qhead->head);
387         }
388         KASSERT(age >= 0, ("age %d", age));
389         M_AGE_SET(m, age);
390         m->m_nextpkt = NULL;
391         qhead->tail = m;
392         qhead->len++;
393         qlen = ++(psq->psq_len);
394         IEEE80211_PSQ_UNLOCK(psq);
395
396         IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
397             "save frame with age %d, %u now queued", age, qlen);
398
399         if (qlen == 1 && vap->iv_set_tim != NULL)
400                 vap->iv_set_tim(ni, 1);
401
402         return 0;
403 }
404
405 /*
406  * Move frames from the ps q to the vap's send queue
407  * and/or the driver's send queue; and kick the start
408  * method for each, as appropriate.  Note we're careful
409  * to preserve packet ordering here.
410  */
411 static void
412 pwrsave_flushq(struct ieee80211_node *ni)
413 {
414         struct ieee80211_psq *psq = &ni->ni_psq;
415         struct ieee80211vap *vap = ni->ni_vap;
416         struct ieee80211_psq_head *qhead;
417         struct ifnet *parent, *ifp;
418
419         IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
420             "flush ps queue, %u packets queued", psq->psq_len);
421
422         IEEE80211_PSQ_LOCK(psq);
423         qhead = &psq->psq_head[0];      /* 802.11 frames */
424         if (qhead->head != NULL) {
425                 /* XXX could dispatch through vap and check M_ENCAP */
426                 parent = vap->iv_ic->ic_ifp;
427                 /* XXX need different driver interface */
428                 /* XXX bypasses q max and OACTIVE */
429                 IF_PREPEND_LIST(&parent->if_snd, qhead->head, qhead->tail,
430                     qhead->len);
431                 qhead->head = qhead->tail = NULL;
432                 qhead->len = 0;
433         } else
434                 parent = NULL;
435
436         qhead = &psq->psq_head[1];      /* 802.3 frames */
437         if (qhead->head != NULL) {
438                 ifp = vap->iv_ifp;
439                 /* XXX need different driver interface */
440                 /* XXX bypasses q max and OACTIVE */
441                 IF_PREPEND_LIST(&ifp->if_snd, qhead->head, qhead->tail,
442                     qhead->len);
443                 qhead->head = qhead->tail = NULL;
444                 qhead->len = 0;
445         } else
446                 ifp = NULL;
447         psq->psq_len = 0;
448         IEEE80211_PSQ_UNLOCK(psq);
449
450         /* NB: do this outside the psq lock */
451         /* XXX packets might get reordered if parent is OACTIVE */
452         if (parent != NULL)
453                 parent->if_start(parent);
454         if (ifp != NULL)
455                 ifp->if_start(ifp);
456 }
457
458 /*
459  * Handle station power-save state change.
460  */
461 void
462 ieee80211_node_pwrsave(struct ieee80211_node *ni, int enable)
463 {
464         struct ieee80211vap *vap = ni->ni_vap;
465         int update;
466
467         update = 0;
468         if (enable) {
469                 if ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) == 0) {
470                         vap->iv_ps_sta++;
471                         update = 1;
472                 }
473                 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
474                 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
475                     "power save mode on, %u sta's in ps mode", vap->iv_ps_sta);
476
477                 if (update)
478                         vap->iv_update_ps(vap, vap->iv_ps_sta);
479         } else {
480                 if (ni->ni_flags & IEEE80211_NODE_PWR_MGT) {
481                         vap->iv_ps_sta--;
482                         update = 1;
483                 }
484                 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
485                 IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
486                     "power save mode off, %u sta's in ps mode", vap->iv_ps_sta);
487
488                 /* NB: order here is intentional so TIM is clear before flush */
489                 if (vap->iv_set_tim != NULL)
490                         vap->iv_set_tim(ni, 0);
491                 if (update) {
492                         /* NB if no sta's in ps, driver should flush mc q */
493                         vap->iv_update_ps(vap, vap->iv_ps_sta);
494                 }
495                 if (ni->ni_psq.psq_len != 0)
496                         pwrsave_flushq(ni);
497         }
498 }
499
500 /*
501  * Handle power-save state change in station mode.
502  */
503 void
504 ieee80211_sta_pwrsave(struct ieee80211vap *vap, int enable)
505 {
506         struct ieee80211_node *ni = vap->iv_bss;
507
508         if (!((enable != 0) ^ ((ni->ni_flags & IEEE80211_NODE_PWR_MGT) != 0)))
509                 return;
510
511         IEEE80211_NOTE(vap, IEEE80211_MSG_POWER, ni,
512             "sta power save mode %s", enable ? "on" : "off");
513         if (!enable) {
514                 ni->ni_flags &= ~IEEE80211_NODE_PWR_MGT;
515                 ieee80211_send_nulldata(ieee80211_ref_node(ni));
516                 /*
517                  * Flush any queued frames; we can do this immediately
518                  * because we know they'll be queued behind the null
519                  * data frame we send the ap.
520                  * XXX can we use a data frame to take us out of ps?
521                  */
522                 if (ni->ni_psq.psq_len != 0)
523                         pwrsave_flushq(ni);
524         } else {
525                 ni->ni_flags |= IEEE80211_NODE_PWR_MGT;
526                 ieee80211_send_nulldata(ieee80211_ref_node(ni));
527         }
528 }