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