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$"); 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 63 static struct ieee80211_rate_table ieee80211_11b_table = { 64 4, /* number of rates, XXX no PBCC */ 65 { 0 }, 66 { 67 /* short ctrl */ 68 /* Preamble dot11Rate Rate */ 69 /* 1 Mb */ { CCK, 1000, 0x00, (0x80| 2), 0 }, 70 /* 2 Mb */ { CCK, 2000, 0x04, (0x80| 4), 1 }, 71 /* 5.5 Mb */ { CCK, 5500, 0x04, (0x80|11), 1 }, 72 /* 11 Mb */ { CCK, 11000, 0x04, (0x80|22), 1 }, 73 /* 22 Mb */ { PBCC, 22000, 0x04, 44, 3 } 74 }, 75 }; 76 77 78 static struct ieee80211_rate_table ieee80211_11g_table = { 79 12, /* number of rates */ 80 { 0 }, 81 { 82 /* short ctrl */ 83 /* Preamble dot11Rate Rate */ 84 /* 1 Mb */ { CCK, 1000, 0x00, (0x80| 2), 0 }, 85 /* 2 Mb */ { CCK, 2000, 0x04, (0x80| 4), 1 }, 86 /* 5.5 Mb */ { CCK, 5500, 0x04, (0x80|11), 2 }, 87 /* 11 Mb */ { CCK, 11000, 0x04, (0x80|22), 3 }, 88 /* 6 Mb */ { OFDM, 6000, 0x00, 12, 4 }, 89 /* 9 Mb */ { OFDM, 9000, 0x00, 18, 4 }, 90 /* 12 Mb */ { OFDM, 12000, 0x00, 24, 6 }, 91 /* 18 Mb */ { OFDM, 18000, 0x00, 36, 6 }, 92 /* 24 Mb */ { OFDM, 24000, 0x00, 48, 8 }, 93 /* 36 Mb */ { OFDM, 36000, 0x00, 72, 8 }, 94 /* 48 Mb */ { OFDM, 48000, 0x00, 96, 8 }, 95 /* 54 Mb */ { OFDM, 54000, 0x00, 108, 8 } 96 }, 97 }; 98 99 static struct ieee80211_rate_table ieee80211_11a_table = { 100 8, /* number of rates */ 101 { 0 }, 102 { 103 /* short ctrl */ 104 /* Preamble dot11Rate Rate */ 105 /* 6 Mb */ { OFDM, 6000, 0x00, (0x80|12), 0 }, 106 /* 9 Mb */ { OFDM, 9000, 0x00, 18, 0 }, 107 /* 12 Mb */ { OFDM, 12000, 0x00, (0x80|24), 2 }, 108 /* 18 Mb */ { OFDM, 18000, 0x00, 36, 2 }, 109 /* 24 Mb */ { OFDM, 24000, 0x00, (0x80|48), 4 }, 110 /* 36 Mb */ { OFDM, 36000, 0x00, 72, 4 }, 111 /* 48 Mb */ { OFDM, 48000, 0x00, 96, 4 }, 112 /* 54 Mb */ { OFDM, 54000, 0x00, 108, 4 } 113 }, 114 }; 115 116 static struct ieee80211_rate_table ieee80211_half_table = { 117 8, /* number of rates */ 118 { 0 }, 119 { 120 /* short ctrl */ 121 /* Preamble dot11Rate Rate */ 122 /* 6 Mb */ { OFDM, 3000, 0x00, (0x80| 6), 0 }, 123 /* 9 Mb */ { OFDM, 4500, 0x00, 9, 0 }, 124 /* 12 Mb */ { OFDM, 6000, 0x00, (0x80|12), 2 }, 125 /* 18 Mb */ { OFDM, 9000, 0x00, 18, 2 }, 126 /* 24 Mb */ { OFDM, 12000, 0x00, (0x80|24), 4 }, 127 /* 36 Mb */ { OFDM, 18000, 0x00, 36, 4 }, 128 /* 48 Mb */ { OFDM, 24000, 0x00, 48, 4 }, 129 /* 54 Mb */ { OFDM, 27000, 0x00, 54, 4 } 130 }, 131 }; 132 133 static struct ieee80211_rate_table ieee80211_quarter_table = { 134 8, /* number of rates */ 135 { 0 }, 136 { 137 /* short ctrl */ 138 /* Preamble dot11Rate Rate */ 139 /* 6 Mb */ { OFDM, 1500, 0x00, (0x80| 3), 0 }, 140 /* 9 Mb */ { OFDM, 2250, 0x00, 4, 0 }, 141 /* 12 Mb */ { OFDM, 3000, 0x00, (0x80| 6), 2 }, 142 /* 18 Mb */ { OFDM, 4500, 0x00, 9, 2 }, 143 /* 24 Mb */ { OFDM, 6000, 0x00, (0x80|12), 4 }, 144 /* 36 Mb */ { OFDM, 9000, 0x00, 18, 4 }, 145 /* 48 Mb */ { OFDM, 12000, 0x00, 24, 4 }, 146 /* 54 Mb */ { OFDM, 13500, 0x00, 27, 4 } 147 }, 148 }; 149 150 static struct ieee80211_rate_table ieee80211_turbog_table = { 151 7, /* number of rates */ 152 { 0 }, 153 { 154 /* short ctrl */ 155 /* Preamble dot11Rate Rate */ 156 /* 6 Mb */ { TURBO, 6000, 0x00, (0x80|12), 0 }, 157 /* 12 Mb */ { TURBO, 12000, 0x00, (0x80|24), 1 }, 158 /* 18 Mb */ { TURBO, 18000, 0x00, 36, 1 }, 159 /* 24 Mb */ { TURBO, 24000, 0x00, (0x80|48), 3 }, 160 /* 36 Mb */ { TURBO, 36000, 0x00, 72, 3 }, 161 /* 48 Mb */ { TURBO, 48000, 0x00, 96, 3 }, 162 /* 54 Mb */ { TURBO, 54000, 0x00, 108, 3 } 163 }, 164 }; 165 166 static struct ieee80211_rate_table ieee80211_turboa_table = { 167 8, /* number of rates */ 168 { 0 }, 169 { 170 /* short ctrl */ 171 /* Preamble dot11Rate Rate */ 172 /* 6 Mb */ { TURBO, 6000, 0x00, (0x80|12), 0 }, 173 /* 9 Mb */ { TURBO, 9000, 0x00, 18, 0 }, 174 /* 12 Mb */ { TURBO, 12000, 0x00, (0x80|24), 2 }, 175 /* 18 Mb */ { TURBO, 18000, 0x00, 36, 2 }, 176 /* 24 Mb */ { TURBO, 24000, 0x00, (0x80|48), 4 }, 177 /* 36 Mb */ { TURBO, 36000, 0x00, 72, 4 }, 178 /* 48 Mb */ { TURBO, 48000, 0x00, 96, 4 }, 179 /* 54 Mb */ { TURBO, 54000, 0x00, 108, 4 } 180 }, 181 }; 182 183 #undef OFDM 184 #undef CCK 185 #undef TURBO 186 #undef XR 187 188 /* 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. 194 * 195 * XXX not reentrant, but shouldn't matter 196 */ 197 static void 198 ieee80211_setup_ratetable(struct ieee80211_rate_table *rt) 199 { 200 #define N(a) (sizeof(a)/sizeof(a[0])) 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 < N(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 #undef N 240 } 241 242 /* Setup all rate tables */ 243 static void 244 ieee80211_phy_init(void) 245 { 246 #define N(arr) (int)(sizeof(arr) / sizeof(arr[0])) 247 static struct ieee80211_rate_table * const ratetables[] = { 248 &ieee80211_half_table, 249 &ieee80211_quarter_table, 250 &ieee80211_11a_table, 251 &ieee80211_11g_table, 252 &ieee80211_turbog_table, 253 &ieee80211_turboa_table, 254 &ieee80211_turboa_table, 255 &ieee80211_11a_table, 256 &ieee80211_11g_table, 257 &ieee80211_11b_table 258 }; 259 int i; 260 261 for (i = 0; i < N(ratetables); ++i) 262 ieee80211_setup_ratetable(ratetables[i]); 263 264 #undef N 265 } 266 SYSINIT(wlan_phy, SI_SUB_DRIVERS, SI_ORDER_FIRST, ieee80211_phy_init, NULL); 267 268 const struct ieee80211_rate_table * 269 ieee80211_get_ratetable(struct ieee80211_channel *c) 270 { 271 const struct ieee80211_rate_table *rt; 272 273 /* XXX HT */ 274 if (IEEE80211_IS_CHAN_HALF(c)) 275 rt = &ieee80211_half_table; 276 else if (IEEE80211_IS_CHAN_QUARTER(c)) 277 rt = &ieee80211_quarter_table; 278 else if (IEEE80211_IS_CHAN_HTA(c)) 279 rt = &ieee80211_11a_table; /* XXX */ 280 else if (IEEE80211_IS_CHAN_HTG(c)) 281 rt = &ieee80211_11g_table; /* XXX */ 282 else if (IEEE80211_IS_CHAN_108G(c)) 283 rt = &ieee80211_turbog_table; 284 else if (IEEE80211_IS_CHAN_ST(c)) 285 rt = &ieee80211_turboa_table; 286 else if (IEEE80211_IS_CHAN_TURBO(c)) 287 rt = &ieee80211_turboa_table; 288 else if (IEEE80211_IS_CHAN_A(c)) 289 rt = &ieee80211_11a_table; 290 else if (IEEE80211_IS_CHAN_ANYG(c)) 291 rt = &ieee80211_11g_table; 292 else if (IEEE80211_IS_CHAN_B(c)) 293 rt = &ieee80211_11b_table; 294 else { 295 /* NB: should not get here */ 296 panic("%s: no rate table for channel; freq %u flags 0x%x\n", 297 __func__, c->ic_freq, c->ic_flags); 298 } 299 return rt; 300 } 301 302 /* 303 * Convert PLCP signal/rate field to 802.11 rate (.5Mbits/s) 304 * 305 * Note we do no parameter checking; this routine is mainly 306 * used to derive an 802.11 rate for constructing radiotap 307 * header data for rx frames. 308 * 309 * XXX might be a candidate for inline 310 */ 311 uint8_t 312 ieee80211_plcp2rate(uint8_t plcp, enum ieee80211_phytype type) 313 { 314 if (type == IEEE80211_T_OFDM) { 315 static const uint8_t ofdm_plcp2rate[16] = { 316 [0xb] = 12, 317 [0xf] = 18, 318 [0xa] = 24, 319 [0xe] = 36, 320 [0x9] = 48, 321 [0xd] = 72, 322 [0x8] = 96, 323 [0xc] = 108 324 }; 325 return ofdm_plcp2rate[plcp & 0xf]; 326 } 327 if (type == IEEE80211_T_CCK) { 328 static const uint8_t cck_plcp2rate[16] = { 329 [0xa] = 2, /* 0x0a */ 330 [0x4] = 4, /* 0x14 */ 331 [0x7] = 11, /* 0x37 */ 332 [0xe] = 22, /* 0x6e */ 333 [0xc] = 44, /* 0xdc , actually PBCC */ 334 }; 335 return cck_plcp2rate[plcp & 0xf]; 336 } 337 return 0; 338 } 339 340 /* 341 * Covert 802.11 rate to PLCP signal. 342 */ 343 uint8_t 344 ieee80211_rate2plcp(int rate, enum ieee80211_phytype type) 345 { 346 /* XXX ignore type for now since rates are unique */ 347 switch (rate) { 348 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 349 case 12: return 0xb; 350 case 18: return 0xf; 351 case 24: return 0xa; 352 case 36: return 0xe; 353 case 48: return 0x9; 354 case 72: return 0xd; 355 case 96: return 0x8; 356 case 108: return 0xc; 357 /* CCK rates (IEEE Std 802.11b-1999 page 15, subclause 18.2.3.3) */ 358 case 2: return 10; 359 case 4: return 20; 360 case 11: return 55; 361 case 22: return 110; 362 /* IEEE Std 802.11g-2003 page 19, subclause 19.3.2.1 */ 363 case 44: return 220; 364 } 365 return 0; /* XXX unsupported/unknown rate */ 366 } 367 368 /* 369 * Compute the time to transmit a frame of length frameLen bytes 370 * using the specified rate, phy, and short preamble setting. 371 * SIFS is included. 372 */ 373 uint16_t 374 ieee80211_compute_duration(const struct ieee80211_rate_table *rt, 375 uint32_t frameLen, uint16_t rate, int isShortPreamble) 376 { 377 uint8_t rix = rt->rateCodeToIndex[rate]; 378 uint32_t bitsPerSymbol, numBits, numSymbols, phyTime, txTime; 379 uint32_t kbps; 380 381 KASSERT(rix != (uint8_t)-1, ("rate %d has no info", rate)); 382 kbps = rt->info[rix].rateKbps; 383 if (kbps == 0) /* XXX bandaid for channel changes */ 384 return 0; 385 386 switch (rt->info[rix].phy) { 387 case IEEE80211_T_CCK: 388 #define CCK_SIFS_TIME 10 389 #define CCK_PREAMBLE_BITS 144 390 #define CCK_PLCP_BITS 48 391 phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS; 392 if (isShortPreamble && rt->info[rix].shortPreamble) 393 phyTime >>= 1; 394 numBits = frameLen << 3; 395 txTime = CCK_SIFS_TIME + phyTime 396 + ((numBits * 1000)/kbps); 397 break; 398 #undef CCK_SIFS_TIME 399 #undef CCK_PREAMBLE_BITS 400 #undef CCK_PLCP_BITS 401 402 case IEEE80211_T_OFDM: 403 #define OFDM_SIFS_TIME 16 404 #define OFDM_PREAMBLE_TIME 20 405 #define OFDM_PLCP_BITS 22 406 #define OFDM_SYMBOL_TIME 4 407 408 #define OFDM_SIFS_TIME_HALF 32 409 #define OFDM_PREAMBLE_TIME_HALF 40 410 #define OFDM_PLCP_BITS_HALF 22 411 #define OFDM_SYMBOL_TIME_HALF 8 412 413 #define OFDM_SIFS_TIME_QUARTER 64 414 #define OFDM_PREAMBLE_TIME_QUARTER 80 415 #define OFDM_PLCP_BITS_QUARTER 22 416 #define OFDM_SYMBOL_TIME_QUARTER 16 417 if (rt == &ieee80211_half_table) { 418 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_QUARTER) / 1000; 419 KASSERT(bitsPerSymbol != 0, ("1/2 rate bps")); 420 421 numBits = OFDM_PLCP_BITS + (frameLen << 3); 422 numSymbols = howmany(numBits, bitsPerSymbol); 423 txTime = OFDM_SIFS_TIME_QUARTER 424 + OFDM_PREAMBLE_TIME_QUARTER 425 + (numSymbols * OFDM_SYMBOL_TIME_QUARTER); 426 } else if (rt == &ieee80211_quarter_table) { 427 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_HALF) / 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_SIFS_TIME_HALF 433 + OFDM_PREAMBLE_TIME_HALF 434 + (numSymbols * OFDM_SYMBOL_TIME_HALF); 435 } else { /* full rate channel */ 436 bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000; 437 KASSERT(bitsPerSymbol != 0, ("full rate bps")); 438 439 numBits = OFDM_PLCP_BITS + (frameLen << 3); 440 numSymbols = howmany(numBits, bitsPerSymbol); 441 txTime = OFDM_SIFS_TIME 442 + OFDM_PREAMBLE_TIME 443 + (numSymbols * OFDM_SYMBOL_TIME); 444 } 445 break; 446 447 #undef OFDM_SIFS_TIME 448 #undef OFDM_PREAMBLE_TIME 449 #undef OFDM_PLCP_BITS 450 #undef OFDM_SYMBOL_TIME 451 452 case IEEE80211_T_TURBO: 453 #define TURBO_SIFS_TIME 8 454 #define TURBO_PREAMBLE_TIME 14 455 #define TURBO_PLCP_BITS 22 456 #define TURBO_SYMBOL_TIME 4 457 /* we still save OFDM rates in kbps - so double them */ 458 bitsPerSymbol = ((kbps << 1) * TURBO_SYMBOL_TIME) / 1000; 459 KASSERT(bitsPerSymbol != 0, ("turbo bps")); 460 461 numBits = TURBO_PLCP_BITS + (frameLen << 3); 462 numSymbols = howmany(numBits, bitsPerSymbol); 463 txTime = TURBO_SIFS_TIME + TURBO_PREAMBLE_TIME 464 + (numSymbols * TURBO_SYMBOL_TIME); 465 break; 466 #undef TURBO_SIFS_TIME 467 #undef TURBO_PREAMBLE_TIME 468 #undef TURBO_PLCP_BITS 469 #undef TURBO_SYMBOL_TIME 470 471 default: 472 panic("%s: unknown phy %u (rate %u)\n", __func__, 473 rt->info[rix].phy, rate); 474 break; 475 } 476 return txTime; 477 } 478