1178354Ssam/*-
2178354Ssam * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3178354Ssam * All rights reserved.
4178354Ssam *
5178354Ssam * Redistribution and use in source and binary forms, with or without
6178354Ssam * modification, are permitted provided that the following conditions
7178354Ssam * are met:
8178354Ssam * 1. Redistributions of source code must retain the above copyright
9178354Ssam *    notice, this list of conditions and the following disclaimer.
10178354Ssam * 2. Redistributions in binary form must reproduce the above copyright
11178354Ssam *    notice, this list of conditions and the following disclaimer in the
12178354Ssam *    documentation and/or other materials provided with the distribution.
13178354Ssam *
14178354Ssam * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15178354Ssam * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16178354Ssam * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17178354Ssam * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18178354Ssam * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19178354Ssam * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20178354Ssam * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21178354Ssam * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22178354Ssam * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23178354Ssam * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24178354Ssam *
25178354Ssam * $FreeBSD$
26178354Ssam */
27178354Ssam
28178354Ssam#ifndef _NET80211_IEEE80211_PHY_H_
29178354Ssam#define _NET80211_IEEE80211_PHY_H_
30178354Ssam
31178354Ssam#ifdef _KERNEL
32178354Ssam/*
33178354Ssam * IEEE 802.11 PHY-related definitions.
34178354Ssam */
35178354Ssam
36178354Ssam/*
37178354Ssam * Contention window (slots).
38178354Ssam */
39178354Ssam#define IEEE80211_CW_MAX	1023	/* aCWmax */
40178354Ssam#define IEEE80211_CW_MIN_0	31	/* DS/CCK aCWmin, ERP aCWmin(0) */
41178354Ssam#define IEEE80211_CW_MIN_1	15	/* OFDM aCWmin, ERP aCWmin(1) */
42178354Ssam
43178354Ssam/*
44178354Ssam * SIFS (microseconds).
45178354Ssam */
46178354Ssam#define IEEE80211_DUR_SIFS	10	/* DS/CCK/ERP SIFS */
47178354Ssam#define IEEE80211_DUR_OFDM_SIFS	16	/* OFDM SIFS */
48178354Ssam
49178354Ssam/*
50178354Ssam * Slot time (microseconds).
51178354Ssam */
52178354Ssam#define IEEE80211_DUR_SLOT	20	/* DS/CCK slottime, ERP long slottime */
53178354Ssam#define IEEE80211_DUR_SHSLOT	9	/* ERP short slottime */
54178354Ssam#define IEEE80211_DUR_OFDM_SLOT	9	/* OFDM slottime */
55178354Ssam
56178354Ssam/*
57178354Ssam * DIFS (microseconds).
58178354Ssam */
59178354Ssam#define IEEE80211_DUR_DIFS(sifs, slot)	((sifs) + 2 * (slot))
60178354Ssam
61178354Ssamstruct ieee80211_channel;
62178354Ssam
63252727Sadrian#define	IEEE80211_RATE_TABLE_SIZE	128
64252727Sadrian
65178354Ssamstruct ieee80211_rate_table {
66178354Ssam	int		rateCount;		/* NB: for proper padding */
67178354Ssam	uint8_t		rateCodeToIndex[256];	/* back mapping */
68178354Ssam	struct {
69178354Ssam		uint8_t		phy;		/* CCK/OFDM/TURBO */
70178354Ssam		uint32_t	rateKbps;	/* transfer rate in kbs */
71178354Ssam		uint8_t		shortPreamble;	/* mask for enabling short
72178354Ssam						 * preamble in CCK rate code */
73178354Ssam		uint8_t		dot11Rate;	/* value for supported rates
74178354Ssam						 * info element of MLME */
75178354Ssam		uint8_t		ctlRateIndex;	/* index of next lower basic
76178354Ssam						 * rate; used for dur. calcs */
77178354Ssam		uint16_t	lpAckDuration;	/* long preamble ACK dur. */
78178354Ssam		uint16_t	spAckDuration;	/* short preamble ACK dur. */
79252727Sadrian	} info[IEEE80211_RATE_TABLE_SIZE];
80178354Ssam};
81178354Ssam
82178354Ssamconst struct ieee80211_rate_table *ieee80211_get_ratetable(
83178354Ssam			struct ieee80211_channel *);
84178354Ssam
85178354Ssamstatic __inline__ uint8_t
86178354Ssamieee80211_ack_rate(const struct ieee80211_rate_table *rt, uint8_t rate)
87178354Ssam{
88252727Sadrian	/*
89252727Sadrian	 * XXX Assert this is for a legacy rate; not for an MCS rate.
90252727Sadrian	 * If the caller wishes to use it for a basic rate, they should
91252727Sadrian	 * clear the high bit first.
92252727Sadrian	 */
93252727Sadrian	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate));
94252727Sadrian
95252727Sadrian	uint8_t cix = rt->info[rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL]].ctlRateIndex;
96178354Ssam	KASSERT(cix != (uint8_t)-1, ("rate %d has no info", rate));
97178354Ssam	return rt->info[cix].dot11Rate;
98178354Ssam}
99178354Ssam
100178354Ssamstatic __inline__ uint8_t
101178354Ssamieee80211_ctl_rate(const struct ieee80211_rate_table *rt, uint8_t rate)
102178354Ssam{
103252727Sadrian	/*
104252727Sadrian	 * XXX Assert this is for a legacy rate; not for an MCS rate.
105252727Sadrian	 * If the caller wishes to use it for a basic rate, they should
106252727Sadrian	 * clear the high bit first.
107252727Sadrian	 */
108252727Sadrian	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate));
109252727Sadrian
110252727Sadrian	uint8_t cix = rt->info[rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL]].ctlRateIndex;
111178354Ssam	KASSERT(cix != (uint8_t)-1, ("rate %d has no info", rate));
112178354Ssam	return rt->info[cix].dot11Rate;
113178354Ssam}
114178354Ssam
115178354Ssamstatic __inline__ enum ieee80211_phytype
116178354Ssamieee80211_rate2phytype(const struct ieee80211_rate_table *rt, uint8_t rate)
117178354Ssam{
118252727Sadrian	/*
119252727Sadrian	 * XXX Assert this is for a legacy rate; not for an MCS rate.
120252727Sadrian	 * If the caller wishes to use it for a basic rate, they should
121252727Sadrian	 * clear the high bit first.
122252727Sadrian	 */
123252727Sadrian	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate));
124252727Sadrian
125252727Sadrian	uint8_t rix = rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL];
126178354Ssam	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
127178354Ssam	return rt->info[rix].phy;
128178354Ssam}
129178354Ssam
130193072Ssamstatic __inline__ int
131193072Ssamieee80211_isratevalid(const struct ieee80211_rate_table *rt, uint8_t rate)
132193072Ssam{
133252727Sadrian	/*
134252727Sadrian	 * XXX Assert this is for a legacy rate; not for an MCS rate.
135252727Sadrian	 * If the caller wishes to use it for a basic rate, they should
136252727Sadrian	 * clear the high bit first.
137252727Sadrian	 */
138252727Sadrian	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate));
139252727Sadrian
140193072Ssam	return rt->rateCodeToIndex[rate] != (uint8_t)-1;
141193072Ssam}
142193072Ssam
143178354Ssam/*
144178354Ssam * Calculate ACK field for
145178354Ssam * o  non-fragment data frames
146178354Ssam * o  management frames
147178354Ssam * sent using rate, phy and short preamble setting.
148178354Ssam */
149178354Ssamstatic __inline__ uint16_t
150178354Ssamieee80211_ack_duration(const struct ieee80211_rate_table *rt,
151178354Ssam    uint8_t rate, int isShortPreamble)
152178354Ssam{
153178354Ssam	uint8_t rix = rt->rateCodeToIndex[rate];
154178354Ssam
155178354Ssam	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
156178354Ssam	if (isShortPreamble) {
157178354Ssam		KASSERT(rt->info[rix].spAckDuration != 0,
158178354Ssam			("shpreamble ack dur is not computed!\n"));
159178354Ssam		return rt->info[rix].spAckDuration;
160178354Ssam	} else {
161178354Ssam		KASSERT(rt->info[rix].lpAckDuration != 0,
162178354Ssam			("lgpreamble ack dur is not computed!\n"));
163178354Ssam		return rt->info[rix].lpAckDuration;
164178354Ssam	}
165178354Ssam}
166178354Ssam
167252727Sadrianstatic __inline__ uint8_t
168252727Sadrianieee80211_legacy_rate_lookup(const struct ieee80211_rate_table *rt,
169252727Sadrian    uint8_t rate)
170252727Sadrian{
171252727Sadrian
172252727Sadrian	return (rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL]);
173252727Sadrian}
174252727Sadrian
175178354Ssam/*
176178354Ssam * Compute the time to transmit a frame of length frameLen bytes
177178354Ssam * using the specified 802.11 rate code, phy, and short preamble
178178354Ssam * setting.
179178354Ssam *
180178354Ssam * NB: SIFS is included.
181178354Ssam */
182178354Ssamuint16_t	ieee80211_compute_duration(const struct ieee80211_rate_table *,
183178354Ssam			uint32_t frameLen, uint16_t rate, int isShortPreamble);
184178354Ssam/*
185178354Ssam * Convert PLCP signal/rate field to 802.11 rate code (.5Mbits/s)
186178354Ssam */
187178958Ssamuint8_t		ieee80211_plcp2rate(uint8_t, enum ieee80211_phytype);
188178354Ssam/*
189178354Ssam * Convert 802.11 rate code to PLCP signal.
190178354Ssam */
191178958Ssamuint8_t		ieee80211_rate2plcp(int, enum ieee80211_phytype);
192252727Sadrian
193252727Sadrianuint32_t	ieee80211_compute_duration_ht(uint32_t frameLen,
194252727Sadrian			uint16_t rate, int streams, int isht40,
195252727Sadrian			int isShortGI);
196252727Sadrian
197178354Ssam#endif	/* _KERNEL */
198178354Ssam#endif	/* !_NET80211_IEEE80211_PHY_H_ */
199