| 1 | /*- |
| 2 | * SPDX-License-Identifier: BSD-2-Clause |
| 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 | |
| 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 | /* |
| 57 | * For drivers that don't implement per-VAP slot time |
| 58 | * (ie, they rely on net80211 figuring out the union |
| 59 | * between VAPs to program a single radio) - return |
| 60 | * the current radio configured slot time. |
| 61 | */ |
| 62 | #define IEEE80211_GET_SLOTTIME(ic) \ |
| 63 | 	((ic->ic_flags & IEEE80211_F_SHSLOT) ? \ |
| 64 | 	 IEEE80211_DUR_SHSLOT : IEEE80211_DUR_SLOT) |
| 65 | |
| 66 | /* |
| 67 | * For drivers that implement per-VAP slot time; look |
| 68 | * at the per-VAP flags to determine whether this VAP |
| 69 | * is in short or long slot time. |
| 70 | */ |
| 71 | #define IEEE80211_VAP_GET_SLOTTIME(vap) \ |
| 72 | 	((vap->iv_flags & IEEE80211_F_SHSLOT) ? \ |
| 73 | 	 IEEE80211_DUR_SHSLOT : IEEE80211_DUR_SLOT) |
| 74 | |
| 75 | /* |
| 76 | * DIFS (microseconds). |
| 77 | */ |
| 78 | #define IEEE80211_DUR_DIFS(sifs, slot)	((sifs) + 2 * (slot)) |
| 79 | |
| 80 | struct ieee80211_channel; |
| 81 | |
| 82 | #define	IEEE80211_RATE_TABLE_SIZE	128 |
| 83 | |
| 84 | struct ieee80211_rate_table { |
| 85 | 	int		rateCount;		/* NB: for proper padding */ |
| 86 | 	uint8_t		rateCodeToIndex[256];	/* back mapping */ |
| 87 | 	struct { |
| 88 | 		uint8_t		phy;		/* CCK/OFDM/TURBO */ |
| 89 | 		uint32_t	rateKbps;	/* transfer rate in kbs */ |
| 90 | 		uint8_t		shortPreamble;	/* mask for enabling short |
| 91 | 						 * preamble in CCK rate code */ |
| 92 | 		uint8_t		dot11Rate;	/* value for supported rates |
| 93 | 						 * info element of MLME */ |
| 94 | 		uint8_t		ctlRateIndex;	/* index of next lower basic |
| 95 | 						 * rate; used for dur. calcs */ |
| 96 | 		uint16_t	lpAckDuration;	/* long preamble ACK dur. */ |
| 97 | 		uint16_t	spAckDuration;	/* short preamble ACK dur. */ |
| 98 | 	} info[IEEE80211_RATE_TABLE_SIZE]; |
| 99 | }; |
| 100 | |
| 101 | const struct ieee80211_rate_table *ieee80211_get_ratetable( |
| 102 | 			struct ieee80211_channel *); |
| 103 | |
| 104 | static __inline__ uint8_t |
| 105 | ieee80211_ack_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__ uint8_t |
| 120 | ieee80211_ctl_rate(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 cix = rt->info[rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL]].ctlRateIndex; |
| 130 | 	KASSERT(cix != (uint8_t)-1, ("rate %d has no info", rate)); |
| 131 | 	return rt->info[cix].dot11Rate; |
| 132 | } |
| 133 | |
| 134 | static __inline__ enum ieee80211_phytype |
| 135 | ieee80211_rate2phytype(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 | 	uint8_t rix = rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL]; |
| 145 | 	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate)); |
| 146 | 	return rt->info[rix].phy; |
| 147 | } |
| 148 | |
| 149 | static __inline__ int |
| 150 | ieee80211_isratevalid(const struct ieee80211_rate_table *rt, uint8_t rate) |
| 151 | { |
| 152 | 	/* |
| 153 | 	 * XXX Assert this is for a legacy rate; not for an MCS rate. |
| 154 | 	 * If the caller wishes to use it for a basic rate, they should |
| 155 | 	 * clear the high bit first. |
| 156 | 	 */ |
| 157 | 	KASSERT(! (rate & 0x80), ("rate %d is basic/mcs?", rate)); |
| 158 | |
| 159 | 	return rt->rateCodeToIndex[rate] != (uint8_t)-1; |
| 160 | } |
| 161 | |
| 162 | /* |
| 163 | * Calculate ACK field for |
| 164 | * o non-fragment data frames |
| 165 | * o management frames |
| 166 | * sent using rate, phy and short preamble setting. |
| 167 | */ |
| 168 | static __inline__ uint16_t |
| 169 | ieee80211_ack_duration(const struct ieee80211_rate_table *rt, |
| 170 | uint8_t rate, int isShortPreamble) |
| 171 | { |
| 172 | 	uint8_t rix = rt->rateCodeToIndex[rate]; |
| 173 | |
| 174 | 	KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate)); |
| 175 | 	if (isShortPreamble) { |
| 176 | 		KASSERT(rt->info[rix].spAckDuration != 0, |
| 177 | 			("shpreamble ack dur is not computed!\n")); |
| 178 | 		return rt->info[rix].spAckDuration; |
| 179 | 	} else { |
| 180 | 		KASSERT(rt->info[rix].lpAckDuration != 0, |
| 181 | 			("lgpreamble ack dur is not computed!\n")); |
| 182 | 		return rt->info[rix].lpAckDuration; |
| 183 | 	} |
| 184 | } |
| 185 | |
| 186 | static __inline__ uint8_t |
| 187 | ieee80211_legacy_rate_lookup(const struct ieee80211_rate_table *rt, |
| 188 | uint8_t rate) |
| 189 | { |
| 190 | |
| 191 | 	return (rt->rateCodeToIndex[rate & IEEE80211_RATE_VAL]); |
| 192 | } |
| 193 | |
| 194 | /* |
| 195 | * Compute the time to transmit a frame of length frameLen bytes |
| 196 | * using the specified 802.11 rate code, phy, and short preamble |
| 197 | * setting. |
| 198 | * |
| 199 | * NB: SIFS is included. |
| 200 | */ |
| 201 | uint16_t	ieee80211_compute_duration(const struct ieee80211_rate_table *, |
| 202 | 			uint32_t frameLen, uint16_t rate, int isShortPreamble); |
| 203 | /* |
| 204 | * Convert PLCP signal/rate field to 802.11 rate code (.5Mbits/s) |
| 205 | */ |
| 206 | uint8_t		ieee80211_plcp2rate(uint8_t, enum ieee80211_phytype); |
| 207 | /* |
| 208 | * Convert 802.11 rate code to PLCP signal. |
| 209 | */ |
| 210 | uint8_t		ieee80211_rate2plcp(int, enum ieee80211_phytype); |
| 211 | |
| 212 | /* |
| 213 | * 802.11n rate manipulation. |
| 214 | */ |
| 215 | |
| 216 | #define	IEEE80211_HT_RC_2_MCS(_rc)	((_rc) & 0x1f) |
| 217 | #define	IEEE80211_HT_RC_2_STREAMS(_rc)	((((_rc) & 0x78) >> 3) + 1) |
| 218 | #define	IEEE80211_IS_HT_RATE(_rc)		( (_rc) & IEEE80211_RATE_MCS) |
| 219 | |
| 220 | uint32_t	ieee80211_compute_duration_ht(uint32_t frameLen, |
| 221 | 			uint16_t rate, int streams, int isht40, |
| 222 | 			int isShortGI); |
| 223 | |
| 224 | enum net80211_sta_rx_bw; |
| 225 | |
| 226 | uint16_t	ieee80211_phy_vht_get_mcs_mask(enum net80211_sta_rx_bw, |
| 227 | 		 uint8_t); |
| 228 | bool		ieee80211_phy_vht_validate_mcs(enum net80211_sta_rx_bw, |
| 229 | 		 uint8_t, uint8_t); |
| 230 | uint32_t	ieee80211_phy_vht_get_mcs_kbit(enum net80211_sta_rx_bw, |
| 231 | 		 uint8_t, uint8_t, bool); |
| 232 | |
| 233 | #endif	/* _KERNEL */ |
| 234 | #endif	/* !_NET80211_IEEE80211_PHY_H_ */ |