10bf6acb6SSam Leffler /*-
20bf6acb6SSam Leffler  * Copyright (c) 2007-2009 Sam Leffler, Errno Consulting
30bf6acb6SSam Leffler  * All rights reserved.
40bf6acb6SSam Leffler  *
50bf6acb6SSam Leffler  * Redistribution and use in source and binary forms, with or without
60bf6acb6SSam Leffler  * modification, are permitted provided that the following conditions
70bf6acb6SSam Leffler  * are met:
80bf6acb6SSam Leffler  * 1. Redistributions of source code must retain the above copyright
90bf6acb6SSam Leffler  *    notice, this list of conditions and the following disclaimer.
100bf6acb6SSam Leffler  * 2. Redistributions in binary form must reproduce the above copyright
110bf6acb6SSam Leffler  *    notice, this list of conditions and the following disclaimer in the
120bf6acb6SSam Leffler  *    documentation and/or other materials provided with the distribution.
130bf6acb6SSam Leffler  *
140bf6acb6SSam Leffler  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
150bf6acb6SSam Leffler  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
160bf6acb6SSam Leffler  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
170bf6acb6SSam Leffler  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
180bf6acb6SSam Leffler  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
190bf6acb6SSam Leffler  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
200bf6acb6SSam Leffler  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
210bf6acb6SSam Leffler  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
220bf6acb6SSam Leffler  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
230bf6acb6SSam Leffler  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
240bf6acb6SSam Leffler  */
250bf6acb6SSam Leffler 
260bf6acb6SSam Leffler #include <sys/cdefs.h>
270bf6acb6SSam Leffler /*
280bf6acb6SSam Leffler  * IEEE 802.11 PHY-related support.
290bf6acb6SSam Leffler  */
300bf6acb6SSam Leffler 
310bf6acb6SSam Leffler #include <sys/param.h>
320bf6acb6SSam Leffler 
330bf6acb6SSam Leffler #include <net/if_llc.h>
340bf6acb6SSam Leffler 
350bf6acb6SSam Leffler #include <net80211/_ieee80211.h>
360bf6acb6SSam Leffler #include <net80211/ieee80211.h>
370bf6acb6SSam Leffler 
380bf6acb6SSam Leffler #define	IEEE80211_F_SHPREAMBLE	0x00040000	/* STATUS: use short preamble */
390bf6acb6SSam Leffler 
400bf6acb6SSam Leffler #include <err.h>
410bf6acb6SSam Leffler #include <stdio.h>
420bf6acb6SSam Leffler #include <stdarg.h>
430bf6acb6SSam Leffler #include <stdlib.h>
440bf6acb6SSam Leffler #include <strings.h>
450bf6acb6SSam Leffler #include <unistd.h>
460bf6acb6SSam Leffler 
470bf6acb6SSam Leffler struct ieee80211_rate_table {
480bf6acb6SSam Leffler 	int		rateCount;		/* NB: for proper padding */
490bf6acb6SSam Leffler 	uint8_t		rateCodeToIndex[256];	/* back mapping */
500bf6acb6SSam Leffler 	struct {
510bf6acb6SSam Leffler 		uint8_t		phy;		/* CCK/OFDM/TURBO */
520bf6acb6SSam Leffler 		uint32_t	rateKbps;	/* transfer rate in kbs */
530bf6acb6SSam Leffler 		uint8_t		shortPreamble;	/* mask for enabling short
540bf6acb6SSam Leffler 						 * preamble in CCK rate code */
550bf6acb6SSam Leffler 		uint8_t		dot11Rate;	/* value for supported rates
560bf6acb6SSam Leffler 						 * info element of MLME */
570bf6acb6SSam Leffler 		uint8_t		ctlRateIndex;	/* index of next lower basic
580bf6acb6SSam Leffler 						 * rate; used for dur. calcs */
590bf6acb6SSam Leffler 		uint16_t	lpAckDuration;	/* long preamble ACK dur. */
600bf6acb6SSam Leffler 		uint16_t	spAckDuration;	/* short preamble ACK dur. */
610bf6acb6SSam Leffler 	} info[32];
620bf6acb6SSam Leffler };
630bf6acb6SSam Leffler 
640bf6acb6SSam Leffler uint16_t
650bf6acb6SSam Leffler ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
660bf6acb6SSam Leffler 	uint32_t frameLen, uint16_t rate, int isShortPreamble);
670bf6acb6SSam Leffler 
680bf6acb6SSam Leffler #define	KASSERT(c, msg) do {			\
690bf6acb6SSam Leffler 	if (!(c)) {				\
700bf6acb6SSam Leffler 		printf msg;			\
710bf6acb6SSam Leffler 		putchar('\n');			\
720bf6acb6SSam Leffler 		exit(-1);			\
730bf6acb6SSam Leffler 	}					\
740bf6acb6SSam Leffler } while (0)
750bf6acb6SSam Leffler 
760bf6acb6SSam Leffler static void
panic(const char * fmt,...)770bf6acb6SSam Leffler panic(const char *fmt, ...)
780bf6acb6SSam Leffler {
790bf6acb6SSam Leffler 	va_list ap;
800bf6acb6SSam Leffler 
810bf6acb6SSam Leffler 	va_start(ap, fmt);
820bf6acb6SSam Leffler 	vprintf(fmt, ap);
830bf6acb6SSam Leffler 	va_end(ap);
840bf6acb6SSam Leffler 	exit(-1);
850bf6acb6SSam Leffler }
860bf6acb6SSam Leffler 
870bf6acb6SSam Leffler /* shorthands to compact tables for readability */
880bf6acb6SSam Leffler #define	OFDM	IEEE80211_T_OFDM
890bf6acb6SSam Leffler #define	CCK	IEEE80211_T_CCK
900bf6acb6SSam Leffler #define	TURBO	IEEE80211_T_TURBO
917077d84dSSam Leffler #define	HALF	IEEE80211_T_OFDM_HALF
927077d84dSSam Leffler #define	QUART	IEEE80211_T_OFDM_QUARTER
937077d84dSSam Leffler #define	PBCC	(IEEE80211_T_OFDM_QUARTER+1)		/* XXX */
940bf6acb6SSam Leffler #define	B(r)	(0x80 | r)
950bf6acb6SSam Leffler #define	Mb(x)	(x*1000)
960bf6acb6SSam Leffler 
970bf6acb6SSam Leffler static struct ieee80211_rate_table ieee80211_11b_table = {
980bf6acb6SSam Leffler     .rateCount = 4,		/* XXX no PBCC */
990bf6acb6SSam Leffler     .info = {
1000bf6acb6SSam Leffler /*                                   short            ctrl  */
1010bf6acb6SSam Leffler /*                                Preamble  dot11Rate Rate */
1020bf6acb6SSam Leffler      [0] = { .phy = CCK,     1000,    0x00,      B(2),   0 },/*   1 Mb */
1030bf6acb6SSam Leffler      [1] = { .phy = CCK,     2000,    0x04,      B(4),   1 },/*   2 Mb */
1040bf6acb6SSam Leffler      [2] = { .phy = CCK,     5500,    0x04,     B(11),   1 },/* 5.5 Mb */
1050bf6acb6SSam Leffler      [3] = { .phy = CCK,    11000,    0x04,     B(22),   1 },/*  11 Mb */
1060bf6acb6SSam Leffler      [4] = { .phy = PBCC,   22000,    0x04,        44,   3 } /*  22 Mb */
1070bf6acb6SSam Leffler     },
1080bf6acb6SSam Leffler };
1090bf6acb6SSam Leffler 
1100bf6acb6SSam Leffler static struct ieee80211_rate_table ieee80211_11g_table = {
1110bf6acb6SSam Leffler     .rateCount = 12,
1120bf6acb6SSam Leffler     .info = {
1130bf6acb6SSam Leffler /*                                   short            ctrl  */
1140bf6acb6SSam Leffler /*                                Preamble  dot11Rate Rate */
1150bf6acb6SSam Leffler      [0] = { .phy = CCK,     1000,    0x00,      B(2),   0 },
1160bf6acb6SSam Leffler      [1] = { .phy = CCK,     2000,    0x04,      B(4),   1 },
1170bf6acb6SSam Leffler      [2] = { .phy = CCK,     5500,    0x04,     B(11),   2 },
1180bf6acb6SSam Leffler      [3] = { .phy = CCK,    11000,    0x04,     B(22),   3 },
1190bf6acb6SSam Leffler      [4] = { .phy = OFDM,    6000,    0x00,        12,   4 },
1200bf6acb6SSam Leffler      [5] = { .phy = OFDM,    9000,    0x00,        18,   4 },
1210bf6acb6SSam Leffler      [6] = { .phy = OFDM,   12000,    0x00,        24,   6 },
1220bf6acb6SSam Leffler      [7] = { .phy = OFDM,   18000,    0x00,        36,   6 },
1230bf6acb6SSam Leffler      [8] = { .phy = OFDM,   24000,    0x00,        48,   8 },
1240bf6acb6SSam Leffler      [9] = { .phy = OFDM,   36000,    0x00,        72,   8 },
1250bf6acb6SSam Leffler     [10] = { .phy = OFDM,   48000,    0x00,        96,   8 },
1260bf6acb6SSam Leffler     [11] = { .phy = OFDM,   54000,    0x00,       108,   8 }
1270bf6acb6SSam Leffler     },
1280bf6acb6SSam Leffler };
1290bf6acb6SSam Leffler 
1300bf6acb6SSam Leffler static struct ieee80211_rate_table ieee80211_11a_table = {
1310bf6acb6SSam Leffler     .rateCount = 8,
1320bf6acb6SSam Leffler     .info = {
1330bf6acb6SSam Leffler /*                                   short            ctrl  */
1340bf6acb6SSam Leffler /*                                Preamble  dot11Rate Rate */
1350bf6acb6SSam Leffler      [0] = { .phy = OFDM,    6000,    0x00,     B(12),   0 },
1360bf6acb6SSam Leffler      [1] = { .phy = OFDM,    9000,    0x00,        18,   0 },
1370bf6acb6SSam Leffler      [2] = { .phy = OFDM,   12000,    0x00,     B(24),   2 },
1380bf6acb6SSam Leffler      [3] = { .phy = OFDM,   18000,    0x00,        36,   2 },
1390bf6acb6SSam Leffler      [4] = { .phy = OFDM,   24000,    0x00,     B(48),   4 },
1400bf6acb6SSam Leffler      [5] = { .phy = OFDM,   36000,    0x00,        72,   4 },
1410bf6acb6SSam Leffler      [6] = { .phy = OFDM,   48000,    0x00,        96,   4 },
1420bf6acb6SSam Leffler      [7] = { .phy = OFDM,   54000,    0x00,       108,   4 }
1430bf6acb6SSam Leffler     },
1440bf6acb6SSam Leffler };
1450bf6acb6SSam Leffler 
1460bf6acb6SSam Leffler static struct ieee80211_rate_table ieee80211_half_table = {
1470bf6acb6SSam Leffler     .rateCount = 8,
1480bf6acb6SSam Leffler     .info = {
1490bf6acb6SSam Leffler /*                                   short            ctrl  */
1500bf6acb6SSam Leffler /*                                Preamble  dot11Rate Rate */
1517077d84dSSam Leffler      [0] = { .phy = HALF,    3000,    0x00,      B(6),   0 },
1527077d84dSSam Leffler      [1] = { .phy = HALF,    4500,    0x00,         9,   0 },
1537077d84dSSam Leffler      [2] = { .phy = HALF,    6000,    0x00,     B(12),   2 },
1547077d84dSSam Leffler      [3] = { .phy = HALF,    9000,    0x00,        18,   2 },
1557077d84dSSam Leffler      [4] = { .phy = HALF,   12000,    0x00,     B(24),   4 },
1567077d84dSSam Leffler      [5] = { .phy = HALF,   18000,    0x00,        36,   4 },
1577077d84dSSam Leffler      [6] = { .phy = HALF,   24000,    0x00,        48,   4 },
1587077d84dSSam Leffler      [7] = { .phy = HALF,   27000,    0x00,        54,   4 }
1590bf6acb6SSam Leffler     },
1600bf6acb6SSam Leffler };
1610bf6acb6SSam Leffler 
1620bf6acb6SSam Leffler static struct ieee80211_rate_table ieee80211_quarter_table = {
1630bf6acb6SSam Leffler     .rateCount = 8,
1640bf6acb6SSam Leffler     .info = {
1650bf6acb6SSam Leffler /*                                   short            ctrl  */
1660bf6acb6SSam Leffler /*                                Preamble  dot11Rate Rate */
1677077d84dSSam Leffler      [0] = { .phy = QUART,   1500,    0x00,      B(3),   0 },
1687077d84dSSam Leffler      [1] = { .phy = QUART,   2250,    0x00,         4,   0 },
1697077d84dSSam Leffler      [2] = { .phy = QUART,   3000,    0x00,      B(9),   2 },
1707077d84dSSam Leffler      [3] = { .phy = QUART,   4500,    0x00,         9,   2 },
1717077d84dSSam Leffler      [4] = { .phy = QUART,   6000,    0x00,     B(12),   4 },
1727077d84dSSam Leffler      [5] = { .phy = QUART,   9000,    0x00,        18,   4 },
1737077d84dSSam Leffler      [6] = { .phy = QUART,  12000,    0x00,        24,   4 },
1747077d84dSSam Leffler      [7] = { .phy = QUART,  13500,    0x00,        27,   4 }
1750bf6acb6SSam Leffler     },
1760bf6acb6SSam Leffler };
1770bf6acb6SSam Leffler 
1780bf6acb6SSam Leffler static struct ieee80211_rate_table ieee80211_turbog_table = {
1790bf6acb6SSam Leffler     .rateCount = 7,
1800bf6acb6SSam Leffler     .info = {
1810bf6acb6SSam Leffler /*                                   short            ctrl  */
1820bf6acb6SSam Leffler /*                                Preamble  dot11Rate Rate */
1830bf6acb6SSam Leffler      [0] = { .phy = TURBO,   12000,   0x00,     B(12),   0 },
1840bf6acb6SSam Leffler      [1] = { .phy = TURBO,   24000,   0x00,     B(24),   1 },
1850bf6acb6SSam Leffler      [2] = { .phy = TURBO,   36000,   0x00,        36,   1 },
1860bf6acb6SSam Leffler      [3] = { .phy = TURBO,   48000,   0x00,     B(48),   3 },
1870bf6acb6SSam Leffler      [4] = { .phy = TURBO,   72000,   0x00,        72,   3 },
1880bf6acb6SSam Leffler      [5] = { .phy = TURBO,   96000,   0x00,        96,   3 },
1890bf6acb6SSam Leffler      [6] = { .phy = TURBO,  108000,   0x00,       108,   3 }
1900bf6acb6SSam Leffler     },
1910bf6acb6SSam Leffler };
1920bf6acb6SSam Leffler 
1930bf6acb6SSam Leffler static struct ieee80211_rate_table ieee80211_turboa_table = {
1940bf6acb6SSam Leffler     .rateCount = 8,
1950bf6acb6SSam Leffler     .info = {
1960bf6acb6SSam Leffler /*                                   short            ctrl  */
1970bf6acb6SSam Leffler /*                                Preamble  dot11Rate Rate */
1980bf6acb6SSam Leffler      [0] = { .phy = TURBO,   12000,   0x00,     B(12),   0 },
1990bf6acb6SSam Leffler      [1] = { .phy = TURBO,   18000,   0x00,        18,   0 },
2000bf6acb6SSam Leffler      [2] = { .phy = TURBO,   24000,   0x00,     B(24),   2 },
2010bf6acb6SSam Leffler      [3] = { .phy = TURBO,   36000,   0x00,        36,   2 },
2020bf6acb6SSam Leffler      [4] = { .phy = TURBO,   48000,   0x00,     B(48),   4 },
2030bf6acb6SSam Leffler      [5] = { .phy = TURBO,   72000,   0x00,        72,   4 },
2040bf6acb6SSam Leffler      [6] = { .phy = TURBO,   96000,   0x00,        96,   4 },
2050bf6acb6SSam Leffler      [7] = { .phy = TURBO,  108000,   0x00,       108,   4 }
2060bf6acb6SSam Leffler     },
2070bf6acb6SSam Leffler };
2080bf6acb6SSam Leffler 
2090bf6acb6SSam Leffler #undef	Mb
2100bf6acb6SSam Leffler #undef	B
2110bf6acb6SSam Leffler #undef	OFDM
2120bf6acb6SSam Leffler #undef	CCK
2130bf6acb6SSam Leffler #undef	TURBO
2140bf6acb6SSam Leffler #undef	XR
2150bf6acb6SSam Leffler 
2160bf6acb6SSam Leffler /*
2170bf6acb6SSam Leffler  * Setup a rate table's reverse lookup table and fill in
2180bf6acb6SSam Leffler  * ack durations.  The reverse lookup tables are assumed
2190bf6acb6SSam Leffler  * to be initialized to zero (or at least the first entry).
2200bf6acb6SSam Leffler  * We use this as a key that indicates whether or not
2210bf6acb6SSam Leffler  * we've previously setup the reverse lookup table.
2220bf6acb6SSam Leffler  *
2230bf6acb6SSam Leffler  * XXX not reentrant, but shouldn't matter
2240bf6acb6SSam Leffler  */
2250bf6acb6SSam Leffler static void
ieee80211_setup_ratetable(struct ieee80211_rate_table * rt)2260bf6acb6SSam Leffler ieee80211_setup_ratetable(struct ieee80211_rate_table *rt)
2270bf6acb6SSam Leffler {
2280bf6acb6SSam Leffler #define	WLAN_CTRL_FRAME_SIZE \
2290bf6acb6SSam Leffler 	(sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
2300bf6acb6SSam Leffler 
2310bf6acb6SSam Leffler 	int i;
2320bf6acb6SSam Leffler 
23360caf0c9SCraig Rodrigues 	for (i = 0; i < nitems(rt->rateCodeToIndex); i++)
2340bf6acb6SSam Leffler 		rt->rateCodeToIndex[i] = (uint8_t) -1;
2350bf6acb6SSam Leffler 	for (i = 0; i < rt->rateCount; i++) {
2360bf6acb6SSam Leffler 		uint8_t code = rt->info[i].dot11Rate;
2370bf6acb6SSam Leffler 		uint8_t cix = rt->info[i].ctlRateIndex;
2380bf6acb6SSam Leffler 		uint8_t ctl_rate = rt->info[cix].dot11Rate;
2390bf6acb6SSam Leffler 
2400bf6acb6SSam Leffler 		rt->rateCodeToIndex[code] = i;
2410bf6acb6SSam Leffler 		if (code & IEEE80211_RATE_BASIC) {
2420bf6acb6SSam Leffler 			/*
2430bf6acb6SSam Leffler 			 * Map w/o basic rate bit too.
2440bf6acb6SSam Leffler 			 */
2450bf6acb6SSam Leffler 			code &= IEEE80211_RATE_VAL;
2460bf6acb6SSam Leffler 			rt->rateCodeToIndex[code] = i;
2470bf6acb6SSam Leffler 		}
2480bf6acb6SSam Leffler 
2490bf6acb6SSam Leffler 		/*
2500bf6acb6SSam Leffler 		 * XXX for 11g the control rate to use for 5.5 and 11 Mb/s
2510bf6acb6SSam Leffler 		 *     depends on whether they are marked as basic rates;
2520bf6acb6SSam Leffler 		 *     the static tables are setup with an 11b-compatible
2530bf6acb6SSam Leffler 		 *     2Mb/s rate which will work but is suboptimal
2540bf6acb6SSam Leffler 		 *
2550bf6acb6SSam Leffler 		 * NB: Control rate is always less than or equal to the
2560bf6acb6SSam Leffler 		 *     current rate, so control rate's reverse lookup entry
2570bf6acb6SSam Leffler 		 *     has been installed and following call is safe.
2580bf6acb6SSam Leffler 		 */
2590bf6acb6SSam Leffler 		rt->info[i].lpAckDuration = ieee80211_compute_duration(rt,
2600bf6acb6SSam Leffler 			WLAN_CTRL_FRAME_SIZE, ctl_rate, 0);
2610bf6acb6SSam Leffler 		rt->info[i].spAckDuration = ieee80211_compute_duration(rt,
2620bf6acb6SSam Leffler 			WLAN_CTRL_FRAME_SIZE, ctl_rate, IEEE80211_F_SHPREAMBLE);
2630bf6acb6SSam Leffler 	}
2640bf6acb6SSam Leffler 
2650bf6acb6SSam Leffler #undef WLAN_CTRL_FRAME_SIZE
2660bf6acb6SSam Leffler }
2670bf6acb6SSam Leffler 
2680bf6acb6SSam Leffler /* Setup all rate tables */
2690bf6acb6SSam Leffler static void
ieee80211_phy_init(void)2700bf6acb6SSam Leffler ieee80211_phy_init(void)
2710bf6acb6SSam Leffler {
2720bf6acb6SSam Leffler 	static struct ieee80211_rate_table * const ratetables[] = {
2730bf6acb6SSam Leffler 		&ieee80211_half_table,
2740bf6acb6SSam Leffler 		&ieee80211_quarter_table,
2750bf6acb6SSam Leffler 		&ieee80211_11a_table,
2760bf6acb6SSam Leffler 		&ieee80211_11g_table,
2770bf6acb6SSam Leffler 		&ieee80211_turbog_table,
2780bf6acb6SSam Leffler 		&ieee80211_turboa_table,
2790bf6acb6SSam Leffler 		&ieee80211_turboa_table,
2800bf6acb6SSam Leffler 		&ieee80211_11a_table,
2810bf6acb6SSam Leffler 		&ieee80211_11g_table,
2820bf6acb6SSam Leffler 		&ieee80211_11b_table
2830bf6acb6SSam Leffler 	};
28460caf0c9SCraig Rodrigues 	unsigned int i;
2850bf6acb6SSam Leffler 
28660caf0c9SCraig Rodrigues 	for (i = 0; i < nitems(ratetables); ++i)
2870bf6acb6SSam Leffler 		ieee80211_setup_ratetable(ratetables[i]);
2880bf6acb6SSam Leffler 
2890bf6acb6SSam Leffler }
2907077d84dSSam Leffler #define CCK_SIFS_TIME		10
2917077d84dSSam Leffler #define CCK_PREAMBLE_BITS	144
2927077d84dSSam Leffler #define CCK_PLCP_BITS		48
2937077d84dSSam Leffler 
2947077d84dSSam Leffler #define OFDM_SIFS_TIME		16
2957077d84dSSam Leffler #define OFDM_PREAMBLE_TIME	20
2967077d84dSSam Leffler #define OFDM_PLCP_BITS		22
2977077d84dSSam Leffler #define OFDM_SYMBOL_TIME	4
2987077d84dSSam Leffler 
2997077d84dSSam Leffler #define OFDM_HALF_SIFS_TIME	32
3007077d84dSSam Leffler #define OFDM_HALF_PREAMBLE_TIME	40
3017077d84dSSam Leffler #define OFDM_HALF_PLCP_BITS	22
3027077d84dSSam Leffler #define OFDM_HALF_SYMBOL_TIME	8
3037077d84dSSam Leffler 
3047077d84dSSam Leffler #define OFDM_QUARTER_SIFS_TIME 		64
3057077d84dSSam Leffler #define OFDM_QUARTER_PREAMBLE_TIME	80
3067077d84dSSam Leffler #define OFDM_QUARTER_PLCP_BITS		22
3077077d84dSSam Leffler #define OFDM_QUARTER_SYMBOL_TIME	16
3087077d84dSSam Leffler 
3097077d84dSSam Leffler #define TURBO_SIFS_TIME		8
3107077d84dSSam Leffler #define TURBO_PREAMBLE_TIME	14
3117077d84dSSam Leffler #define TURBO_PLCP_BITS		22
3127077d84dSSam Leffler #define TURBO_SYMBOL_TIME	4
3137077d84dSSam Leffler 
3147077d84dSSam Leffler #define	HT_L_STF	8
3157077d84dSSam Leffler #define	HT_L_LTF	8
3167077d84dSSam Leffler #define	HT_L_SIG	4
3177077d84dSSam Leffler #define	HT_SIG		8
3187077d84dSSam Leffler #define	HT_STF		4
3197077d84dSSam Leffler #define	HT_LTF(n)	((n) * 4)
3200bf6acb6SSam Leffler 
3210bf6acb6SSam Leffler /*
3220bf6acb6SSam Leffler  * Compute the time to transmit a frame of length frameLen bytes
3230bf6acb6SSam Leffler  * using the specified rate, phy, and short preamble setting.
3240bf6acb6SSam Leffler  * SIFS is included.
3250bf6acb6SSam Leffler  */
3260bf6acb6SSam Leffler uint16_t
ieee80211_compute_duration(const struct ieee80211_rate_table * rt,uint32_t frameLen,uint16_t rate,int isShortPreamble)3270bf6acb6SSam Leffler ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
3280bf6acb6SSam Leffler 	uint32_t frameLen, uint16_t rate, int isShortPreamble)
3290bf6acb6SSam Leffler {
3300bf6acb6SSam Leffler 	uint8_t rix = rt->rateCodeToIndex[rate];
3310bf6acb6SSam Leffler 	uint32_t bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
3320bf6acb6SSam Leffler 	uint32_t kbps;
3330bf6acb6SSam Leffler 
3340bf6acb6SSam Leffler 	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
3350bf6acb6SSam Leffler 	kbps = rt->info[rix].rateKbps;
3360bf6acb6SSam Leffler 	if (kbps == 0)			/* XXX bandaid for channel changes */
3370bf6acb6SSam Leffler 		return 0;
3380bf6acb6SSam Leffler 
3390bf6acb6SSam Leffler 	switch (rt->info[rix].phy) {
3400bf6acb6SSam Leffler 	case IEEE80211_T_CCK:
3410bf6acb6SSam Leffler 		phyTime		= CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
3420bf6acb6SSam Leffler 		if (isShortPreamble && rt->info[rix].shortPreamble)
3430bf6acb6SSam Leffler 			phyTime >>= 1;
3440bf6acb6SSam Leffler 		numBits		= frameLen << 3;
3450bf6acb6SSam Leffler 		txTime		= CCK_SIFS_TIME + phyTime
3460bf6acb6SSam Leffler 				+ ((numBits * 1000)/kbps);
3470bf6acb6SSam Leffler 		break;
3480bf6acb6SSam Leffler 	case IEEE80211_T_OFDM:
3490bf6acb6SSam Leffler 		bitsPerSymbol	= (kbps * OFDM_SYMBOL_TIME) / 1000;
3500bf6acb6SSam Leffler 		KASSERT(bitsPerSymbol != 0, ("full rate bps"));
3510bf6acb6SSam Leffler 
3520bf6acb6SSam Leffler 		numBits		= OFDM_PLCP_BITS + (frameLen << 3);
3530bf6acb6SSam Leffler 		numSymbols	= howmany(numBits, bitsPerSymbol);
3540bf6acb6SSam Leffler 		txTime		= OFDM_SIFS_TIME
3550bf6acb6SSam Leffler 				+ OFDM_PREAMBLE_TIME
3560bf6acb6SSam Leffler 				+ (numSymbols * OFDM_SYMBOL_TIME);
3570bf6acb6SSam Leffler 		break;
3587077d84dSSam Leffler 	case IEEE80211_T_OFDM_HALF:
3597077d84dSSam Leffler 		bitsPerSymbol	= (kbps * OFDM_HALF_SYMBOL_TIME) / 1000;
3607077d84dSSam Leffler 		KASSERT(bitsPerSymbol != 0, ("1/4 rate bps"));
3610bf6acb6SSam Leffler 
3627077d84dSSam Leffler 		numBits		= OFDM_PLCP_BITS + (frameLen << 3);
3637077d84dSSam Leffler 		numSymbols	= howmany(numBits, bitsPerSymbol);
3647077d84dSSam Leffler 		txTime		= OFDM_HALF_SIFS_TIME
3657077d84dSSam Leffler 				+ OFDM_HALF_PREAMBLE_TIME
3667077d84dSSam Leffler 				+ (numSymbols * OFDM_HALF_SYMBOL_TIME);
3677077d84dSSam Leffler 		break;
3687077d84dSSam Leffler 	case IEEE80211_T_OFDM_QUARTER:
3697077d84dSSam Leffler 		bitsPerSymbol	= (kbps * OFDM_QUARTER_SYMBOL_TIME) / 1000;
3707077d84dSSam Leffler 		KASSERT(bitsPerSymbol != 0, ("1/2 rate bps"));
3710bf6acb6SSam Leffler 
3727077d84dSSam Leffler 		numBits		= OFDM_PLCP_BITS + (frameLen << 3);
3737077d84dSSam Leffler 		numSymbols	= howmany(numBits, bitsPerSymbol);
3747077d84dSSam Leffler 		txTime		= OFDM_QUARTER_SIFS_TIME
3757077d84dSSam Leffler 				+ OFDM_QUARTER_PREAMBLE_TIME
3767077d84dSSam Leffler 				+ (numSymbols * OFDM_QUARTER_SYMBOL_TIME);
3777077d84dSSam Leffler 		break;
3780bf6acb6SSam Leffler 	case IEEE80211_T_TURBO:
3790bf6acb6SSam Leffler 		/* we still save OFDM rates in kbps - so double them */
3800bf6acb6SSam Leffler 		bitsPerSymbol = ((kbps << 1) * TURBO_SYMBOL_TIME) / 1000;
3810bf6acb6SSam Leffler 		KASSERT(bitsPerSymbol != 0, ("turbo bps"));
3820bf6acb6SSam Leffler 
3830bf6acb6SSam Leffler 		numBits       = TURBO_PLCP_BITS + (frameLen << 3);
3840bf6acb6SSam Leffler 		numSymbols    = howmany(numBits, bitsPerSymbol);
3850bf6acb6SSam Leffler 		txTime        = TURBO_SIFS_TIME + TURBO_PREAMBLE_TIME
3860bf6acb6SSam Leffler 			      + (numSymbols * TURBO_SYMBOL_TIME);
3870bf6acb6SSam Leffler 		break;
3880bf6acb6SSam Leffler 	default:
3890bf6acb6SSam Leffler 		panic("%s: unknown phy %u (rate %u)\n", __func__,
3900bf6acb6SSam Leffler 		      rt->info[rix].phy, rate);
3910bf6acb6SSam Leffler 		break;
3920bf6acb6SSam Leffler 	}
3930bf6acb6SSam Leffler 	return txTime;
3940bf6acb6SSam Leffler }
3950bf6acb6SSam Leffler 
3960bf6acb6SSam Leffler uint32_t
ieee80211_compute_duration_ht(const struct ieee80211_rate_table * rt,uint32_t frameLen,uint16_t rate,int streams,int isht40,int isShortGI)3970bf6acb6SSam Leffler ieee80211_compute_duration_ht(const struct ieee80211_rate_table *rt,
3980bf6acb6SSam Leffler 	uint32_t frameLen, uint16_t rate,
3990bf6acb6SSam Leffler 	int streams, int isht40, int isShortGI)
4000bf6acb6SSam Leffler {
4010bf6acb6SSam Leffler 	static const uint16_t ht20_bps[16] = {
4020bf6acb6SSam Leffler 	    26, 52, 78, 104, 156, 208, 234, 260,
4030bf6acb6SSam Leffler 	    52, 104, 156, 208, 312, 416, 468, 520
4040bf6acb6SSam Leffler 	};
4050bf6acb6SSam Leffler 	static const uint16_t ht40_bps[16] = {
4060bf6acb6SSam Leffler 	    54, 108, 162, 216, 324, 432, 486, 540,
4070bf6acb6SSam Leffler 	    108, 216, 324, 432, 648, 864, 972, 1080,
4080bf6acb6SSam Leffler 	};
4090bf6acb6SSam Leffler 	uint32_t bitsPerSymbol, numBits, numSymbols, txTime;
4100bf6acb6SSam Leffler 
4110bf6acb6SSam Leffler 	KASSERT(rate & IEEE80211_RATE_MCS, ("not mcs %d", rate));
4120bf6acb6SSam Leffler 	KASSERT((rate &~ IEEE80211_RATE_MCS) < 16, ("bad mcs 0x%x", rate));
4130bf6acb6SSam Leffler 
4140bf6acb6SSam Leffler 	if (isht40)
4150bf6acb6SSam Leffler 		bitsPerSymbol = ht40_bps[rate & 0xf];
4160bf6acb6SSam Leffler 	else
4170bf6acb6SSam Leffler 		bitsPerSymbol = ht20_bps[rate & 0xf];
4180bf6acb6SSam Leffler 	numBits = OFDM_PLCP_BITS + (frameLen << 3);
4190bf6acb6SSam Leffler 	numSymbols = howmany(numBits, bitsPerSymbol);
4200bf6acb6SSam Leffler 	if (isShortGI)
4210bf6acb6SSam Leffler 		txTime = ((numSymbols * 18) + 4) / 5;	/* 3.6us */
4220bf6acb6SSam Leffler 	else
4230bf6acb6SSam Leffler 		txTime = numSymbols * 4;		/* 4us */
4240bf6acb6SSam Leffler 	return txTime + HT_L_STF + HT_L_LTF +
4250bf6acb6SSam Leffler 	    HT_L_SIG + HT_SIG + HT_STF + HT_LTF(streams);
4260bf6acb6SSam Leffler }
4270bf6acb6SSam Leffler 
4280bf6acb6SSam Leffler static const struct ieee80211_rate_table *
mode2table(const char * mode)4290bf6acb6SSam Leffler mode2table(const char *mode)
4300bf6acb6SSam Leffler {
4310bf6acb6SSam Leffler 	if (strcasecmp(mode, "half") == 0)
4320bf6acb6SSam Leffler 		return &ieee80211_half_table;
4330bf6acb6SSam Leffler 	else if (strcasecmp(mode, "quarter") == 0)
4340bf6acb6SSam Leffler 		return &ieee80211_quarter_table;
4350bf6acb6SSam Leffler 	else if (strcasecmp(mode, "hta") == 0)
4360bf6acb6SSam Leffler 		return &ieee80211_11a_table;	/* XXX */
4370bf6acb6SSam Leffler 	else if (strcasecmp(mode, "htg") == 0)
4380bf6acb6SSam Leffler 		return &ieee80211_11g_table;	/* XXX */
4390bf6acb6SSam Leffler 	else if (strcasecmp(mode, "108g") == 0)
4400bf6acb6SSam Leffler 		return &ieee80211_turbog_table;
4410bf6acb6SSam Leffler 	else if (strcasecmp(mode, "sturbo") == 0)
4420bf6acb6SSam Leffler 		return &ieee80211_turboa_table;
4430bf6acb6SSam Leffler 	else if (strcasecmp(mode, "turbo") == 0)
4440bf6acb6SSam Leffler 		return &ieee80211_turboa_table;
4450bf6acb6SSam Leffler 	else if (strcasecmp(mode, "11a") == 0)
4460bf6acb6SSam Leffler 		return &ieee80211_11a_table;
4470bf6acb6SSam Leffler 	else if (strcasecmp(mode, "11g") == 0)
4480bf6acb6SSam Leffler 		return &ieee80211_11g_table;
4490bf6acb6SSam Leffler 	else if (strcasecmp(mode, "11b") == 0)
4500bf6acb6SSam Leffler 		return &ieee80211_11b_table;
4510bf6acb6SSam Leffler 	else
4520bf6acb6SSam Leffler 		return NULL;
4530bf6acb6SSam Leffler }
4540bf6acb6SSam Leffler 
4550bf6acb6SSam Leffler const char *
srate(int rate)4560bf6acb6SSam Leffler srate(int rate)
4570bf6acb6SSam Leffler {
4580bf6acb6SSam Leffler 	static char buf[32];
4590bf6acb6SSam Leffler 	if (rate & 1)
4600bf6acb6SSam Leffler 		snprintf(buf, sizeof(buf), "%u.5", rate/2);
4610bf6acb6SSam Leffler 	else
4620bf6acb6SSam Leffler 		snprintf(buf, sizeof(buf), "%u", rate/2);
4630bf6acb6SSam Leffler 	return buf;
4640bf6acb6SSam Leffler }
4650bf6acb6SSam Leffler 
4660bf6acb6SSam Leffler static int
checkpreamble(const struct ieee80211_rate_table * rt,uint8_t rix,int isShortPreamble,int verbose)4670bf6acb6SSam Leffler checkpreamble(const struct ieee80211_rate_table *rt, uint8_t rix,
4680bf6acb6SSam Leffler 	int isShortPreamble, int verbose)
4690bf6acb6SSam Leffler {
4700bf6acb6SSam Leffler 	if (isShortPreamble) {
4710bf6acb6SSam Leffler 		if (rt->info[rix].phy != IEEE80211_T_CCK) {
4720bf6acb6SSam Leffler 			if (verbose)
4730bf6acb6SSam Leffler 				warnx("short preamble not meaningful, ignored");
4740bf6acb6SSam Leffler 			isShortPreamble = 0;
4750bf6acb6SSam Leffler 		} else if (!rt->info[rix].shortPreamble) {
4760bf6acb6SSam Leffler 			if (verbose)
4770bf6acb6SSam Leffler 				warnx("short preamble not meaningful with "
4780bf6acb6SSam Leffler 				    "rate %s, ignored",
4790bf6acb6SSam Leffler 				    srate(rt->info[rix].dot11Rate &~ IEEE80211_RATE_BASIC));
4800bf6acb6SSam Leffler 			isShortPreamble = 0;
4810bf6acb6SSam Leffler 		}
4820bf6acb6SSam Leffler 	}
4830bf6acb6SSam Leffler 	return isShortPreamble;
4840bf6acb6SSam Leffler }
4850bf6acb6SSam Leffler 
4860bf6acb6SSam Leffler static void
usage(const char * progname)4870bf6acb6SSam Leffler usage(const char *progname)
4880bf6acb6SSam Leffler {
4890bf6acb6SSam Leffler 	fprintf(stderr, "usage: %s [-a] [-l framelen] [-m mode] [-r rate] [-s]\n",
4900bf6acb6SSam Leffler 	    progname);
4910bf6acb6SSam Leffler 	fprintf(stderr, "-a             display calculations for all possible rates\n");
4920bf6acb6SSam Leffler 	fprintf(stderr, "-l framelen    length in bytes of 802.11 payload (default 1536)\n");
4930bf6acb6SSam Leffler 	fprintf(stderr, "-m 11a         calculate for 11a channel\n");
4940bf6acb6SSam Leffler 	fprintf(stderr, "-m 11b         calculate for 11b channel\n");
4950bf6acb6SSam Leffler 	fprintf(stderr, "-m 11g         calculate for 11g channel (default)\n");
4960bf6acb6SSam Leffler 	fprintf(stderr, "-m half        calculate for 1/2 width channel\n");
4970bf6acb6SSam Leffler 	fprintf(stderr, "-m quarter     calculate for 1/4 width channel\n");
4980bf6acb6SSam Leffler 	fprintf(stderr, "-m 108g        calculate for dynamic turbo 11g channel\n");
4990bf6acb6SSam Leffler 	fprintf(stderr, "-m sturbo      calculate for static turbo channel\n");
5000bf6acb6SSam Leffler 	fprintf(stderr, "-m turbo       calculate for dynamic turbo 11a channel\n");
5010bf6acb6SSam Leffler 	fprintf(stderr, "-r rate        IEEE rate code (default 54)\n");
5020bf6acb6SSam Leffler 	fprintf(stderr, "-s             short preamble (default long)\n");
5030bf6acb6SSam Leffler 	exit(0);
5040bf6acb6SSam Leffler }
5050bf6acb6SSam Leffler 
5060bf6acb6SSam Leffler int
main(int argc,char * argv[])5070bf6acb6SSam Leffler main(int argc, char *argv[])
5080bf6acb6SSam Leffler {
5090bf6acb6SSam Leffler 	const struct ieee80211_rate_table *rt;
5100bf6acb6SSam Leffler 	const char *mode;
5110bf6acb6SSam Leffler 	uint32_t frameLen;
5120bf6acb6SSam Leffler 	uint16_t rate;
5130bf6acb6SSam Leffler 	uint16_t time;
5140bf6acb6SSam Leffler 	uint8_t rix;
5150bf6acb6SSam Leffler 	int ch, allrates, isShortPreamble, isShort;
5160bf6acb6SSam Leffler 	float frate;
5170bf6acb6SSam Leffler 
5180bf6acb6SSam Leffler 	ieee80211_phy_init();
5190bf6acb6SSam Leffler 
5200bf6acb6SSam Leffler 	mode = "11g";
5210bf6acb6SSam Leffler 	isShortPreamble = 0;
5220bf6acb6SSam Leffler 	frameLen = 1500
5230bf6acb6SSam Leffler 		 + sizeof(struct ieee80211_frame)
5240bf6acb6SSam Leffler 		 + LLC_SNAPFRAMELEN
5250bf6acb6SSam Leffler 		 + IEEE80211_CRC_LEN
5260bf6acb6SSam Leffler 		 ;
5270bf6acb6SSam Leffler 	rate = 2*54;
5280bf6acb6SSam Leffler 	allrates = 0;
5290bf6acb6SSam Leffler 	while ((ch = getopt(argc, argv, "al:m:r:s")) != -1) {
5300bf6acb6SSam Leffler 		switch (ch) {
5310bf6acb6SSam Leffler 		case 'a':
5320bf6acb6SSam Leffler 			allrates = 1;
5330bf6acb6SSam Leffler 			break;
5340bf6acb6SSam Leffler 		case 'l':
5350bf6acb6SSam Leffler 			frameLen = strtoul(optarg, NULL, 0);
5360bf6acb6SSam Leffler 			break;
5370bf6acb6SSam Leffler 		case 'm':
5380bf6acb6SSam Leffler 			mode = optarg;
5390bf6acb6SSam Leffler 			break;
5400bf6acb6SSam Leffler 		case 'r':
5410bf6acb6SSam Leffler 			frate = atof(optarg);
5420bf6acb6SSam Leffler 			rate = (int) 2*frate;
5430bf6acb6SSam Leffler 			break;
5440bf6acb6SSam Leffler 		case 's':
5450bf6acb6SSam Leffler 			isShortPreamble = 1;
5460bf6acb6SSam Leffler 			break;
5470bf6acb6SSam Leffler 		default:
5480bf6acb6SSam Leffler 			usage(argv[0]);
5490bf6acb6SSam Leffler 			break;
5500bf6acb6SSam Leffler 		}
5510bf6acb6SSam Leffler 	}
5520bf6acb6SSam Leffler 	rt = mode2table(mode);
5530bf6acb6SSam Leffler 	if (rt == NULL)
5540bf6acb6SSam Leffler 		errx(-1, "unknown mode %s", mode);
5550bf6acb6SSam Leffler 	if (!allrates) {
5560bf6acb6SSam Leffler 		rix = rt->rateCodeToIndex[rate];
5570bf6acb6SSam Leffler 		if (rix == (uint8_t) -1)
5580bf6acb6SSam Leffler 			errx(-1, "rate %s not valid for mode %s", srate(rate), mode);
5590bf6acb6SSam Leffler 		isShort = checkpreamble(rt, rix, isShortPreamble, 1);
5600bf6acb6SSam Leffler 
5610bf6acb6SSam Leffler 		time = ieee80211_compute_duration(rt, frameLen, rate, isShort);
5620bf6acb6SSam Leffler 		printf("%u usec to send %u bytes @ %s Mb/s, %s preamble\n",
5630bf6acb6SSam Leffler 		    time, frameLen, srate(rate),
5640bf6acb6SSam Leffler 		    isShort ? "short" : "long");
5650bf6acb6SSam Leffler 	} else {
5660bf6acb6SSam Leffler 		for (rix = 0; rix < rt->rateCount; rix++) {
5670bf6acb6SSam Leffler 			rate = rt->info[rix].dot11Rate &~ IEEE80211_RATE_BASIC;
5680bf6acb6SSam Leffler 			isShort = checkpreamble(rt, rix, isShortPreamble, 0);
5690bf6acb6SSam Leffler 			time = ieee80211_compute_duration(rt, frameLen, rate,
5700bf6acb6SSam Leffler 			    isShort);
5710bf6acb6SSam Leffler 			printf("%u usec to send %u bytes @ %s Mb/s, %s preamble\n",
5720bf6acb6SSam Leffler 			    time, frameLen, srate(rate),
5730bf6acb6SSam Leffler 			    isShort ? "short" : "long");
5740bf6acb6SSam Leffler 		}
5750bf6acb6SSam Leffler 	}
5760bf6acb6SSam Leffler 	return 0;
5770bf6acb6SSam Leffler }
578