1 /*-
2  * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
16  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
17  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
18  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
19  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
20  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
21  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27 
28 #ifndef _NET80211_IEEE80211_PHY_H_
29 #define _NET80211_IEEE80211_PHY_H_
30 
31 #ifdef _KERNEL
32 /*
33  * IEEE 802.11 PHY-related definitions.
34  */
35 
36 /*
37  * Contention window (slots).
38  */
39 #define IEEE80211_CW_MAX	1023	/* aCWmax */
40 #define IEEE80211_CW_MIN_0	31	/* DS/CCK aCWmin, ERP aCWmin(0) */
41 #define IEEE80211_CW_MIN_1	15	/* OFDM aCWmin, ERP aCWmin(1) */
42 
43 /*
44  * SIFS (microseconds).
45  */
46 #define IEEE80211_DUR_SIFS	10	/* DS/CCK/ERP SIFS */
47 #define IEEE80211_DUR_OFDM_SIFS	16	/* OFDM SIFS */
48 
49 /*
50  * Slot time (microseconds).
51  */
52 #define IEEE80211_DUR_SLOT	20	/* DS/CCK slottime, ERP long slottime */
53 #define IEEE80211_DUR_SHSLOT	9	/* ERP short slottime */
54 #define IEEE80211_DUR_OFDM_SLOT	9	/* OFDM slottime */
55 
56 #define IEEE80211_GET_SLOTTIME(ic) \
57 	((ic->ic_flags & IEEE80211_F_SHSLOT) ? \
58 	    IEEE80211_DUR_SHSLOT : IEEE80211_DUR_SLOT)
59 
60 /*
61  * DIFS (microseconds).
62  */
63 #define IEEE80211_DUR_DIFS(sifs, slot)	((sifs) + 2 * (slot))
64 
65 struct ieee80211_channel;
66 
67 #define	IEEE80211_RATE_TABLE_SIZE	128
68 
69 struct ieee80211_rate_table {
70 	int		rateCount;		/* NB: for proper padding */
71 	uint8_t		rateCodeToIndex[256];	/* back mapping */
72 	struct {
73 		uint8_t		phy;		/* CCK/OFDM/TURBO */
74 		uint32_t	rateKbps;	/* transfer rate in kbs */
75 		uint8_t		shortPreamble;	/* mask for enabling short
76 						 * preamble in CCK rate code */
77 		uint8_t		dot11Rate;	/* value for supported rates
78 						 * info element of MLME */
79 		uint8_t		ctlRateIndex;	/* index of next lower basic
80 						 * rate; used for dur. calcs */
81 		uint16_t	lpAckDuration;	/* long preamble ACK dur. */
82 		uint16_t	spAckDuration;	/* short preamble ACK dur. */
83 	} info[IEEE80211_RATE_TABLE_SIZE];
84 };
85 
86 const struct ieee80211_rate_table *ieee80211_get_ratetable(
87 			struct ieee80211_channel *);
88 
89 static __inline__ uint8_t
90 ieee80211_ack_rate(const struct ieee80211_rate_table *rt, uint8_t rate)
91 {
92 	/*
93 	 * XXX Assert this is for a legacy rate; not for an MCS rate.
94 	 * If the caller wishes to use it for a basic rate, they should
95 	 * clear the high bit first.
96 	 */
97 	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate));
98 
99 	uint8_t cix = rt->info[rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL]].ctlRateIndex;
100 	KASSERT(cix != (uint8_t)-1, ("rate %d has no info", rate));
101 	return rt->info[cix].dot11Rate;
102 }
103 
104 static __inline__ uint8_t
105 ieee80211_ctl_rate(const struct ieee80211_rate_table *rt, uint8_t rate)
106 {
107 	/*
108 	 * XXX Assert this is for a legacy rate; not for an MCS rate.
109 	 * If the caller wishes to use it for a basic rate, they should
110 	 * clear the high bit first.
111 	 */
112 	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate));
113 
114 	uint8_t cix = rt->info[rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL]].ctlRateIndex;
115 	KASSERT(cix != (uint8_t)-1, ("rate %d has no info", rate));
116 	return rt->info[cix].dot11Rate;
117 }
118 
119 static __inline__ enum ieee80211_phytype
120 ieee80211_rate2phytype(const struct ieee80211_rate_table *rt, uint8_t rate)
121 {
122 	/*
123 	 * XXX Assert this is for a legacy rate; not for an MCS rate.
124 	 * If the caller wishes to use it for a basic rate, they should
125 	 * clear the high bit first.
126 	 */
127 	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate));
128 
129 	uint8_t rix = rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL];
130 	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
131 	return rt->info[rix].phy;
132 }
133 
134 static __inline__ int
135 ieee80211_isratevalid(const struct ieee80211_rate_table *rt, uint8_t rate)
136 {
137 	/*
138 	 * XXX Assert this is for a legacy rate; not for an MCS rate.
139 	 * If the caller wishes to use it for a basic rate, they should
140 	 * clear the high bit first.
141 	 */
142 	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate));
143 
144 	return rt->rateCodeToIndex[rate] != (uint8_t)-1;
145 }
146 
147 /*
148  * Calculate ACK field for
149  * o  non-fragment data frames
150  * o  management frames
151  * sent using rate, phy and short preamble setting.
152  */
153 static __inline__ uint16_t
154 ieee80211_ack_duration(const struct ieee80211_rate_table *rt,
155     uint8_t rate, int isShortPreamble)
156 {
157 	uint8_t rix = rt->rateCodeToIndex[rate];
158 
159 	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
160 	if (isShortPreamble) {
161 		KASSERT(rt->info[rix].spAckDuration != 0,
162 			("shpreamble ack dur is not computed!\n"));
163 		return rt->info[rix].spAckDuration;
164 	} else {
165 		KASSERT(rt->info[rix].lpAckDuration != 0,
166 			("lgpreamble ack dur is not computed!\n"));
167 		return rt->info[rix].lpAckDuration;
168 	}
169 }
170 
171 static __inline__ uint8_t
172 ieee80211_legacy_rate_lookup(const struct ieee80211_rate_table *rt,
173     uint8_t rate)
174 {
175 
176 	return (rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL]);
177 }
178 
179 /*
180  * Compute the time to transmit a frame of length frameLen bytes
181  * using the specified 802.11 rate code, phy, and short preamble
182  * setting.
183  *
184  * NB: SIFS is included.
185  */
186 uint16_t	ieee80211_compute_duration(const struct ieee80211_rate_table *,
187 			uint32_t frameLen, uint16_t rate, int isShortPreamble);
188 /*
189  * Convert PLCP signal/rate field to 802.11 rate code (.5Mbits/s)
190  */
191 uint8_t		ieee80211_plcp2rate(uint8_t, enum ieee80211_phytype);
192 /*
193  * Convert 802.11 rate code to PLCP signal.
194  */
195 uint8_t		ieee80211_rate2plcp(int, enum ieee80211_phytype);
196 
197 /*
198  * 802.11n rate manipulation.
199  */
200 
201 #define	IEEE80211_HT_RC_2_MCS(_rc)	((_rc) & 0x1f)
202 #define	IEEE80211_HT_RC_2_STREAMS(_rc)	((((_rc) & 0x78) >> 3) + 1)
203 #define	IEEE80211_IS_HT_RATE(_rc)		( (_rc) & IEEE80211_RATE_MCS)
204 
205 uint32_t	ieee80211_compute_duration_ht(uint32_t frameLen,
206 			uint16_t rate, int streams, int isht40,
207 			int isShortGI);
208 
209 #endif	/* _KERNEL */
210 #endif	/* !_NET80211_IEEE80211_PHY_H_ */
211