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