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