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