ieee80211_superg.c revision 191550
1/*-
2 * Copyright (c) 2002-2009 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: head/sys/net80211/ieee80211_superg.c 191550 2009-04-26 22:45:21Z sam $");
28
29#include "opt_wlan.h"
30
31#include <sys/param.h>
32#include <sys/systm.h>
33#include <sys/mbuf.h>
34#include <sys/kernel.h>
35#include <sys/endian.h>
36
37#include <sys/socket.h>
38
39#include <net/bpf.h>
40#include <net/ethernet.h>
41#include <net/if.h>
42#include <net/if_llc.h>
43#include <net/if_media.h>
44
45#include <net80211/ieee80211_var.h>
46#include <net80211/ieee80211_input.h>
47#include <net80211/ieee80211_phy.h>
48#include <net80211/ieee80211_superg.h>
49
50/*
51 * Atheros fast-frame encapsulation format.
52 * FF max payload:
53 * 802.2 + FFHDR + HPAD + 802.3 + 802.2 + 1500 + SPAD + 802.3 + 802.2 + 1500:
54 *   8   +   4   +  4   +   14  +   8   + 1500 +  6   +   14  +   8   + 1500
55 * = 3066
56 */
57/* fast frame header is 32-bits */
58#define	ATH_FF_PROTO	0x0000003f	/* protocol */
59#define	ATH_FF_PROTO_S	0
60#define	ATH_FF_FTYPE	0x000000c0	/* frame type */
61#define	ATH_FF_FTYPE_S	6
62#define	ATH_FF_HLEN32	0x00000300	/* optional hdr length */
63#define	ATH_FF_HLEN32_S	8
64#define	ATH_FF_SEQNUM	0x001ffc00	/* sequence number */
65#define	ATH_FF_SEQNUM_S	10
66#define	ATH_FF_OFFSET	0xffe00000	/* offset to 2nd payload */
67#define	ATH_FF_OFFSET_S	21
68
69#define	ATH_FF_MAX_HDR_PAD	4
70#define	ATH_FF_MAX_SEP_PAD	6
71#define	ATH_FF_MAX_HDR		30
72
73#define	ATH_FF_PROTO_L2TUNNEL	0	/* L2 tunnel protocol */
74#define	ATH_FF_ETH_TYPE		0x88bd	/* Ether type for encapsulated frames */
75#define	ATH_FF_SNAP_ORGCODE_0	0x00
76#define	ATH_FF_SNAP_ORGCODE_1	0x03
77#define	ATH_FF_SNAP_ORGCODE_2	0x7f
78
79#define	ATH_FF_TXQMIN	2		/* min txq depth for staging */
80#define	ATH_FF_TXQMAX	50		/* maximum # of queued frames allowed */
81#define	ATH_FF_STAGEMAX	5		/* max waiting period for staged frame*/
82
83#define	ETHER_HEADER_COPY(dst, src) \
84	memcpy(dst, src, sizeof(struct ether_header))
85
86/* XXX public for sysctl hookup */
87int	ieee80211_ffppsmin = 2;		/* pps threshold for ff aggregation */
88int	ieee80211_ffagemax = -1;	/* max time frames held on stage q */
89
90void
91ieee80211_superg_attach(struct ieee80211com *ic)
92{
93	ieee80211_ffagemax = msecs_to_ticks(150);
94}
95
96void
97ieee80211_superg_detach(struct ieee80211com *ic)
98{
99}
100
101void
102ieee80211_superg_vattach(struct ieee80211vap *vap)
103{
104	if (vap->iv_caps & IEEE80211_C_FF)
105		vap->iv_flags |= IEEE80211_F_FF;
106	/* NB: we only implement sta mode */
107	if (vap->iv_opmode == IEEE80211_M_STA &&
108	    (vap->iv_caps & IEEE80211_C_TURBOP))
109		vap->iv_flags |= IEEE80211_F_TURBOP;
110}
111
112void
113ieee80211_superg_vdetach(struct ieee80211vap *vap)
114{
115}
116
117#define	ATH_OUI_BYTES		0x00, 0x03, 0x7f
118/*
119 * Add a WME information element to a frame.
120 */
121uint8_t *
122ieee80211_add_ath(uint8_t *frm, uint8_t caps, ieee80211_keyix defkeyix)
123{
124	static const struct ieee80211_ath_ie info = {
125		.ath_id		= IEEE80211_ELEMID_VENDOR,
126		.ath_len	= sizeof(struct ieee80211_ath_ie) - 2,
127		.ath_oui	= { ATH_OUI_BYTES },
128		.ath_oui_type	= ATH_OUI_TYPE,
129		.ath_oui_subtype= ATH_OUI_SUBTYPE,
130		.ath_version	= ATH_OUI_VERSION,
131	};
132	struct ieee80211_ath_ie *ath = (struct ieee80211_ath_ie *) frm;
133
134	memcpy(frm, &info, sizeof(info));
135	ath->ath_capability = caps;
136	if (defkeyix != IEEE80211_KEYIX_NONE) {
137		ath->ath_defkeyix[0] = (defkeyix & 0xff);
138		ath->ath_defkeyix[1] = ((defkeyix >> 8) & 0xff);
139	} else {
140		ath->ath_defkeyix[0] = 0xff;
141		ath->ath_defkeyix[1] = 0x7f;
142	}
143	return frm + sizeof(info);
144}
145#undef ATH_OUI_BYTES
146
147uint8_t *
148ieee80211_add_athcaps(uint8_t *frm, const struct ieee80211_node *bss)
149{
150	const struct ieee80211vap *vap = bss->ni_vap;
151
152	return ieee80211_add_ath(frm,
153	    vap->iv_flags & IEEE80211_F_ATHEROS,
154	    ((vap->iv_flags & IEEE80211_F_WPA) == 0 &&
155	    bss->ni_authmode != IEEE80211_AUTH_8021X) ?
156	    vap->iv_def_txkey : IEEE80211_KEYIX_NONE);
157}
158
159void
160ieee80211_parse_ath(struct ieee80211_node *ni, uint8_t *ie)
161{
162	const struct ieee80211_ath_ie *ath =
163		(const struct ieee80211_ath_ie *) ie;
164
165	ni->ni_ath_flags = ath->ath_capability;
166	ni->ni_ath_defkeyix = LE_READ_2(&ath->ath_defkeyix);
167}
168
169int
170ieee80211_parse_athparams(struct ieee80211_node *ni, uint8_t *frm,
171	const struct ieee80211_frame *wh)
172{
173	struct ieee80211vap *vap = ni->ni_vap;
174	const struct ieee80211_ath_ie *ath;
175	u_int len = frm[1];
176	int capschanged;
177	uint16_t defkeyix;
178
179	if (len < sizeof(struct ieee80211_ath_ie)-2) {
180		IEEE80211_DISCARD_IE(vap,
181		    IEEE80211_MSG_ELEMID | IEEE80211_MSG_SUPERG,
182		    wh, "Atheros", "too short, len %u", len);
183		return -1;
184	}
185	ath = (const struct ieee80211_ath_ie *)frm;
186	capschanged = (ni->ni_ath_flags != ath->ath_capability);
187	defkeyix = LE_READ_2(ath->ath_defkeyix);
188	if (capschanged || defkeyix != ni->ni_ath_defkeyix) {
189		ni->ni_ath_flags = ath->ath_capability;
190		ni->ni_ath_defkeyix = defkeyix;
191		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
192		    "ath ie change: new caps 0x%x defkeyix 0x%x",
193		    ni->ni_ath_flags, ni->ni_ath_defkeyix);
194	}
195	if (IEEE80211_ATH_CAP(vap, ni, ATHEROS_CAP_TURBO_PRIME)) {
196		uint16_t curflags, newflags;
197
198		/*
199		 * Check for turbo mode switch.  Calculate flags
200		 * for the new mode and effect the switch.
201		 */
202		newflags = curflags = vap->iv_ic->ic_bsschan->ic_flags;
203		/* NB: BOOST is not in ic_flags, so get it from the ie */
204		if (ath->ath_capability & ATHEROS_CAP_BOOST)
205			newflags |= IEEE80211_CHAN_TURBO;
206		else
207			newflags &= ~IEEE80211_CHAN_TURBO;
208		if (newflags != curflags)
209			ieee80211_dturbo_switch(vap, newflags);
210	}
211	return capschanged;
212}
213
214/*
215 * Decap the encapsulated frame pair and dispatch the first
216 * for delivery.  The second frame is returned for delivery
217 * via the normal path.
218 */
219struct mbuf *
220ieee80211_ff_decap(struct ieee80211_node *ni, struct mbuf *m)
221{
222#define	FF_LLC_SIZE	(sizeof(struct ether_header) + sizeof(struct llc))
223#define	MS(x,f)	(((x) & f) >> f##_S)
224	struct ieee80211vap *vap = ni->ni_vap;
225	struct llc *llc;
226	uint32_t ath;
227	struct mbuf *n;
228	int framelen;
229
230	/* NB: we assume caller does this check for us */
231	KASSERT(IEEE80211_ATH_CAP(vap, ni, IEEE80211_NODE_FF),
232	    ("ff not negotiated"));
233	/*
234	 * Check for fast-frame tunnel encapsulation.
235	 */
236	if (m->m_pkthdr.len < 3*FF_LLC_SIZE)
237		return m;
238	if (m->m_len < FF_LLC_SIZE &&
239	    (m = m_pullup(m, FF_LLC_SIZE)) == NULL) {
240		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
241		    ni->ni_macaddr, "fast-frame",
242		    "%s", "m_pullup(llc) failed");
243		vap->iv_stats.is_rx_tooshort++;
244		return NULL;
245	}
246	llc = (struct llc *)(mtod(m, uint8_t *) +
247	    sizeof(struct ether_header));
248	if (llc->llc_snap.ether_type != htons(ATH_FF_ETH_TYPE))
249		return m;
250	m_adj(m, FF_LLC_SIZE);
251	m_copydata(m, 0, sizeof(uint32_t), (caddr_t) &ath);
252	if (MS(ath, ATH_FF_PROTO) != ATH_FF_PROTO_L2TUNNEL) {
253		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
254		    ni->ni_macaddr, "fast-frame",
255		    "unsupport tunnel protocol, header 0x%x", ath);
256		vap->iv_stats.is_ff_badhdr++;
257		m_freem(m);
258		return NULL;
259	}
260	/* NB: skip header and alignment padding */
261	m_adj(m, roundup(sizeof(uint32_t) - 2, 4) + 2);
262
263	vap->iv_stats.is_ff_decap++;
264
265	/*
266	 * Decap the first frame, bust it apart from the
267	 * second and deliver; then decap the second frame
268	 * and return it to the caller for normal delivery.
269	 */
270	m = ieee80211_decap1(m, &framelen);
271	if (m == NULL) {
272		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
273		    ni->ni_macaddr, "fast-frame", "%s", "first decap failed");
274		vap->iv_stats.is_ff_tooshort++;
275		return NULL;
276	}
277	n = m_split(m, framelen, M_NOWAIT);
278	if (n == NULL) {
279		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
280		    ni->ni_macaddr, "fast-frame",
281		    "%s", "unable to split encapsulated frames");
282		vap->iv_stats.is_ff_split++;
283		m_freem(m);			/* NB: must reclaim */
284		return NULL;
285	}
286	/* XXX not right for WDS */
287	vap->iv_deliver_data(vap, ni, m);	/* 1st of pair */
288
289	/*
290	 * Decap second frame.
291	 */
292	m_adj(n, roundup2(framelen, 4) - framelen);	/* padding */
293	n = ieee80211_decap1(n, &framelen);
294	if (n == NULL) {
295		IEEE80211_DISCARD_MAC(vap, IEEE80211_MSG_ANY,
296		    ni->ni_macaddr, "fast-frame", "%s", "second decap failed");
297		vap->iv_stats.is_ff_tooshort++;
298	}
299	/* XXX verify framelen against mbuf contents */
300	return n;				/* 2nd delivered by caller */
301#undef MS
302#undef FF_LLC_SIZE
303}
304
305/*
306 * Do Ethernet-LLC encapsulation for each payload in a fast frame
307 * tunnel encapsulation.  The frame is assumed to have an Ethernet
308 * header at the front that must be stripped before prepending the
309 * LLC followed by the Ethernet header passed in (with an Ethernet
310 * type that specifies the payload size).
311 */
312static struct mbuf *
313ff_encap1(struct ieee80211vap *vap, struct mbuf *m,
314	const struct ether_header *eh)
315{
316	struct llc *llc;
317	uint16_t payload;
318
319	/* XXX optimize by combining m_adj+M_PREPEND */
320	m_adj(m, sizeof(struct ether_header) - sizeof(struct llc));
321	llc = mtod(m, struct llc *);
322	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
323	llc->llc_control = LLC_UI;
324	llc->llc_snap.org_code[0] = 0;
325	llc->llc_snap.org_code[1] = 0;
326	llc->llc_snap.org_code[2] = 0;
327	llc->llc_snap.ether_type = eh->ether_type;
328	payload = m->m_pkthdr.len;		/* NB: w/o Ethernet header */
329
330	M_PREPEND(m, sizeof(struct ether_header), M_DONTWAIT);
331	if (m == NULL) {		/* XXX cannot happen */
332		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
333			"%s: no space for ether_header\n", __func__);
334		vap->iv_stats.is_tx_nobuf++;
335		return NULL;
336	}
337	ETHER_HEADER_COPY(mtod(m, void *), eh);
338	mtod(m, struct ether_header *)->ether_type = htons(payload);
339	return m;
340}
341
342/*
343 * Fast frame encapsulation.  There must be two packets
344 * chained with m_nextpkt.  We do header adjustment for
345 * each, add the tunnel encapsulation, and then concatenate
346 * the mbuf chains to form a single frame for transmission.
347 */
348struct mbuf *
349ieee80211_ff_encap(struct ieee80211vap *vap, struct mbuf *m1, int hdrspace,
350	struct ieee80211_key *key)
351{
352	struct mbuf *m2;
353	struct ether_header eh1, eh2;
354	struct llc *llc;
355	struct mbuf *m;
356	int pad;
357
358	m2 = m1->m_nextpkt;
359	if (m2 == NULL) {
360		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
361		    "%s: only one frame\n", __func__);
362		goto bad;
363	}
364	m1->m_nextpkt = NULL;
365	/*
366	 * Include fast frame headers in adjusting header layout.
367	 */
368	KASSERT(m1->m_len >= sizeof(eh1), ("no ethernet header!"));
369	ETHER_HEADER_COPY(&eh1, mtod(m1, caddr_t));
370	m1 = ieee80211_mbuf_adjust(vap,
371		hdrspace + sizeof(struct llc) + sizeof(uint32_t) + 2 +
372		    sizeof(struct ether_header),
373		key, m1);
374	if (m1 == NULL) {
375		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
376		m_freem(m2);
377		goto bad;
378	}
379
380	/*
381	 * Copy second frame's Ethernet header out of line
382	 * and adjust for encapsulation headers.  Note that
383	 * we make room for padding in case there isn't room
384	 * at the end of first frame.
385	 */
386	KASSERT(m2->m_len >= sizeof(eh2), ("no ethernet header!"));
387	ETHER_HEADER_COPY(&eh2, mtod(m2, caddr_t));
388	m2 = ieee80211_mbuf_adjust(vap,
389		ATH_FF_MAX_HDR_PAD + sizeof(struct ether_header),
390		NULL, m2);
391	if (m2 == NULL) {
392		/* NB: ieee80211_mbuf_adjust handles msgs+statistics */
393		goto bad;
394	}
395
396	/*
397	 * Now do tunnel encapsulation.  First, each
398	 * frame gets a standard encapsulation.
399	 */
400	m1 = ff_encap1(vap, m1, &eh1);
401	if (m1 == NULL)
402		goto bad;
403	m2 = ff_encap1(vap, m2, &eh2);
404	if (m2 == NULL)
405		goto bad;
406
407	/*
408	 * Pad leading frame to a 4-byte boundary.  If there
409	 * is space at the end of the first frame, put it
410	 * there; otherwise prepend to the front of the second
411	 * frame.  We know doing the second will always work
412	 * because we reserve space above.  We prefer appending
413	 * as this typically has better DMA alignment properties.
414	 */
415	for (m = m1; m->m_next != NULL; m = m->m_next)
416		;
417	pad = roundup2(m1->m_pkthdr.len, 4) - m1->m_pkthdr.len;
418	if (pad) {
419		if (M_TRAILINGSPACE(m) < pad) {		/* prepend to second */
420			m2->m_data -= pad;
421			m2->m_len += pad;
422			m2->m_pkthdr.len += pad;
423		} else {				/* append to first */
424			m->m_len += pad;
425			m1->m_pkthdr.len += pad;
426		}
427	}
428
429	/*
430	 * Now, stick 'em together and prepend the tunnel headers;
431	 * first the Atheros tunnel header (all zero for now) and
432	 * then a special fast frame LLC.
433	 *
434	 * XXX optimize by prepending together
435	 */
436	m->m_next = m2;			/* NB: last mbuf from above */
437	m1->m_pkthdr.len += m2->m_pkthdr.len;
438	M_PREPEND(m1, sizeof(uint32_t)+2, M_DONTWAIT);
439	if (m1 == NULL) {		/* XXX cannot happen */
440		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
441		    "%s: no space for tunnel header\n", __func__);
442		vap->iv_stats.is_tx_nobuf++;
443		return NULL;
444	}
445	memset(mtod(m1, void *), 0, sizeof(uint32_t)+2);
446
447	M_PREPEND(m1, sizeof(struct llc), M_DONTWAIT);
448	if (m1 == NULL) {		/* XXX cannot happen */
449		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
450		    "%s: no space for llc header\n", __func__);
451		vap->iv_stats.is_tx_nobuf++;
452		return NULL;
453	}
454	llc = mtod(m1, struct llc *);
455	llc->llc_dsap = llc->llc_ssap = LLC_SNAP_LSAP;
456	llc->llc_control = LLC_UI;
457	llc->llc_snap.org_code[0] = ATH_FF_SNAP_ORGCODE_0;
458	llc->llc_snap.org_code[1] = ATH_FF_SNAP_ORGCODE_1;
459	llc->llc_snap.org_code[2] = ATH_FF_SNAP_ORGCODE_2;
460	llc->llc_snap.ether_type = htons(ATH_FF_ETH_TYPE);
461
462	vap->iv_stats.is_ff_encap++;
463
464	return m1;
465bad:
466	if (m1 != NULL)
467		m_freem(m1);
468	if (m2 != NULL)
469		m_freem(m2);
470	return NULL;
471}
472
473static void
474ff_transmit(struct ieee80211_node *ni, struct mbuf *m)
475{
476	struct ieee80211vap *vap = ni->ni_vap;
477	int error;
478
479	/* encap and xmit */
480	m = ieee80211_encap(vap, ni, m);
481	if (m != NULL) {
482		struct ifnet *ifp = vap->iv_ifp;
483		struct ifnet *parent = ni->ni_ic->ic_ifp;
484
485		if (bpf_peers_present(vap->iv_rawbpf))
486			bpf_mtap(vap->iv_rawbpf, m);
487
488		error = parent->if_transmit(parent, m);
489		if (error != 0) {
490			/* NB: IFQ_HANDOFF reclaims mbuf */
491			ieee80211_free_node(ni);
492		} else {
493			ifp->if_opackets++;
494		}
495	} else
496		ieee80211_free_node(ni);
497}
498
499/*
500 * Flush frames to device; note we re-use the linked list
501 * the frames were stored on and use the sentinel (unchanged)
502 * which may be non-NULL.
503 */
504static void
505ff_flush(struct mbuf *head, struct mbuf *last)
506{
507	struct mbuf *m, *next;
508	struct ieee80211_node *ni;
509	struct ieee80211vap *vap;
510
511	for (m = head; m != last; m = next) {
512		next = m->m_nextpkt;
513		m->m_nextpkt = NULL;
514
515		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
516		vap = ni->ni_vap;
517
518		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
519		    "%s: flush frame, age %u", __func__, M_AGE_GET(m));
520		vap->iv_stats.is_ff_flush++;
521
522		ff_transmit(ni, m);
523	}
524}
525
526/*
527 * Age frames on the staging queue.
528 */
529void
530ieee80211_ff_age(struct ieee80211com *ic, struct ieee80211_stageq *sq, int quanta)
531{
532	struct mbuf *m, *head;
533	struct ieee80211_node *ni;
534	struct ieee80211_tx_ampdu *tap;
535
536	KASSERT(sq->head != NULL, ("stageq empty"));
537
538	IEEE80211_LOCK(ic);
539	head = sq->head;
540	while ((m = sq->head) != NULL && M_AGE_GET(m) < quanta) {
541		/* clear tap ref to frame */
542		ni = (struct ieee80211_node *) m->m_pkthdr.rcvif;
543		tap = &ni->ni_tx_ampdu[M_WME_GETAC(m)];
544		KASSERT(tap->txa_private == m, ("staging queue empty"));
545		tap->txa_private = NULL;
546
547		sq->head = m->m_nextpkt;
548		sq->depth--;
549		ic->ic_stageqdepth--;
550	}
551	if (m == NULL)
552		sq->tail = NULL;
553	else
554		M_AGE_SUB(m, quanta);
555	IEEE80211_UNLOCK(ic);
556
557	ff_flush(head, m);
558}
559
560static void
561stageq_add(struct ieee80211_stageq *sq, struct mbuf *m)
562{
563	int age = ieee80211_ffagemax;
564	if (sq->tail != NULL) {
565		sq->tail->m_nextpkt = m;
566		age -= M_AGE_GET(sq->head);
567	} else
568		sq->head = m;
569	KASSERT(age >= 0, ("age %d", age));
570	M_AGE_SET(m, age);
571	m->m_nextpkt = NULL;
572	sq->tail = m;
573	sq->depth++;
574}
575
576static void
577stageq_remove(struct ieee80211_stageq *sq, struct mbuf *mstaged)
578{
579	struct mbuf *m, *mprev;
580
581	mprev = NULL;
582	for (m = sq->head; m != NULL; m = m->m_nextpkt) {
583		if (m == mstaged) {
584			if (mprev == NULL)
585				sq->head = m->m_nextpkt;
586			else
587				mprev->m_nextpkt = m->m_nextpkt;
588			if (sq->tail == m)
589				sq->tail = mprev;
590			sq->depth--;
591			return;
592		}
593		mprev = m;
594	}
595	printf("%s: packet not found\n", __func__);
596}
597
598static uint32_t
599ff_approx_txtime(struct ieee80211_node *ni,
600	const struct mbuf *m1, const struct mbuf *m2)
601{
602	struct ieee80211com *ic = ni->ni_ic;
603	struct ieee80211vap *vap = ni->ni_vap;
604	uint32_t framelen;
605
606	/*
607	 * Approximate the frame length to be transmitted. A swag to add
608	 * the following maximal values to the skb payload:
609	 *   - 32: 802.11 encap + CRC
610	 *   - 24: encryption overhead (if wep bit)
611	 *   - 4 + 6: fast-frame header and padding
612	 *   - 16: 2 LLC FF tunnel headers
613	 *   - 14: 1 802.3 FF tunnel header (mbuf already accounts for 2nd)
614	 */
615	framelen = m1->m_pkthdr.len + 32 +
616	    ATH_FF_MAX_HDR_PAD + ATH_FF_MAX_SEP_PAD + ATH_FF_MAX_HDR;
617	if (vap->iv_flags & IEEE80211_F_PRIVACY)
618		framelen += 24;
619	if (m2 != NULL)
620		framelen += m2->m_pkthdr.len;
621	return ieee80211_compute_duration(ic->ic_rt, framelen, ni->ni_txrate, 0);
622}
623
624/*
625 * Check if the supplied frame can be partnered with an existing
626 * or pending frame.  Return a reference to any frame that should be
627 * sent on return; otherwise return NULL.
628 */
629struct mbuf *
630ieee80211_ff_check(struct ieee80211_node *ni, struct mbuf *m)
631{
632	struct ieee80211vap *vap = ni->ni_vap;
633	struct ieee80211com *ic = ni->ni_ic;
634	const int pri = M_WME_GETAC(m);
635	struct ieee80211_stageq *sq;
636	struct ieee80211_tx_ampdu *tap;
637	struct mbuf *mstaged;
638	uint32_t txtime, limit;
639
640	/*
641	 * Check if the supplied frame can be aggregated.
642	 *
643	 * NB: we allow EAPOL frames to be aggregated with other ucast traffic.
644	 *     Do 802.1x EAPOL frames proceed in the clear? Then they couldn't
645	 *     be aggregated with other types of frames when encryption is on?
646	 */
647	IEEE80211_LOCK(ic);
648	tap = &ni->ni_tx_ampdu[pri];
649	mstaged = tap->txa_private;		/* NB: we reuse AMPDU state */
650	ieee80211_txampdu_count_packet(tap);
651
652	/*
653	 * When not in station mode never aggregate a multicast
654	 * frame; this insures, for example, that a combined frame
655	 * does not require multiple encryption keys.
656	 */
657	if (vap->iv_opmode != IEEE80211_M_STA &&
658	    ETHER_IS_MULTICAST(mtod(m, struct ether_header *)->ether_dhost)) {
659		/* XXX flush staged frame? */
660		IEEE80211_UNLOCK(ic);
661		return m;
662	}
663	/*
664	 * If there is no frame to combine with and the pps is
665	 * too low; then do not attempt to aggregate this frame.
666	 */
667	if (mstaged == NULL &&
668	    ieee80211_txampdu_getpps(tap) < ieee80211_ffppsmin) {
669		IEEE80211_UNLOCK(ic);
670		return m;
671	}
672	sq = &ic->ic_ff_stageq[pri];
673	/*
674	 * Check the txop limit to insure the aggregate fits.
675	 */
676	limit = IEEE80211_TXOP_TO_US(
677		ic->ic_wme.wme_chanParams.cap_wmeParams[pri].wmep_txopLimit);
678	if (limit != 0 &&
679	    (txtime = ff_approx_txtime(ni, m, mstaged)) > limit) {
680		/*
681		 * Aggregate too long, return to the caller for direct
682		 * transmission.  In addition, flush any pending frame
683		 * before sending this one.
684		 */
685		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
686		    "%s: txtime %u exceeds txop limit %u\n",
687		    __func__, txtime, limit);
688
689		tap->txa_private = NULL;
690		if (mstaged != NULL)
691			stageq_remove(sq, mstaged);
692		IEEE80211_UNLOCK(ic);
693
694		if (mstaged != NULL) {
695			IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
696			    "%s: flush staged frame", __func__);
697			/* encap and xmit */
698			ff_transmit(ni, mstaged);
699		}
700		return m;		/* NB: original frame */
701	}
702	/*
703	 * An aggregation candidate.  If there's a frame to partner
704	 * with then combine and return for processing.  Otherwise
705	 * save this frame and wait for a partner to show up (or
706	 * the frame to be flushed).  Note that staged frames also
707	 * hold their node reference.
708	 */
709	if (mstaged != NULL) {
710		tap->txa_private = NULL;
711		stageq_remove(sq, mstaged);
712		IEEE80211_UNLOCK(ic);
713
714		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
715		    "%s: aggregate fast-frame", __func__);
716		/*
717		 * Release the node reference; we only need
718		 * the one already in mstaged.
719		 */
720		KASSERT(mstaged->m_pkthdr.rcvif == (void *)ni,
721		    ("rcvif %p ni %p", mstaged->m_pkthdr.rcvif, ni));
722		ieee80211_free_node(ni);
723
724		m->m_nextpkt = NULL;
725		mstaged->m_nextpkt = m;
726		mstaged->m_flags |= M_FF; /* NB: mark for encap work */
727	} else {
728		KASSERT(tap->txa_private == NULL,
729		    ("txa_private %p", tap->txa_private));
730		tap->txa_private = m;
731
732		stageq_add(sq, m);
733		ic->ic_stageqdepth++;
734		IEEE80211_UNLOCK(ic);
735
736		IEEE80211_NOTE(vap, IEEE80211_MSG_SUPERG, ni,
737		    "%s: stage frame, %u queued", __func__, sq->depth);
738		/* NB: mstaged is NULL */
739	}
740	return mstaged;
741}
742
743void
744ieee80211_ff_node_init(struct ieee80211_node *ni)
745{
746	/*
747	 * Clean FF state on re-associate.  This handles the case
748	 * where a station leaves w/o notifying us and then returns
749	 * before node is reaped for inactivity.
750	 */
751	ieee80211_ff_node_cleanup(ni);
752}
753
754void
755ieee80211_ff_node_cleanup(struct ieee80211_node *ni)
756{
757	struct ieee80211com *ic = ni->ni_ic;
758	struct ieee80211_tx_ampdu *tap;
759	struct mbuf *m, *head;
760	int ac;
761
762	IEEE80211_LOCK(ic);
763	head = NULL;
764	for (ac = 0; ac < WME_NUM_AC; ac++) {
765		tap = &ni->ni_tx_ampdu[ac];
766		m = tap->txa_private;
767		if (m != NULL) {
768			tap->txa_private = NULL;
769			stageq_remove(&ic->ic_ff_stageq[ac], m);
770			m->m_nextpkt = head;
771			head = m;
772		}
773	}
774	IEEE80211_UNLOCK(ic);
775
776	for (m = head; m != NULL; m = m->m_nextpkt) {
777		m_freem(m);
778		ieee80211_free_node(ni);
779	}
780}
781
782/*
783 * Switch between turbo and non-turbo operating modes.
784 * Use the specified channel flags to locate the new
785 * channel, update 802.11 state, and then call back into
786 * the driver to effect the change.
787 */
788void
789ieee80211_dturbo_switch(struct ieee80211vap *vap, int newflags)
790{
791	struct ieee80211com *ic = vap->iv_ic;
792	struct ieee80211_channel *chan;
793
794	chan = ieee80211_find_channel(ic, ic->ic_bsschan->ic_freq, newflags);
795	if (chan == NULL) {		/* XXX should not happen */
796		IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
797		    "%s: no channel with freq %u flags 0x%x\n",
798		    __func__, ic->ic_bsschan->ic_freq, newflags);
799		return;
800	}
801
802	IEEE80211_DPRINTF(vap, IEEE80211_MSG_SUPERG,
803	    "%s: %s -> %s (freq %u flags 0x%x)\n", __func__,
804	    ieee80211_phymode_name[ieee80211_chan2mode(ic->ic_bsschan)],
805	    ieee80211_phymode_name[ieee80211_chan2mode(chan)],
806	    chan->ic_freq, chan->ic_flags);
807
808	ic->ic_bsschan = chan;
809	ic->ic_prevchan = ic->ic_curchan;
810	ic->ic_curchan = chan;
811	ic->ic_rt = ieee80211_get_ratetable(chan);
812	ic->ic_set_channel(ic);
813	/* NB: do not need to reset ERP state 'cuz we're in sta mode */
814}
815
816/*
817 * Return the current ``state'' of an Atheros capbility.
818 * If associated in station mode report the negotiated
819 * setting. Otherwise report the current setting.
820 */
821static int
822getathcap(struct ieee80211vap *vap, int cap)
823{
824	if (vap->iv_opmode == IEEE80211_M_STA &&
825	    vap->iv_state == IEEE80211_S_RUN)
826		return IEEE80211_ATH_CAP(vap, vap->iv_bss, cap) != 0;
827	else
828		return (vap->iv_flags & cap) != 0;
829}
830
831static int
832superg_ioctl_get80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
833{
834	switch (ireq->i_type) {
835	case IEEE80211_IOC_FF:
836		ireq->i_val = getathcap(vap, IEEE80211_F_FF);
837		break;
838	case IEEE80211_IOC_TURBOP:
839		ireq->i_val = getathcap(vap, IEEE80211_F_TURBOP);
840		break;
841	default:
842		return ENOSYS;
843	}
844	return 0;
845}
846IEEE80211_IOCTL_GET(superg, superg_ioctl_get80211);
847
848static int
849superg_ioctl_set80211(struct ieee80211vap *vap, struct ieee80211req *ireq)
850{
851	switch (ireq->i_type) {
852	case IEEE80211_IOC_FF:
853		if (ireq->i_val) {
854			if ((vap->iv_caps & IEEE80211_C_FF) == 0)
855				return EOPNOTSUPP;
856			vap->iv_flags |= IEEE80211_F_FF;
857		} else
858			vap->iv_flags &= ~IEEE80211_F_FF;
859		return ENETRESET;
860	case IEEE80211_IOC_TURBOP:
861		if (ireq->i_val) {
862			if ((vap->iv_caps & IEEE80211_C_TURBOP) == 0)
863				return EOPNOTSUPP;
864			vap->iv_flags |= IEEE80211_F_TURBOP;
865		} else
866			vap->iv_flags &= ~IEEE80211_F_TURBOP;
867		return ENETRESET;
868	default:
869		return ENOSYS;
870	}
871	return 0;
872}
873IEEE80211_IOCTL_SET(superg, superg_ioctl_set80211);
874