2 * Copyright (c) 2007-2008 Sam Leffler, Errno Consulting
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
25 * $FreeBSD: head/sys/net80211/ieee80211_phy.c 188821 2009-02-19 17:44:23Z sam $
29 * IEEE 802.11 PHY-related support.
34 #include <sys/param.h>
35 #include <sys/kernel.h>
36 #include <sys/systm.h>
38 #include <sys/socket.h>
41 #include <net/if_media.h>
42 #include <net/route.h>
44 #include <netproto/802_11/ieee80211_var.h>
45 #include <netproto/802_11/ieee80211_phy.h>
48 struct ieee80211_ds_plcp_hdr {
57 /* shorthands to compact tables for readability */
58 #define OFDM IEEE80211_T_OFDM
59 #define CCK IEEE80211_T_CCK
60 #define TURBO IEEE80211_T_TURBO
61 #define HALF IEEE80211_T_OFDM_HALF
62 #define QUART IEEE80211_T_OFDM_QUARTER
63 #define PBCC (IEEE80211_T_OFDM_QUARTER+1) /* XXX */
64 #define B(r) (0x80 | r)
65 #define Mb(x) (x*1000)
67 static struct ieee80211_rate_table ieee80211_11b_table = {
68 .rateCount = 4, /* XXX no PBCC */
71 /* Preamble dot11Rate Rate */
72 [0] = { .phy = CCK, 1000, 0x00, B(2), 0 },/* 1 Mb */
73 [1] = { .phy = CCK, 2000, 0x04, B(4), 1 },/* 2 Mb */
74 [2] = { .phy = CCK, 5500, 0x04, B(11), 1 },/* 5.5 Mb */
75 [3] = { .phy = CCK, 11000, 0x04, B(22), 1 },/* 11 Mb */
76 [4] = { .phy = PBCC, 22000, 0x04, 44, 3 } /* 22 Mb */
80 static struct ieee80211_rate_table ieee80211_11g_table = {
84 /* Preamble dot11Rate Rate */
85 [0] = { .phy = CCK, 1000, 0x00, B(2), 0 },
86 [1] = { .phy = CCK, 2000, 0x04, B(4), 1 },
87 [2] = { .phy = CCK, 5500, 0x04, B(11), 2 },
88 [3] = { .phy = CCK, 11000, 0x04, B(22), 3 },
89 [4] = { .phy = OFDM, 6000, 0x00, 12, 4 },
90 [5] = { .phy = OFDM, 9000, 0x00, 18, 4 },
91 [6] = { .phy = OFDM, 12000, 0x00, 24, 6 },
92 [7] = { .phy = OFDM, 18000, 0x00, 36, 6 },
93 [8] = { .phy = OFDM, 24000, 0x00, 48, 8 },
94 [9] = { .phy = OFDM, 36000, 0x00, 72, 8 },
95 [10] = { .phy = OFDM, 48000, 0x00, 96, 8 },
96 [11] = { .phy = OFDM, 54000, 0x00, 108, 8 }
100 static struct ieee80211_rate_table ieee80211_11a_table = {
104 /* Preamble dot11Rate Rate */
105 [0] = { .phy = OFDM, 6000, 0x00, B(12), 0 },
106 [1] = { .phy = OFDM, 9000, 0x00, 18, 0 },
107 [2] = { .phy = OFDM, 12000, 0x00, B(24), 2 },
108 [3] = { .phy = OFDM, 18000, 0x00, 36, 2 },
109 [4] = { .phy = OFDM, 24000, 0x00, B(48), 4 },
110 [5] = { .phy = OFDM, 36000, 0x00, 72, 4 },
111 [6] = { .phy = OFDM, 48000, 0x00, 96, 4 },
112 [7] = { .phy = OFDM, 54000, 0x00, 108, 4 }
116 static struct ieee80211_rate_table ieee80211_half_table = {
120 /* Preamble dot11Rate Rate */
121 [0] = { .phy = HALF, 3000, 0x00, B(6), 0 },
122 [1] = { .phy = HALF, 4500, 0x00, 9, 0 },
123 [2] = { .phy = HALF, 6000, 0x00, B(12), 2 },
124 [3] = { .phy = HALF, 9000, 0x00, 18, 2 },
125 [4] = { .phy = HALF, 12000, 0x00, B(24), 4 },
126 [5] = { .phy = HALF, 18000, 0x00, 36, 4 },
127 [6] = { .phy = HALF, 24000, 0x00, 48, 4 },
128 [7] = { .phy = HALF, 27000, 0x00, 54, 4 }
132 static struct ieee80211_rate_table ieee80211_quarter_table = {
136 /* Preamble dot11Rate Rate */
137 [0] = { .phy = QUART, 1500, 0x00, B(3), 0 },
138 [1] = { .phy = QUART, 2250, 0x00, 4, 0 },
139 [2] = { .phy = QUART, 3000, 0x00, B(9), 2 },
140 [3] = { .phy = QUART, 4500, 0x00, 9, 2 },
141 [4] = { .phy = QUART, 6000, 0x00, B(12), 4 },
142 [5] = { .phy = QUART, 9000, 0x00, 18, 4 },
143 [6] = { .phy = QUART, 12000, 0x00, 24, 4 },
144 [7] = { .phy = QUART, 13500, 0x00, 27, 4 }
148 static struct ieee80211_rate_table ieee80211_turbog_table = {
152 /* Preamble dot11Rate Rate */
153 [0] = { .phy = TURBO, 12000, 0x00, B(12), 0 },
154 [1] = { .phy = TURBO, 24000, 0x00, B(24), 1 },
155 [2] = { .phy = TURBO, 36000, 0x00, 36, 1 },
156 [3] = { .phy = TURBO, 48000, 0x00, B(48), 3 },
157 [4] = { .phy = TURBO, 72000, 0x00, 72, 3 },
158 [5] = { .phy = TURBO, 96000, 0x00, 96, 3 },
159 [6] = { .phy = TURBO, 108000, 0x00, 108, 3 }
163 static struct ieee80211_rate_table ieee80211_turboa_table = {
167 /* Preamble dot11Rate Rate */
168 [0] = { .phy = TURBO, 12000, 0x00, B(12), 0 },
169 [1] = { .phy = TURBO, 18000, 0x00, 18, 0 },
170 [2] = { .phy = TURBO, 24000, 0x00, B(24), 2 },
171 [3] = { .phy = TURBO, 36000, 0x00, 36, 2 },
172 [4] = { .phy = TURBO, 48000, 0x00, B(48), 4 },
173 [5] = { .phy = TURBO, 72000, 0x00, 72, 4 },
174 [6] = { .phy = TURBO, 96000, 0x00, 96, 4 },
175 [7] = { .phy = TURBO, 108000, 0x00, 108, 4 }
189 * Setup a rate table's reverse lookup table and fill in
190 * ack durations. The reverse lookup tables are assumed
191 * to be initialized to zero (or at least the first entry).
192 * We use this as a key that indicates whether or not
193 * we've previously setup the reverse lookup table.
195 * XXX not reentrant, but shouldn't matter
198 ieee80211_setup_ratetable(struct ieee80211_rate_table *rt)
200 #define WLAN_CTRL_FRAME_SIZE \
201 (sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
205 for (i = 0; i < NELEM(rt->rateCodeToIndex); i++)
206 rt->rateCodeToIndex[i] = (uint8_t) -1;
207 for (i = 0; i < rt->rateCount; i++) {
208 uint8_t code = rt->info[i].dot11Rate;
209 uint8_t cix = rt->info[i].ctlRateIndex;
210 uint8_t ctl_rate = rt->info[cix].dot11Rate;
212 rt->rateCodeToIndex[code] = i;
213 if (code & IEEE80211_RATE_BASIC) {
215 * Map w/o basic rate bit too.
217 code &= IEEE80211_RATE_VAL;
218 rt->rateCodeToIndex[code] = i;
222 * XXX for 11g the control rate to use for 5.5 and 11 Mb/s
223 * depends on whether they are marked as basic rates;
224 * the static tables are setup with an 11b-compatible
225 * 2Mb/s rate which will work but is suboptimal
227 * NB: Control rate is always less than or equal to the
228 * current rate, so control rate's reverse lookup entry
229 * has been installed and following call is safe.
231 rt->info[i].lpAckDuration = ieee80211_compute_duration(rt,
232 WLAN_CTRL_FRAME_SIZE, ctl_rate, 0);
233 rt->info[i].spAckDuration = ieee80211_compute_duration(rt,
234 WLAN_CTRL_FRAME_SIZE, ctl_rate, IEEE80211_F_SHPREAMBLE);
237 #undef WLAN_CTRL_FRAME_SIZE
240 /* Setup all rate tables */
242 ieee80211_phy_init(void)
244 static struct ieee80211_rate_table * const ratetables[] = {
245 &ieee80211_half_table,
246 &ieee80211_quarter_table,
247 &ieee80211_11a_table,
248 &ieee80211_11g_table,
249 &ieee80211_turbog_table,
250 &ieee80211_turboa_table,
251 &ieee80211_turboa_table,
252 &ieee80211_11a_table,
253 &ieee80211_11g_table,
258 for (i = 0; i < NELEM(ratetables); ++i)
259 ieee80211_setup_ratetable(ratetables[i]);
262 SYSINIT(wlan_phy, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_phy_init, NULL);
264 const struct ieee80211_rate_table *
265 ieee80211_get_ratetable(struct ieee80211_channel *c)
267 const struct ieee80211_rate_table *rt;
270 if (IEEE80211_IS_CHAN_HALF(c))
271 rt = &ieee80211_half_table;
272 else if (IEEE80211_IS_CHAN_QUARTER(c))
273 rt = &ieee80211_quarter_table;
274 else if (IEEE80211_IS_CHAN_HTA(c))
275 rt = &ieee80211_11a_table; /* XXX */
276 else if (IEEE80211_IS_CHAN_HTG(c))
277 rt = &ieee80211_11g_table; /* XXX */
278 else if (IEEE80211_IS_CHAN_108G(c))
279 rt = &ieee80211_turbog_table;
280 else if (IEEE80211_IS_CHAN_ST(c))
281 rt = &ieee80211_turboa_table;
282 else if (IEEE80211_IS_CHAN_TURBO(c))
283 rt = &ieee80211_turboa_table;
284 else if (IEEE80211_IS_CHAN_A(c))
285 rt = &ieee80211_11a_table;
286 else if (IEEE80211_IS_CHAN_ANYG(c))
287 rt = &ieee80211_11g_table;
288 else if (IEEE80211_IS_CHAN_B(c))
289 rt = &ieee80211_11b_table;
291 /* NB: should not get here */
292 panic("%s: no rate table for channel; freq %u flags 0x%x\n",
293 __func__, c->ic_freq, c->ic_flags);
299 * Convert PLCP signal/rate field to 802.11 rate (.5Mbits/s)
301 * Note we do no parameter checking; this routine is mainly
302 * used to derive an 802.11 rate for constructing radiotap
303 * header data for rx frames.
305 * XXX might be a candidate for inline
308 ieee80211_plcp2rate(uint8_t plcp, enum ieee80211_phytype type)
310 if (type == IEEE80211_T_OFDM) {
311 static const uint8_t ofdm_plcp2rate[16] = {
321 return ofdm_plcp2rate[plcp & 0xf];
323 if (type == IEEE80211_T_CCK) {
324 static const uint8_t cck_plcp2rate[16] = {
325 [0xa] = 2, /* 0x0a */
326 [0x4] = 4, /* 0x14 */
327 [0x7] = 11, /* 0x37 */
328 [0xe] = 22, /* 0x6e */
329 [0xc] = 44, /* 0xdc , actually PBCC */
331 return cck_plcp2rate[plcp & 0xf];
337 * Covert 802.11 rate to PLCP signal.
340 ieee80211_rate2plcp(int rate, enum ieee80211_phytype type)
342 /* XXX ignore type for now since rates are unique */
344 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
352 case 108: return 0xc;
353 /* CCK rates (IEEE Std 802.11b-1999 page 15, subclause 18.2.3.3) */
358 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
361 return 0; /* XXX unsupported/unknown rate */
364 #define CCK_SIFS_TIME 10
365 #define CCK_PREAMBLE_BITS 144
366 #define CCK_PLCP_BITS 48
368 #define OFDM_SIFS_TIME 16
369 #define OFDM_PREAMBLE_TIME 20
370 #define OFDM_PLCP_BITS 22
371 #define OFDM_SYMBOL_TIME 4
373 #define OFDM_HALF_SIFS_TIME 32
374 #define OFDM_HALF_PREAMBLE_TIME 40
375 #define OFDM_HALF_PLCP_BITS 22
376 #define OFDM_HALF_SYMBOL_TIME 8
378 #define OFDM_QUARTER_SIFS_TIME 64
379 #define OFDM_QUARTER_PREAMBLE_TIME 80
380 #define OFDM_QUARTER_PLCP_BITS 22
381 #define OFDM_QUARTER_SYMBOL_TIME 16
383 #define TURBO_SIFS_TIME 8
384 #define TURBO_PREAMBLE_TIME 14
385 #define TURBO_PLCP_BITS 22
386 #define TURBO_SYMBOL_TIME 4
389 * Compute the time to transmit a frame of length frameLen bytes
390 * using the specified rate, phy, and short preamble setting.
394 ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
395 uint32_t frameLen, uint16_t rate, int isShortPreamble)
397 uint8_t rix = rt->rateCodeToIndex[rate];
398 uint32_t bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
401 KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
402 kbps = rt->info[rix].rateKbps;
403 if (kbps == 0) /* XXX bandaid for channel changes */
406 switch (rt->info[rix].phy) {
407 case IEEE80211_T_CCK:
408 phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
409 if (isShortPreamble && rt->info[rix].shortPreamble)
411 numBits = frameLen << 3;
412 txTime = CCK_SIFS_TIME + phyTime
413 + ((numBits * 1000)/kbps);
415 case IEEE80211_T_OFDM:
416 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
417 KASSERT(bitsPerSymbol != 0, ("full rate bps"));
419 numBits = OFDM_PLCP_BITS + (frameLen << 3);
420 numSymbols = howmany(numBits, bitsPerSymbol);
421 txTime = OFDM_SIFS_TIME
423 + (numSymbols * OFDM_SYMBOL_TIME);
425 case IEEE80211_T_OFDM_HALF:
426 bitsPerSymbol = (kbps * OFDM_HALF_SYMBOL_TIME) / 1000;
427 KASSERT(bitsPerSymbol != 0, ("1/4 rate bps"));
429 numBits = OFDM_PLCP_BITS + (frameLen << 3);
430 numSymbols = howmany(numBits, bitsPerSymbol);
431 txTime = OFDM_HALF_SIFS_TIME
432 + OFDM_HALF_PREAMBLE_TIME
433 + (numSymbols * OFDM_HALF_SYMBOL_TIME);
435 case IEEE80211_T_OFDM_QUARTER:
436 bitsPerSymbol = (kbps * OFDM_QUARTER_SYMBOL_TIME) / 1000;
437 KASSERT(bitsPerSymbol != 0, ("1/2 rate bps"));
439 numBits = OFDM_PLCP_BITS + (frameLen << 3);
440 numSymbols = howmany(numBits, bitsPerSymbol);
441 txTime = OFDM_QUARTER_SIFS_TIME
442 + OFDM_QUARTER_PREAMBLE_TIME
443 + (numSymbols * OFDM_QUARTER_SYMBOL_TIME);
445 case IEEE80211_T_TURBO:
446 /* we still save OFDM rates in kbps - so double them */
447 bitsPerSymbol = ((kbps << 1) * TURBO_SYMBOL_TIME) / 1000;
448 KASSERT(bitsPerSymbol != 0, ("turbo bps"));
450 numBits = TURBO_PLCP_BITS + (frameLen << 3);
451 numSymbols = howmany(numBits, bitsPerSymbol);
452 txTime = TURBO_SIFS_TIME + TURBO_PREAMBLE_TIME
453 + (numSymbols * TURBO_SYMBOL_TIME);
456 panic("%s: unknown phy %u (rate %u)\n", __func__,
457 rt->info[rix].phy, rate);