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
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD: src/sys/net80211/ieee80211_phy.c,v 1.3 2008/09/06 17:48:25 sam Exp $");
28
29 /*
30 * IEEE 802.11 PHY-related support.
31 */
32
33 #include "opt_inet.h"
34
35 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/systm.h>
38
39 #include <sys/socket.h>
40
41 #include <net/if.h>
42 #include <net/if_media.h>
43
44 #include <net80211/ieee80211_var.h>
45 #include <net80211/ieee80211_phy.h>
46
47 #ifdef notyet
48 struct ieee80211_ds_plcp_hdr {
49 uint8_t i_signal;
50 uint8_t i_service;
51 uint16_t i_length;
52 uint16_t i_crc;
53 } __packed;
54
55 #endif /* notyet */
56
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 PBCC (IEEE80211_T_HT+1) /* XXX */
62 #define B(r) (0x80 | r)
63 #define Mb(x) (x*1000)
64
65 static struct ieee80211_rate_table ieee80211_11b_table = {
66 .rateCount = 4, /* XXX no PBCC */
67 .info = {
68 /* short ctrl */
69 /* Preamble dot11Rate Rate */
70 [0] = { .phy = CCK, 1000, 0x00, B(2), 0 },/* 1 Mb */
71 [1] = { .phy = CCK, 2000, 0x04, B(4), 1 },/* 2 Mb */
72 [2] = { .phy = CCK, 5500, 0x04, B(11), 1 },/* 5.5 Mb */
73 [3] = { .phy = CCK, 11000, 0x04, B(22), 1 },/* 11 Mb */
74 [4] = { .phy = PBCC, 22000, 0x04, 44, 3 } /* 22 Mb */
75 },
76 };
77
78 static struct ieee80211_rate_table ieee80211_11g_table = {
79 .rateCount = 12,
80 .info = {
81 /* short ctrl */
82 /* Preamble dot11Rate Rate */
83 [0] = { .phy = CCK, 1000, 0x00, B(2), 0 },
84 [1] = { .phy = CCK, 2000, 0x04, B(4), 1 },
85 [2] = { .phy = CCK, 5500, 0x04, B(11), 2 },
86 [3] = { .phy = CCK, 11000, 0x04, B(22), 3 },
87 [4] = { .phy = OFDM, 6000, 0x00, 12, 4 },
88 [5] = { .phy = OFDM, 9000, 0x00, 18, 4 },
89 [6] = { .phy = OFDM, 12000, 0x00, 24, 6 },
90 [7] = { .phy = OFDM, 18000, 0x00, 36, 6 },
91 [8] = { .phy = OFDM, 24000, 0x00, 48, 8 },
92 [9] = { .phy = OFDM, 36000, 0x00, 72, 8 },
93 [10] = { .phy = OFDM, 48000, 0x00, 96, 8 },
94 [11] = { .phy = OFDM, 54000, 0x00, 108, 8 }
95 },
96 };
97
98 static struct ieee80211_rate_table ieee80211_11a_table = {
99 .rateCount = 8,
100 .info = {
101 /* short ctrl */
102 /* Preamble dot11Rate Rate */
103 [0] = { .phy = OFDM, 6000, 0x00, B(12), 0 },
104 [1] = { .phy = OFDM, 9000, 0x00, 18, 0 },
105 [2] = { .phy = OFDM, 12000, 0x00, B(24), 2 },
106 [3] = { .phy = OFDM, 18000, 0x00, 36, 2 },
107 [4] = { .phy = OFDM, 24000, 0x00, B(48), 4 },
108 [5] = { .phy = OFDM, 36000, 0x00, 72, 4 },
109 [6] = { .phy = OFDM, 48000, 0x00, 96, 4 },
110 [7] = { .phy = OFDM, 54000, 0x00, 108, 4 }
111 },
112 };
113
114 static struct ieee80211_rate_table ieee80211_half_table = {
115 .rateCount = 8,
116 .info = {
117 /* short ctrl */
118 /* Preamble dot11Rate Rate */
119 [0] = { .phy = OFDM, 3000, 0x00, B(6), 0 },
120 [1] = { .phy = OFDM, 4500, 0x00, 9, 0 },
121 [2] = { .phy = OFDM, 6000, 0x00, B(12), 2 },
122 [3] = { .phy = OFDM, 9000, 0x00, 18, 2 },
123 [4] = { .phy = OFDM, 12000, 0x00, B(24), 4 },
124 [5] = { .phy = OFDM, 18000, 0x00, 36, 4 },
125 [6] = { .phy = OFDM, 24000, 0x00, 48, 4 },
126 [7] = { .phy = OFDM, 27000, 0x00, 54, 4 }
127 },
128 };
129
130 static struct ieee80211_rate_table ieee80211_quarter_table = {
131 .rateCount = 8,
132 .info = {
133 /* short ctrl */
134 /* Preamble dot11Rate Rate */
135 [0] = { .phy = OFDM, 1500, 0x00, B(3), 0 },
136 [1] = { .phy = OFDM, 2250, 0x00, 4, 0 },
137 [2] = { .phy = OFDM, 3000, 0x00, B(9), 2 },
138 [3] = { .phy = OFDM, 4500, 0x00, 9, 2 },
139 [4] = { .phy = OFDM, 6000, 0x00, B(12), 4 },
140 [5] = { .phy = OFDM, 9000, 0x00, 18, 4 },
141 [6] = { .phy = OFDM, 12000, 0x00, 24, 4 },
142 [7] = { .phy = OFDM, 13500, 0x00, 27, 4 }
143 },
144 };
145
146 static struct ieee80211_rate_table ieee80211_turbog_table = {
147 .rateCount = 7,
148 .info = {
149 /* short ctrl */
150 /* Preamble dot11Rate Rate */
151 [0] = { .phy = TURBO, 12000, 0x00, B(12), 0 },
152 [1] = { .phy = TURBO, 24000, 0x00, B(24), 1 },
153 [2] = { .phy = TURBO, 36000, 0x00, 36, 1 },
154 [3] = { .phy = TURBO, 48000, 0x00, B(48), 3 },
155 [4] = { .phy = TURBO, 72000, 0x00, 72, 3 },
156 [5] = { .phy = TURBO, 96000, 0x00, 96, 3 },
157 [6] = { .phy = TURBO, 108000, 0x00, 108, 3 }
158 },
159 };
160
161 static struct ieee80211_rate_table ieee80211_turboa_table = {
162 .rateCount = 8,
163 .info = {
164 /* short ctrl */
165 /* Preamble dot11Rate Rate */
166 [0] = { .phy = TURBO, 12000, 0x00, B(12), 0 },
167 [1] = { .phy = TURBO, 18000, 0x00, 18, 0 },
168 [2] = { .phy = TURBO, 24000, 0x00, B(24), 2 },
169 [3] = { .phy = TURBO, 36000, 0x00, 36, 2 },
170 [4] = { .phy = TURBO, 48000, 0x00, B(48), 4 },
171 [5] = { .phy = TURBO, 72000, 0x00, 72, 4 },
172 [6] = { .phy = TURBO, 96000, 0x00, 96, 4 },
173 [7] = { .phy = TURBO, 108000, 0x00, 108, 4 }
174 },
175 };
176
177 #undef Mb
178 #undef B
179 #undef OFDM
180 #undef CCK
181 #undef TURBO
182 #undef XR
183
184 /*
185 * Setup a rate table's reverse lookup table and fill in
186 * ack durations. The reverse lookup tables are assumed
187 * to be initialized to zero (or at least the first entry).
188 * We use this as a key that indicates whether or not
189 * we've previously setup the reverse lookup table.
190 *
191 * XXX not reentrant, but shouldn't matter
192 */
193 static void
194 ieee80211_setup_ratetable(struct ieee80211_rate_table *rt)
195 {
196 #define N(a) (sizeof(a)/sizeof(a[0]))
197 #define WLAN_CTRL_FRAME_SIZE \
198 (sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
199
200 int i;
201
202 for (i = 0; i < N(rt->rateCodeToIndex); i++)
203 rt->rateCodeToIndex[i] = (uint8_t) -1;
204 for (i = 0; i < rt->rateCount; i++) {
205 uint8_t code = rt->info[i].dot11Rate;
206 uint8_t cix = rt->info[i].ctlRateIndex;
207 uint8_t ctl_rate = rt->info[cix].dot11Rate;
208
209 rt->rateCodeToIndex[code] = i;
210 if (code & IEEE80211_RATE_BASIC) {
211 /*
212 * Map w/o basic rate bit too.
213 */
214 code &= IEEE80211_RATE_VAL;
215 rt->rateCodeToIndex[code] = i;
216 }
217
218 /*
219 * XXX for 11g the control rate to use for 5.5 and 11 Mb/s
220 * depends on whether they are marked as basic rates;
221 * the static tables are setup with an 11b-compatible
222 * 2Mb/s rate which will work but is suboptimal
223 *
224 * NB: Control rate is always less than or equal to the
225 * current rate, so control rate's reverse lookup entry
226 * has been installed and following call is safe.
227 */
228 rt->info[i].lpAckDuration = ieee80211_compute_duration(rt,
229 WLAN_CTRL_FRAME_SIZE, ctl_rate, 0);
230 rt->info[i].spAckDuration = ieee80211_compute_duration(rt,
231 WLAN_CTRL_FRAME_SIZE, ctl_rate, IEEE80211_F_SHPREAMBLE);
232 }
233
234 #undef WLAN_CTRL_FRAME_SIZE
235 #undef N
236 }
237
238 /* Setup all rate tables */
239 static void
240 ieee80211_phy_init(void)
241 {
242 #define N(arr) (int)(sizeof(arr) / sizeof(arr[0]))
243 static struct ieee80211_rate_table * const ratetables[] = {
244 &ieee80211_half_table,
245 &ieee80211_quarter_table,
246 &ieee80211_11a_table,
247 &ieee80211_11g_table,
248 &ieee80211_turbog_table,
249 &ieee80211_turboa_table,
250 &ieee80211_turboa_table,
251 &ieee80211_11a_table,
252 &ieee80211_11g_table,
253 &ieee80211_11b_table
254 };
255 int i;
256
257 for (i = 0; i < N(ratetables); ++i)
258 ieee80211_setup_ratetable(ratetables[i]);
259
260 #undef N
261 }
262 SYSINIT(wlan_phy, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_phy_init, NULL);
263
264 const struct ieee80211_rate_table *
265 ieee80211_get_ratetable(struct ieee80211_channel *c)
266 {
267 const struct ieee80211_rate_table *rt;
268
269 /* XXX HT */
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;
290 else {
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);
294 }
295 return rt;
296 }
297
298 /*
299 * Convert PLCP signal/rate field to 802.11 rate (.5Mbits/s)
300 *
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.
304 *
305 * XXX might be a candidate for inline
306 */
307 uint8_t
308 ieee80211_plcp2rate(uint8_t plcp, enum ieee80211_phytype type)
309 {
310 if (type == IEEE80211_T_OFDM) {
311 static const uint8_t ofdm_plcp2rate[16] = {
312 [0xb] = 12,
313 [0xf] = 18,
314 [0xa] = 24,
315 [0xe] = 36,
316 [0x9] = 48,
317 [0xd] = 72,
318 [0x8] = 96,
319 [0xc] = 108
320 };
321 return ofdm_plcp2rate[plcp & 0xf];
322 }
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 */
330 };
331 return cck_plcp2rate[plcp & 0xf];
332 }
333 return 0;
334 }
335
336 /*
337 * Covert 802.11 rate to PLCP signal.
338 */
339 uint8_t
340 ieee80211_rate2plcp(int rate, enum ieee80211_phytype type)
341 {
342 /* XXX ignore type for now since rates are unique */
343 switch (rate) {
344 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
345 case 12: return 0xb;
346 case 18: return 0xf;
347 case 24: return 0xa;
348 case 36: return 0xe;
349 case 48: return 0x9;
350 case 72: return 0xd;
351 case 96: return 0x8;
352 case 108: return 0xc;
353 /* CCK rates (IEEE Std 802.11b-1999 page 15, subclause 18.2.3.3) */
354 case 2: return 10;
355 case 4: return 20;
356 case 11: return 55;
357 case 22: return 110;
358 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */
359 case 44: return 220;
360 }
361 return 0; /* XXX unsupported/unknown rate */
362 }
363
364 /*
365 * Compute the time to transmit a frame of length frameLen bytes
366 * using the specified rate, phy, and short preamble setting.
367 * SIFS is included.
368 */
369 uint16_t
370 ieee80211_compute_duration(const struct ieee80211_rate_table *rt,
371 uint32_t frameLen, uint16_t rate, int isShortPreamble)
372 {
373 uint8_t rix = rt->rateCodeToIndex[rate];
374 uint32_t bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
375 uint32_t kbps;
376
377 KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate));
378 kbps = rt->info[rix].rateKbps;
379 if (kbps == 0) /* XXX bandaid for channel changes */
380 return 0;
381
382 switch (rt->info[rix].phy) {
383 case IEEE80211_T_CCK:
384 #define CCK_SIFS_TIME 10
385 #define CCK_PREAMBLE_BITS 144
386 #define CCK_PLCP_BITS 48
387 phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
388 if (isShortPreamble && rt->info[rix].shortPreamble)
389 phyTime >>= 1;
390 numBits = frameLen << 3;
391 txTime = CCK_SIFS_TIME + phyTime
392 + ((numBits * 1000)/kbps);
393 break;
394 #undef CCK_SIFS_TIME
395 #undef CCK_PREAMBLE_BITS
396 #undef CCK_PLCP_BITS
397
398 case IEEE80211_T_OFDM:
399 #define OFDM_SIFS_TIME 16
400 #define OFDM_PREAMBLE_TIME 20
401 #define OFDM_PLCP_BITS 22
402 #define OFDM_SYMBOL_TIME 4
403
404 #define OFDM_SIFS_TIME_HALF 32
405 #define OFDM_PREAMBLE_TIME_HALF 40
406 #define OFDM_PLCP_BITS_HALF 22
407 #define OFDM_SYMBOL_TIME_HALF 8
408
409 #define OFDM_SIFS_TIME_QUARTER 64
410 #define OFDM_PREAMBLE_TIME_QUARTER 80
411 #define OFDM_PLCP_BITS_QUARTER 22
412 #define OFDM_SYMBOL_TIME_QUARTER 16
413 if (rt == &ieee80211_half_table) {
414 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_QUARTER) / 1000;
415 KASSERT(bitsPerSymbol != 0, ("1/2 rate bps"));
416
417 numBits = OFDM_PLCP_BITS + (frameLen << 3);
418 numSymbols = howmany(numBits, bitsPerSymbol);
419 txTime = OFDM_SIFS_TIME_QUARTER
420 + OFDM_PREAMBLE_TIME_QUARTER
421 + (numSymbols * OFDM_SYMBOL_TIME_QUARTER);
422 } else if (rt == &ieee80211_quarter_table) {
423 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_HALF) / 1000;
424 KASSERT(bitsPerSymbol != 0, ("1/4 rate bps"));
425
426 numBits = OFDM_PLCP_BITS + (frameLen << 3);
427 numSymbols = howmany(numBits, bitsPerSymbol);
428 txTime = OFDM_SIFS_TIME_HALF
429 + OFDM_PREAMBLE_TIME_HALF
430 + (numSymbols * OFDM_SYMBOL_TIME_HALF);
431 } else { /* full rate channel */
432 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
433 KASSERT(bitsPerSymbol != 0, ("full rate bps"));
434
435 numBits = OFDM_PLCP_BITS + (frameLen << 3);
436 numSymbols = howmany(numBits, bitsPerSymbol);
437 txTime = OFDM_SIFS_TIME
438 + OFDM_PREAMBLE_TIME
439 + (numSymbols * OFDM_SYMBOL_TIME);
440 }
441 break;
442
443 #undef OFDM_SIFS_TIME
444 #undef OFDM_PREAMBLE_TIME
445 #undef OFDM_PLCP_BITS
446 #undef OFDM_SYMBOL_TIME
447
448 case IEEE80211_T_TURBO:
449 #define TURBO_SIFS_TIME 8
450 #define TURBO_PREAMBLE_TIME 14
451 #define TURBO_PLCP_BITS 22
452 #define TURBO_SYMBOL_TIME 4
453 /* we still save OFDM rates in kbps - so double them */
454 bitsPerSymbol = ((kbps << 1) * TURBO_SYMBOL_TIME) / 1000;
455 KASSERT(bitsPerSymbol != 0, ("turbo bps"));
456
457 numBits = TURBO_PLCP_BITS + (frameLen << 3);
458 numSymbols = howmany(numBits, bitsPerSymbol);
459 txTime = TURBO_SIFS_TIME + TURBO_PREAMBLE_TIME
460 + (numSymbols * TURBO_SYMBOL_TIME);
461 break;
462 #undef TURBO_SIFS_TIME
463 #undef TURBO_PREAMBLE_TIME
464 #undef TURBO_PLCP_BITS
465 #undef TURBO_SYMBOL_TIME
466
467 default:
468 panic("%s: unknown phy %u (rate %u)\n", __func__,
469 rt->info[rix].phy, rate);
470 break;
471 }
472 return txTime;
473 }
474
|