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