xref: /haiku/src/libs/compat/freebsd_wlan/net80211/ieee80211_phy.h (revision 5ac9b506412b11afb993bb52d161efe7666958a5)
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: releng/12.0/sys/net80211/ieee80211_phy.h 326272 2017-11-27 15:23:17Z pfg $
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 #define IEEE80211_GET_SLOTTIME(ic) \
59 	((ic->ic_flags & IEEE80211_F_SHSLOT) ? \
60 	    IEEE80211_DUR_SHSLOT : IEEE80211_DUR_SLOT)
61 
62 /*
63  * DIFS (microseconds).
64  */
65 #define IEEE80211_DUR_DIFS(sifs, slot)	((sifs) + 2 * (slot))
66 
67 struct ieee80211_channel;
68 
69 #define	IEEE80211_RATE_TABLE_SIZE	128
70 
71 struct ieee80211_rate_table {
72 	int		rateCount;		/* NB: for proper padding */
73 	uint8_t		rateCodeToIndex[256];	/* back mapping */
74 	struct {
75 		uint8_t		phy;		/* CCK/OFDM/TURBO */
76 		uint32_t	rateKbps;	/* transfer rate in kbs */
77 		uint8_t		shortPreamble;	/* mask for enabling short
78 						 * preamble in CCK rate code */
79 		uint8_t		dot11Rate;	/* value for supported rates
80 						 * info element of MLME */
81 		uint8_t		ctlRateIndex;	/* index of next lower basic
82 						 * rate; used for dur. calcs */
83 		uint16_t	lpAckDuration;	/* long preamble ACK dur. */
84 		uint16_t	spAckDuration;	/* short preamble ACK dur. */
85 	} info[IEEE80211_RATE_TABLE_SIZE];
86 };
87 
88 const struct ieee80211_rate_table *ieee80211_get_ratetable(
89 			struct ieee80211_channel *);
90 
91 static __inline__ uint8_t
92 ieee80211_ack_rate(const struct ieee80211_rate_table *rt, uint8_t rate)
93 {
94 	uint8_t cix;
95 	/*
96 	 * XXX Assert this is for a legacy rate; not for an MCS rate.
97 	 * If the caller wishes to use it for a basic rate, they should
98 	 * clear the high bit first.
99 	 */
100 	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate));
101 
102 	cix = rt->info[rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL]].ctlRateIndex;
103 	KASSERT(cix != (uint8_t)-1, ("rate %d has no info", rate));
104 	return rt->info[cix].dot11Rate;
105 }
106 
107 static __inline__ uint8_t
108 ieee80211_ctl_rate(const struct ieee80211_rate_table *rt, uint8_t rate)
109 {
110 	uint8_t cix;
111 	/*
112 	 * XXX Assert this is for a legacy rate; not for an MCS rate.
113 	 * If the caller wishes to use it for a basic rate, they should
114 	 * clear the high bit first.
115 	 */
116 	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate));
117 
118 	cix = rt->info[rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL]].ctlRateIndex;
119 	KASSERT(cix != (uint8_t)-1, ("rate %d has no info", rate));
120 	return rt->info[cix].dot11Rate;
121 }
122 
123 static __inline__ enum ieee80211_phytype
124 ieee80211_rate2phytype(const struct ieee80211_rate_table *rt, uint8_t rate)
125 {
126 	uint8_t rix;
127 	/*
128 	 * XXX Assert this is for a legacy rate; not for an MCS rate.
129 	 * If the caller wishes to use it for a basic rate, they should
130 	 * clear the high bit first.
131 	 */
132 	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate));
133 
134 	rix = rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL];
135 	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
136 	return (enum ieee80211_phytype)rt->info[rix].phy;
137 }
138 
139 static __inline__ int
140 ieee80211_isratevalid(const struct ieee80211_rate_table *rt, uint8_t rate)
141 {
142 	/*
143 	 * XXX Assert this is for a legacy rate; not for an MCS rate.
144 	 * If the caller wishes to use it for a basic rate, they should
145 	 * clear the high bit first.
146 	 */
147 	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate));
148 
149 	return rt->rateCodeToIndex[rate] != (uint8_t)-1;
150 }
151 
152 /*
153  * Calculate ACK field for
154  * o  non-fragment data frames
155  * o  management frames
156  * sent using rate, phy and short preamble setting.
157  */
158 static __inline__ uint16_t
159 ieee80211_ack_duration(const struct ieee80211_rate_table *rt,
160     uint8_t rate, int isShortPreamble)
161 {
162 	uint8_t rix = rt->rateCodeToIndex[rate];
163 
164 	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
165 	if (isShortPreamble) {
166 		KASSERT(rt->info[rix].spAckDuration != 0,
167 			("shpreamble ack dur is not computed!\n"));
168 		return rt->info[rix].spAckDuration;
169 	} else {
170 		KASSERT(rt->info[rix].lpAckDuration != 0,
171 			("lgpreamble ack dur is not computed!\n"));
172 		return rt->info[rix].lpAckDuration;
173 	}
174 }
175 
176 static __inline__ uint8_t
177 ieee80211_legacy_rate_lookup(const struct ieee80211_rate_table *rt,
178     uint8_t rate)
179 {
180 
181 	return (rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL]);
182 }
183 
184 /*
185  * Compute the time to transmit a frame of length frameLen bytes
186  * using the specified 802.11 rate code, phy, and short preamble
187  * setting.
188  *
189  * NB: SIFS is included.
190  */
191 uint16_t	ieee80211_compute_duration(const struct ieee80211_rate_table *,
192 			uint32_t frameLen, uint16_t rate, int isShortPreamble);
193 /*
194  * Convert PLCP signal/rate field to 802.11 rate code (.5Mbits/s)
195  */
196 uint8_t		ieee80211_plcp2rate(uint8_t, enum ieee80211_phytype);
197 /*
198  * Convert 802.11 rate code to PLCP signal.
199  */
200 uint8_t		ieee80211_rate2plcp(int, enum ieee80211_phytype);
201 
202 /*
203  * 802.11n rate manipulation.
204  */
205 
206 #define	IEEE80211_HT_RC_2_MCS(_rc)	((_rc) & 0x1f)
207 #define	IEEE80211_HT_RC_2_STREAMS(_rc)	((((_rc) & 0x78) >> 3) + 1)
208 #define	IEEE80211_IS_HT_RATE(_rc)		( (_rc) & IEEE80211_RATE_MCS)
209 
210 uint32_t	ieee80211_compute_duration_ht(uint32_t frameLen,
211 			uint16_t rate, int streams, int isht40,
212 			int isShortGI);
213 
214 #endif	/* _KERNEL */
215 #endif	/* !_NET80211_IEEE80211_PHY_H_ */
216