1 /* 2 * Copyright 2008 Sun Microsystems, Inc. All rights reserved. 3 * Use is subject to license terms. 4 */ 5 6 /* 7 * Copyright (c) 2006 8 * Damien Bergamini <damien.bergamini@free.fr> 9 * 10 * Permission to use, copy, modify, and distribute this software for any 11 * purpose with or without fee is hereby granted, provided that the above 12 * copyright notice and this permission notice appear in all copies. 13 * 14 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 15 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 16 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 17 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 19 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 20 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 21 */ 22 23 /* 24 * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters. 25 */ 26 27 #include <sys/types.h> 28 #include <sys/byteorder.h> 29 #include <sys/conf.h> 30 #include <sys/cmn_err.h> 31 #include <sys/stat.h> 32 #include <sys/ddi.h> 33 #include <sys/sunddi.h> 34 #include <sys/strsubr.h> 35 #include <sys/ethernet.h> 36 #include <inet/common.h> 37 #include <inet/nd.h> 38 #include <inet/mi.h> 39 #include <sys/note.h> 40 #include <sys/stream.h> 41 #include <sys/strsun.h> 42 #include <sys/modctl.h> 43 #include <sys/devops.h> 44 #include <sys/dlpi.h> 45 #include <sys/mac.h> 46 #include <sys/mac_wifi.h> 47 #include <sys/net80211.h> 48 #include <sys/net80211_proto.h> 49 #include <sys/varargs.h> 50 #include <sys/policy.h> 51 #include <sys/pci.h> 52 53 #include "wpireg.h" 54 #include "wpivar.h" 55 #include <inet/wifi_ioctl.h> 56 57 #ifdef DEBUG 58 #define WPI_DEBUG_80211 (1 << 0) 59 #define WPI_DEBUG_CMD (1 << 1) 60 #define WPI_DEBUG_DMA (1 << 2) 61 #define WPI_DEBUG_EEPROM (1 << 3) 62 #define WPI_DEBUG_FW (1 << 4) 63 #define WPI_DEBUG_HW (1 << 5) 64 #define WPI_DEBUG_INTR (1 << 6) 65 #define WPI_DEBUG_MRR (1 << 7) 66 #define WPI_DEBUG_PIO (1 << 8) 67 #define WPI_DEBUG_RX (1 << 9) 68 #define WPI_DEBUG_SCAN (1 << 10) 69 #define WPI_DEBUG_TX (1 << 11) 70 #define WPI_DEBUG_RATECTL (1 << 12) 71 #define WPI_DEBUG_RADIO (1 << 13) 72 #define WPI_DEBUG_RESUME (1 << 14) 73 uint32_t wpi_dbg_flags = 0; 74 #define WPI_DBG(x) \ 75 wpi_dbg x 76 #else 77 #define WPI_DBG(x) 78 #endif 79 80 static void *wpi_soft_state_p = NULL; 81 static uint8_t wpi_fw_bin [] = { 82 #include "fw-wpi/ipw3945.ucode.hex" 83 }; 84 85 /* DMA attributes for a shared page */ 86 static ddi_dma_attr_t sh_dma_attr = { 87 DMA_ATTR_V0, /* version of this structure */ 88 0, /* lowest usable address */ 89 0xffffffffU, /* highest usable address */ 90 0xffffffffU, /* maximum DMAable byte count */ 91 0x1000, /* alignment in bytes */ 92 0x1000, /* burst sizes (any?) */ 93 1, /* minimum transfer */ 94 0xffffffffU, /* maximum transfer */ 95 0xffffffffU, /* maximum segment length */ 96 1, /* maximum number of segments */ 97 1, /* granularity */ 98 0, /* flags (reserved) */ 99 }; 100 101 /* DMA attributes for a ring descriptor */ 102 static ddi_dma_attr_t ring_desc_dma_attr = { 103 DMA_ATTR_V0, /* version of this structure */ 104 0, /* lowest usable address */ 105 0xffffffffU, /* highest usable address */ 106 0xffffffffU, /* maximum DMAable byte count */ 107 0x4000, /* alignment in bytes */ 108 0x100, /* burst sizes (any?) */ 109 1, /* minimum transfer */ 110 0xffffffffU, /* maximum transfer */ 111 0xffffffffU, /* maximum segment length */ 112 1, /* maximum number of segments */ 113 1, /* granularity */ 114 0, /* flags (reserved) */ 115 }; 116 117 118 /* DMA attributes for a tx cmd */ 119 static ddi_dma_attr_t tx_cmd_dma_attr = { 120 DMA_ATTR_V0, /* version of this structure */ 121 0, /* lowest usable address */ 122 0xffffffffU, /* highest usable address */ 123 0xffffffffU, /* maximum DMAable byte count */ 124 4, /* alignment in bytes */ 125 0x100, /* burst sizes (any?) */ 126 1, /* minimum transfer */ 127 0xffffffffU, /* maximum transfer */ 128 0xffffffffU, /* maximum segment length */ 129 1, /* maximum number of segments */ 130 1, /* granularity */ 131 0, /* flags (reserved) */ 132 }; 133 134 /* DMA attributes for a rx buffer */ 135 static ddi_dma_attr_t rx_buffer_dma_attr = { 136 DMA_ATTR_V0, /* version of this structure */ 137 0, /* lowest usable address */ 138 0xffffffffU, /* highest usable address */ 139 0xffffffffU, /* maximum DMAable byte count */ 140 1, /* alignment in bytes */ 141 0x100, /* burst sizes (any?) */ 142 1, /* minimum transfer */ 143 0xffffffffU, /* maximum transfer */ 144 0xffffffffU, /* maximum segment length */ 145 1, /* maximum number of segments */ 146 1, /* granularity */ 147 0, /* flags (reserved) */ 148 }; 149 150 /* 151 * DMA attributes for a tx buffer. 152 * the maximum number of segments is 4 for the hardware. 153 * now all the wifi drivers put the whole frame in a single 154 * descriptor, so we define the maximum number of segments 4, 155 * just the same as the rx_buffer. we consider leverage the HW 156 * ability in the future, that is why we don't define rx and tx 157 * buffer_dma_attr as the same. 158 */ 159 static ddi_dma_attr_t tx_buffer_dma_attr = { 160 DMA_ATTR_V0, /* version of this structure */ 161 0, /* lowest usable address */ 162 0xffffffffU, /* highest usable address */ 163 0xffffffffU, /* maximum DMAable byte count */ 164 1, /* alignment in bytes */ 165 0x100, /* burst sizes (any?) */ 166 1, /* minimum transfer */ 167 0xffffffffU, /* maximum transfer */ 168 0xffffffffU, /* maximum segment length */ 169 1, /* maximum number of segments */ 170 1, /* granularity */ 171 0, /* flags (reserved) */ 172 }; 173 174 /* DMA attributes for a load firmware */ 175 static ddi_dma_attr_t fw_buffer_dma_attr = { 176 DMA_ATTR_V0, /* version of this structure */ 177 0, /* lowest usable address */ 178 0xffffffffU, /* highest usable address */ 179 0x7fffffff, /* maximum DMAable byte count */ 180 4, /* alignment in bytes */ 181 0x100, /* burst sizes (any?) */ 182 1, /* minimum transfer */ 183 0xffffffffU, /* maximum transfer */ 184 0xffffffffU, /* maximum segment length */ 185 4, /* maximum number of segments */ 186 1, /* granularity */ 187 0, /* flags (reserved) */ 188 }; 189 190 /* regs access attributes */ 191 static ddi_device_acc_attr_t wpi_reg_accattr = { 192 DDI_DEVICE_ATTR_V0, 193 DDI_STRUCTURE_LE_ACC, 194 DDI_STRICTORDER_ACC, 195 DDI_DEFAULT_ACC 196 }; 197 198 /* DMA access attributes */ 199 static ddi_device_acc_attr_t wpi_dma_accattr = { 200 DDI_DEVICE_ATTR_V0, 201 DDI_NEVERSWAP_ACC, 202 DDI_STRICTORDER_ACC, 203 DDI_DEFAULT_ACC 204 }; 205 206 static int wpi_ring_init(wpi_sc_t *); 207 static void wpi_ring_free(wpi_sc_t *); 208 static int wpi_alloc_shared(wpi_sc_t *); 209 static void wpi_free_shared(wpi_sc_t *); 210 static int wpi_alloc_fw_dma(wpi_sc_t *); 211 static void wpi_free_fw_dma(wpi_sc_t *); 212 static int wpi_alloc_rx_ring(wpi_sc_t *); 213 static void wpi_reset_rx_ring(wpi_sc_t *); 214 static void wpi_free_rx_ring(wpi_sc_t *); 215 static int wpi_alloc_tx_ring(wpi_sc_t *, wpi_tx_ring_t *, int, int); 216 static void wpi_reset_tx_ring(wpi_sc_t *, wpi_tx_ring_t *); 217 static void wpi_free_tx_ring(wpi_sc_t *, wpi_tx_ring_t *); 218 219 static ieee80211_node_t *wpi_node_alloc(ieee80211com_t *); 220 static void wpi_node_free(ieee80211_node_t *); 221 static int wpi_newstate(ieee80211com_t *, enum ieee80211_state, int); 222 static int wpi_key_set(ieee80211com_t *, const struct ieee80211_key *, 223 const uint8_t mac[IEEE80211_ADDR_LEN]); 224 static void wpi_mem_lock(wpi_sc_t *); 225 static void wpi_mem_unlock(wpi_sc_t *); 226 static uint32_t wpi_mem_read(wpi_sc_t *, uint16_t); 227 static void wpi_mem_write(wpi_sc_t *, uint16_t, uint32_t); 228 static void wpi_mem_write_region_4(wpi_sc_t *, uint16_t, 229 const uint32_t *, int); 230 static uint16_t wpi_read_prom_word(wpi_sc_t *, uint32_t); 231 static int wpi_load_microcode(wpi_sc_t *); 232 static int wpi_load_firmware(wpi_sc_t *, uint32_t); 233 static void wpi_rx_intr(wpi_sc_t *, wpi_rx_desc_t *, 234 wpi_rx_data_t *); 235 static void wpi_tx_intr(wpi_sc_t *, wpi_rx_desc_t *, 236 wpi_rx_data_t *); 237 static void wpi_cmd_intr(wpi_sc_t *, wpi_rx_desc_t *); 238 static uint_t wpi_intr(caddr_t); 239 static uint_t wpi_notif_softintr(caddr_t); 240 static uint8_t wpi_plcp_signal(int); 241 static void wpi_read_eeprom(wpi_sc_t *); 242 static int wpi_cmd(wpi_sc_t *, int, const void *, int, int); 243 static int wpi_mrr_setup(wpi_sc_t *); 244 static void wpi_set_led(wpi_sc_t *, uint8_t, uint8_t, uint8_t); 245 static int wpi_auth(wpi_sc_t *); 246 static int wpi_scan(wpi_sc_t *); 247 static int wpi_config(wpi_sc_t *); 248 static void wpi_stop_master(wpi_sc_t *); 249 static int wpi_power_up(wpi_sc_t *); 250 static int wpi_reset(wpi_sc_t *); 251 static void wpi_hw_config(wpi_sc_t *); 252 static int wpi_init(wpi_sc_t *); 253 static void wpi_stop(wpi_sc_t *); 254 static void wpi_amrr_init(wpi_amrr_t *); 255 static void wpi_amrr_timeout(wpi_sc_t *); 256 static void wpi_amrr_ratectl(void *, ieee80211_node_t *); 257 258 static int wpi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd); 259 static int wpi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd); 260 261 /* 262 * GLD specific operations 263 */ 264 static int wpi_m_stat(void *arg, uint_t stat, uint64_t *val); 265 static int wpi_m_start(void *arg); 266 static void wpi_m_stop(void *arg); 267 static int wpi_m_unicst(void *arg, const uint8_t *macaddr); 268 static int wpi_m_multicst(void *arg, boolean_t add, const uint8_t *m); 269 static int wpi_m_promisc(void *arg, boolean_t on); 270 static mblk_t *wpi_m_tx(void *arg, mblk_t *mp); 271 static void wpi_m_ioctl(void *arg, queue_t *wq, mblk_t *mp); 272 static int wpi_m_setprop(void *arg, const char *pr_name, 273 mac_prop_id_t wldp_pr_num, uint_t wldp_length, const void *wldp_buf); 274 static int wpi_m_getprop(void *arg, const char *pr_name, 275 mac_prop_id_t wldp_pr_num, uint_t pr_flags, uint_t wldp_lenth, 276 void *wldp_buf, uint_t *); 277 static void wpi_destroy_locks(wpi_sc_t *sc); 278 static int wpi_send(ieee80211com_t *ic, mblk_t *mp, uint8_t type); 279 static void wpi_thread(wpi_sc_t *sc); 280 281 /* 282 * Supported rates for 802.11a/b/g modes (in 500Kbps unit). 283 */ 284 static const struct ieee80211_rateset wpi_rateset_11b = 285 { 4, { 2, 4, 11, 22 } }; 286 287 static const struct ieee80211_rateset wpi_rateset_11g = 288 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 289 290 static const uint8_t wpi_ridx_to_signal[] = { 291 /* OFDM: IEEE Std 802.11a-1999, pp. 14 Table 80 */ 292 /* R1-R4 (ral/ural is R4-R1) */ 293 0xd, 0xf, 0x5, 0x7, 0x9, 0xb, 0x1, 0x3, 294 /* CCK: device-dependent */ 295 10, 20, 55, 110 296 }; 297 298 /* 299 * For mfthread only 300 */ 301 extern pri_t minclsyspri; 302 303 /* 304 * Module Loading Data & Entry Points 305 */ 306 DDI_DEFINE_STREAM_OPS(wpi_devops, nulldev, nulldev, wpi_attach, 307 wpi_detach, nodev, NULL, D_MP, NULL, ddi_quiesce_not_supported); 308 309 static struct modldrv wpi_modldrv = { 310 &mod_driverops, 311 "Intel(R) PRO/Wireless 3945ABG driver", 312 &wpi_devops 313 }; 314 315 static struct modlinkage wpi_modlinkage = { 316 MODREV_1, 317 &wpi_modldrv, 318 NULL 319 }; 320 321 int 322 _init(void) 323 { 324 int status; 325 326 status = ddi_soft_state_init(&wpi_soft_state_p, 327 sizeof (wpi_sc_t), 1); 328 if (status != DDI_SUCCESS) 329 return (status); 330 331 mac_init_ops(&wpi_devops, "wpi"); 332 status = mod_install(&wpi_modlinkage); 333 if (status != DDI_SUCCESS) { 334 mac_fini_ops(&wpi_devops); 335 ddi_soft_state_fini(&wpi_soft_state_p); 336 } 337 338 return (status); 339 } 340 341 int 342 _fini(void) 343 { 344 int status; 345 346 status = mod_remove(&wpi_modlinkage); 347 if (status == DDI_SUCCESS) { 348 mac_fini_ops(&wpi_devops); 349 ddi_soft_state_fini(&wpi_soft_state_p); 350 } 351 352 return (status); 353 } 354 355 int 356 _info(struct modinfo *mip) 357 { 358 return (mod_info(&wpi_modlinkage, mip)); 359 } 360 361 /* 362 * Mac Call Back entries 363 */ 364 mac_callbacks_t wpi_m_callbacks = { 365 MC_IOCTL | MC_SETPROP | MC_GETPROP, 366 wpi_m_stat, 367 wpi_m_start, 368 wpi_m_stop, 369 wpi_m_promisc, 370 wpi_m_multicst, 371 wpi_m_unicst, 372 wpi_m_tx, 373 NULL, 374 wpi_m_ioctl, 375 NULL, 376 NULL, 377 NULL, 378 wpi_m_setprop, 379 wpi_m_getprop 380 }; 381 382 #ifdef DEBUG 383 void 384 wpi_dbg(uint32_t flags, const char *fmt, ...) 385 { 386 va_list ap; 387 388 if (flags & wpi_dbg_flags) { 389 va_start(ap, fmt); 390 vcmn_err(CE_NOTE, fmt, ap); 391 va_end(ap); 392 } 393 } 394 #endif 395 /* 396 * device operations 397 */ 398 int 399 wpi_attach(dev_info_t *dip, ddi_attach_cmd_t cmd) 400 { 401 wpi_sc_t *sc; 402 ddi_acc_handle_t cfg_handle; 403 caddr_t cfg_base; 404 ieee80211com_t *ic; 405 int instance, err, i; 406 char strbuf[32]; 407 wifi_data_t wd = { 0 }; 408 mac_register_t *macp; 409 410 switch (cmd) { 411 case DDI_ATTACH: 412 break; 413 case DDI_RESUME: 414 sc = ddi_get_soft_state(wpi_soft_state_p, 415 ddi_get_instance(dip)); 416 ASSERT(sc != NULL); 417 mutex_enter(&sc->sc_glock); 418 sc->sc_flags &= ~WPI_F_SUSPEND; 419 mutex_exit(&sc->sc_glock); 420 if (sc->sc_flags & WPI_F_RUNNING) { 421 (void) wpi_init(sc); 422 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1); 423 } 424 WPI_DBG((WPI_DEBUG_RESUME, "wpi: resume \n")); 425 return (DDI_SUCCESS); 426 default: 427 err = DDI_FAILURE; 428 goto attach_fail1; 429 } 430 431 instance = ddi_get_instance(dip); 432 err = ddi_soft_state_zalloc(wpi_soft_state_p, instance); 433 if (err != DDI_SUCCESS) { 434 cmn_err(CE_WARN, 435 "wpi_attach(): failed to allocate soft state\n"); 436 goto attach_fail1; 437 } 438 sc = ddi_get_soft_state(wpi_soft_state_p, instance); 439 sc->sc_dip = dip; 440 441 err = ddi_regs_map_setup(dip, 0, &cfg_base, 0, 0, 442 &wpi_reg_accattr, &cfg_handle); 443 if (err != DDI_SUCCESS) { 444 cmn_err(CE_WARN, 445 "wpi_attach(): failed to map config spaces regs\n"); 446 goto attach_fail2; 447 } 448 sc->sc_rev = ddi_get8(cfg_handle, 449 (uint8_t *)(cfg_base + PCI_CONF_REVID)); 450 ddi_put8(cfg_handle, (uint8_t *)(cfg_base + 0x41), 0); 451 sc->sc_clsz = ddi_get16(cfg_handle, 452 (uint16_t *)(cfg_base + PCI_CONF_CACHE_LINESZ)); 453 ddi_regs_map_free(&cfg_handle); 454 if (!sc->sc_clsz) 455 sc->sc_clsz = 16; 456 sc->sc_clsz = (sc->sc_clsz << 2); 457 sc->sc_dmabuf_sz = roundup(0x1000 + sizeof (struct ieee80211_frame) + 458 IEEE80211_MTU + IEEE80211_CRC_LEN + 459 (IEEE80211_WEP_IVLEN + IEEE80211_WEP_KIDLEN + 460 IEEE80211_WEP_CRCLEN), sc->sc_clsz); 461 /* 462 * Map operating registers 463 */ 464 err = ddi_regs_map_setup(dip, 1, &sc->sc_base, 465 0, 0, &wpi_reg_accattr, &sc->sc_handle); 466 if (err != DDI_SUCCESS) { 467 cmn_err(CE_WARN, 468 "wpi_attach(): failed to map device regs\n"); 469 goto attach_fail2; 470 } 471 472 /* 473 * Allocate shared page. 474 */ 475 err = wpi_alloc_shared(sc); 476 if (err != DDI_SUCCESS) { 477 cmn_err(CE_WARN, "failed to allocate shared page\n"); 478 goto attach_fail3; 479 } 480 481 /* 482 * Get the hw conf, including MAC address, then init all rings. 483 */ 484 wpi_read_eeprom(sc); 485 err = wpi_ring_init(sc); 486 if (err != DDI_SUCCESS) { 487 cmn_err(CE_WARN, "wpi_attach(): " 488 "failed to allocate and initialize ring\n"); 489 goto attach_fail4; 490 } 491 492 sc->sc_hdr = (const wpi_firmware_hdr_t *)wpi_fw_bin; 493 494 /* firmware image layout: |HDR|<--TEXT-->|<--DATA-->|<--BOOT-->| */ 495 sc->sc_text = (const char *)(sc->sc_hdr + 1); 496 sc->sc_data = sc->sc_text + LE_32(sc->sc_hdr->textsz); 497 sc->sc_boot = sc->sc_data + LE_32(sc->sc_hdr->datasz); 498 err = wpi_alloc_fw_dma(sc); 499 if (err != DDI_SUCCESS) { 500 cmn_err(CE_WARN, "wpi_attach(): " 501 "failed to allocate firmware dma\n"); 502 goto attach_fail5; 503 } 504 505 /* 506 * Initialize mutexs and condvars 507 */ 508 err = ddi_get_iblock_cookie(dip, 0, &sc->sc_iblk); 509 if (err != DDI_SUCCESS) { 510 cmn_err(CE_WARN, 511 "wpi_attach(): failed to do ddi_get_iblock_cookie()\n"); 512 goto attach_fail6; 513 } 514 mutex_init(&sc->sc_glock, NULL, MUTEX_DRIVER, sc->sc_iblk); 515 mutex_init(&sc->sc_tx_lock, NULL, MUTEX_DRIVER, sc->sc_iblk); 516 cv_init(&sc->sc_fw_cv, NULL, CV_DRIVER, NULL); 517 cv_init(&sc->sc_cmd_cv, NULL, CV_DRIVER, NULL); 518 519 /* 520 * initialize the mfthread 521 */ 522 mutex_init(&sc->sc_mt_lock, NULL, MUTEX_DRIVER, 523 (void *) sc->sc_iblk); 524 cv_init(&sc->sc_mt_cv, NULL, CV_DRIVER, NULL); 525 sc->sc_mf_thread = NULL; 526 sc->sc_mf_thread_switch = 0; 527 /* 528 * Initialize the wifi part, which will be used by 529 * generic layer 530 */ 531 ic = &sc->sc_ic; 532 ic->ic_phytype = IEEE80211_T_OFDM; 533 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 534 ic->ic_state = IEEE80211_S_INIT; 535 ic->ic_maxrssi = 70; /* experimental number */ 536 ic->ic_caps = IEEE80211_C_SHPREAMBLE | IEEE80211_C_TXPMGT | 537 IEEE80211_C_PMGT | IEEE80211_C_SHSLOT; 538 539 /* 540 * use software WEP and TKIP, hardware CCMP; 541 */ 542 ic->ic_caps |= IEEE80211_C_AES_CCM; 543 ic->ic_caps |= IEEE80211_C_WPA; /* Support WPA/WPA2 */ 544 545 /* set supported .11b and .11g rates */ 546 ic->ic_sup_rates[IEEE80211_MODE_11B] = wpi_rateset_11b; 547 ic->ic_sup_rates[IEEE80211_MODE_11G] = wpi_rateset_11g; 548 549 /* set supported .11b and .11g channels (1 through 14) */ 550 for (i = 1; i <= 14; i++) { 551 ic->ic_sup_channels[i].ich_freq = 552 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 553 ic->ic_sup_channels[i].ich_flags = 554 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 555 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ | 556 IEEE80211_CHAN_PASSIVE; 557 } 558 ic->ic_ibss_chan = &ic->ic_sup_channels[0]; 559 ic->ic_xmit = wpi_send; 560 /* 561 * init Wifi layer 562 */ 563 ieee80211_attach(ic); 564 565 /* register WPA door */ 566 ieee80211_register_door(ic, ddi_driver_name(dip), 567 ddi_get_instance(dip)); 568 569 /* 570 * Override 80211 default routines 571 */ 572 sc->sc_newstate = ic->ic_newstate; 573 ic->ic_newstate = wpi_newstate; 574 ic->ic_node_alloc = wpi_node_alloc; 575 ic->ic_node_free = wpi_node_free; 576 ic->ic_crypto.cs_key_set = wpi_key_set; 577 ieee80211_media_init(ic); 578 /* 579 * initialize default tx key 580 */ 581 ic->ic_def_txkey = 0; 582 583 err = ddi_add_softintr(dip, DDI_SOFTINT_LOW, 584 &sc->sc_notif_softint_id, &sc->sc_iblk, NULL, wpi_notif_softintr, 585 (caddr_t)sc); 586 if (err != DDI_SUCCESS) { 587 cmn_err(CE_WARN, 588 "wpi_attach(): failed to do ddi_add_softintr()\n"); 589 goto attach_fail7; 590 } 591 592 /* 593 * Add the interrupt handler 594 */ 595 err = ddi_add_intr(dip, 0, &sc->sc_iblk, NULL, 596 wpi_intr, (caddr_t)sc); 597 if (err != DDI_SUCCESS) { 598 cmn_err(CE_WARN, 599 "wpi_attach(): failed to do ddi_add_intr()\n"); 600 goto attach_fail8; 601 } 602 603 /* 604 * Initialize pointer to device specific functions 605 */ 606 wd.wd_secalloc = WIFI_SEC_NONE; 607 wd.wd_opmode = ic->ic_opmode; 608 IEEE80211_ADDR_COPY(wd.wd_bssid, ic->ic_macaddr); 609 610 macp = mac_alloc(MAC_VERSION); 611 if (err != DDI_SUCCESS) { 612 cmn_err(CE_WARN, 613 "wpi_attach(): failed to do mac_alloc()\n"); 614 goto attach_fail9; 615 } 616 617 macp->m_type_ident = MAC_PLUGIN_IDENT_WIFI; 618 macp->m_driver = sc; 619 macp->m_dip = dip; 620 macp->m_src_addr = ic->ic_macaddr; 621 macp->m_callbacks = &wpi_m_callbacks; 622 macp->m_min_sdu = 0; 623 macp->m_max_sdu = IEEE80211_MTU; 624 macp->m_pdata = &wd; 625 macp->m_pdata_size = sizeof (wd); 626 627 /* 628 * Register the macp to mac 629 */ 630 err = mac_register(macp, &ic->ic_mach); 631 mac_free(macp); 632 if (err != DDI_SUCCESS) { 633 cmn_err(CE_WARN, 634 "wpi_attach(): failed to do mac_register()\n"); 635 goto attach_fail9; 636 } 637 638 /* 639 * Create minor node of type DDI_NT_NET_WIFI 640 */ 641 (void) snprintf(strbuf, sizeof (strbuf), "wpi%d", instance); 642 err = ddi_create_minor_node(dip, strbuf, S_IFCHR, 643 instance + 1, DDI_NT_NET_WIFI, 0); 644 if (err != DDI_SUCCESS) 645 cmn_err(CE_WARN, 646 "wpi_attach(): failed to do ddi_create_minor_node()\n"); 647 648 /* 649 * Notify link is down now 650 */ 651 mac_link_update(ic->ic_mach, LINK_STATE_DOWN); 652 653 /* 654 * create the mf thread to handle the link status, 655 * recovery fatal error, etc. 656 */ 657 658 sc->sc_mf_thread_switch = 1; 659 if (sc->sc_mf_thread == NULL) 660 sc->sc_mf_thread = thread_create((caddr_t)NULL, 0, 661 wpi_thread, sc, 0, &p0, TS_RUN, minclsyspri); 662 663 sc->sc_flags |= WPI_F_ATTACHED; 664 665 return (DDI_SUCCESS); 666 attach_fail9: 667 ddi_remove_intr(dip, 0, sc->sc_iblk); 668 attach_fail8: 669 ddi_remove_softintr(sc->sc_notif_softint_id); 670 sc->sc_notif_softint_id = NULL; 671 attach_fail7: 672 ieee80211_detach(ic); 673 wpi_destroy_locks(sc); 674 attach_fail6: 675 wpi_free_fw_dma(sc); 676 attach_fail5: 677 wpi_ring_free(sc); 678 attach_fail4: 679 wpi_free_shared(sc); 680 attach_fail3: 681 ddi_regs_map_free(&sc->sc_handle); 682 attach_fail2: 683 ddi_soft_state_free(wpi_soft_state_p, instance); 684 attach_fail1: 685 return (err); 686 } 687 688 int 689 wpi_detach(dev_info_t *dip, ddi_detach_cmd_t cmd) 690 { 691 wpi_sc_t *sc; 692 int err; 693 694 sc = ddi_get_soft_state(wpi_soft_state_p, ddi_get_instance(dip)); 695 ASSERT(sc != NULL); 696 697 switch (cmd) { 698 case DDI_DETACH: 699 break; 700 case DDI_SUSPEND: 701 if (sc->sc_flags & WPI_F_RUNNING) { 702 wpi_stop(sc); 703 } 704 mutex_enter(&sc->sc_glock); 705 sc->sc_flags |= WPI_F_SUSPEND; 706 mutex_exit(&sc->sc_glock); 707 WPI_DBG((WPI_DEBUG_RESUME, "wpi: suspend \n")); 708 return (DDI_SUCCESS); 709 default: 710 return (DDI_FAILURE); 711 } 712 if (!(sc->sc_flags & WPI_F_ATTACHED)) 713 return (DDI_FAILURE); 714 715 err = mac_disable(sc->sc_ic.ic_mach); 716 if (err != DDI_SUCCESS) 717 return (err); 718 719 /* 720 * Destroy the mf_thread 721 */ 722 mutex_enter(&sc->sc_mt_lock); 723 sc->sc_mf_thread_switch = 0; 724 while (sc->sc_mf_thread != NULL) { 725 if (cv_wait_sig(&sc->sc_mt_cv, &sc->sc_mt_lock) == 0) 726 break; 727 } 728 mutex_exit(&sc->sc_mt_lock); 729 730 wpi_stop(sc); 731 732 /* 733 * Unregiste from the MAC layer subsystem 734 */ 735 (void) mac_unregister(sc->sc_ic.ic_mach); 736 737 mutex_enter(&sc->sc_glock); 738 wpi_free_fw_dma(sc); 739 wpi_ring_free(sc); 740 wpi_free_shared(sc); 741 mutex_exit(&sc->sc_glock); 742 743 ddi_remove_intr(dip, 0, sc->sc_iblk); 744 ddi_remove_softintr(sc->sc_notif_softint_id); 745 sc->sc_notif_softint_id = NULL; 746 747 /* 748 * detach ieee80211 749 */ 750 ieee80211_detach(&sc->sc_ic); 751 752 wpi_destroy_locks(sc); 753 754 ddi_regs_map_free(&sc->sc_handle); 755 ddi_remove_minor_node(dip, NULL); 756 ddi_soft_state_free(wpi_soft_state_p, ddi_get_instance(dip)); 757 758 return (DDI_SUCCESS); 759 } 760 761 static void 762 wpi_destroy_locks(wpi_sc_t *sc) 763 { 764 cv_destroy(&sc->sc_mt_cv); 765 mutex_destroy(&sc->sc_mt_lock); 766 cv_destroy(&sc->sc_cmd_cv); 767 cv_destroy(&sc->sc_fw_cv); 768 mutex_destroy(&sc->sc_tx_lock); 769 mutex_destroy(&sc->sc_glock); 770 } 771 772 /* 773 * Allocate an area of memory and a DMA handle for accessing it 774 */ 775 static int 776 wpi_alloc_dma_mem(wpi_sc_t *sc, size_t memsize, ddi_dma_attr_t *dma_attr_p, 777 ddi_device_acc_attr_t *acc_attr_p, uint_t dma_flags, wpi_dma_t *dma_p) 778 { 779 caddr_t vaddr; 780 int err; 781 782 /* 783 * Allocate handle 784 */ 785 err = ddi_dma_alloc_handle(sc->sc_dip, dma_attr_p, 786 DDI_DMA_SLEEP, NULL, &dma_p->dma_hdl); 787 if (err != DDI_SUCCESS) { 788 dma_p->dma_hdl = NULL; 789 return (DDI_FAILURE); 790 } 791 792 /* 793 * Allocate memory 794 */ 795 err = ddi_dma_mem_alloc(dma_p->dma_hdl, memsize, acc_attr_p, 796 dma_flags & (DDI_DMA_CONSISTENT | DDI_DMA_STREAMING), 797 DDI_DMA_SLEEP, NULL, &vaddr, &dma_p->alength, &dma_p->acc_hdl); 798 if (err != DDI_SUCCESS) { 799 ddi_dma_free_handle(&dma_p->dma_hdl); 800 dma_p->dma_hdl = NULL; 801 dma_p->acc_hdl = NULL; 802 return (DDI_FAILURE); 803 } 804 805 /* 806 * Bind the two together 807 */ 808 dma_p->mem_va = vaddr; 809 err = ddi_dma_addr_bind_handle(dma_p->dma_hdl, NULL, 810 vaddr, dma_p->alength, dma_flags, DDI_DMA_SLEEP, NULL, 811 &dma_p->cookie, &dma_p->ncookies); 812 if (err != DDI_DMA_MAPPED) { 813 ddi_dma_mem_free(&dma_p->acc_hdl); 814 ddi_dma_free_handle(&dma_p->dma_hdl); 815 dma_p->acc_hdl = NULL; 816 dma_p->dma_hdl = NULL; 817 return (DDI_FAILURE); 818 } 819 820 dma_p->nslots = ~0U; 821 dma_p->size = ~0U; 822 dma_p->token = ~0U; 823 dma_p->offset = 0; 824 return (DDI_SUCCESS); 825 } 826 827 /* 828 * Free one allocated area of DMAable memory 829 */ 830 static void 831 wpi_free_dma_mem(wpi_dma_t *dma_p) 832 { 833 if (dma_p->dma_hdl != NULL) { 834 if (dma_p->ncookies) { 835 (void) ddi_dma_unbind_handle(dma_p->dma_hdl); 836 dma_p->ncookies = 0; 837 } 838 ddi_dma_free_handle(&dma_p->dma_hdl); 839 dma_p->dma_hdl = NULL; 840 } 841 842 if (dma_p->acc_hdl != NULL) { 843 ddi_dma_mem_free(&dma_p->acc_hdl); 844 dma_p->acc_hdl = NULL; 845 } 846 } 847 848 /* 849 * Allocate an area of dma memory for firmware load. 850 * Idealy, this allocation should be a one time action, that is, 851 * the memory will be freed after the firmware is uploaded to the 852 * card. but since a recovery mechanism for the fatal firmware need 853 * reload the firmware, and re-allocate dma at run time may be failed, 854 * so we allocate it at attach and keep it in the whole lifecycle of 855 * the driver. 856 */ 857 static int 858 wpi_alloc_fw_dma(wpi_sc_t *sc) 859 { 860 int i, err = DDI_SUCCESS; 861 wpi_dma_t *dma_p; 862 863 err = wpi_alloc_dma_mem(sc, LE_32(sc->sc_hdr->textsz), 864 &fw_buffer_dma_attr, &wpi_dma_accattr, 865 DDI_DMA_RDWR | DDI_DMA_CONSISTENT, 866 &sc->sc_dma_fw_text); 867 dma_p = &sc->sc_dma_fw_text; 868 WPI_DBG((WPI_DEBUG_DMA, "ncookies:%d addr1:%x size1:%x\n", 869 dma_p->ncookies, dma_p->cookie.dmac_address, 870 dma_p->cookie.dmac_size)); 871 if (err != DDI_SUCCESS) { 872 cmn_err(CE_WARN, "wpi_alloc_fw_dma(): failed to alloc" 873 "text dma memory"); 874 goto fail; 875 } 876 for (i = 0; i < dma_p->ncookies; i++) { 877 sc->sc_fw_text_cookie[i] = dma_p->cookie; 878 ddi_dma_nextcookie(dma_p->dma_hdl, &dma_p->cookie); 879 } 880 err = wpi_alloc_dma_mem(sc, LE_32(sc->sc_hdr->datasz), 881 &fw_buffer_dma_attr, &wpi_dma_accattr, 882 DDI_DMA_RDWR | DDI_DMA_CONSISTENT, 883 &sc->sc_dma_fw_data); 884 dma_p = &sc->sc_dma_fw_data; 885 WPI_DBG((WPI_DEBUG_DMA, "ncookies:%d addr1:%x size1:%x\n", 886 dma_p->ncookies, dma_p->cookie.dmac_address, 887 dma_p->cookie.dmac_size)); 888 if (err != DDI_SUCCESS) { 889 cmn_err(CE_WARN, "wpi_alloc_fw_dma(): failed to alloc" 890 "data dma memory"); 891 goto fail; 892 } 893 for (i = 0; i < dma_p->ncookies; i++) { 894 sc->sc_fw_data_cookie[i] = dma_p->cookie; 895 ddi_dma_nextcookie(dma_p->dma_hdl, &dma_p->cookie); 896 } 897 fail: 898 return (err); 899 } 900 901 static void 902 wpi_free_fw_dma(wpi_sc_t *sc) 903 { 904 wpi_free_dma_mem(&sc->sc_dma_fw_text); 905 wpi_free_dma_mem(&sc->sc_dma_fw_data); 906 } 907 908 /* 909 * Allocate a shared page between host and NIC. 910 */ 911 static int 912 wpi_alloc_shared(wpi_sc_t *sc) 913 { 914 int err = DDI_SUCCESS; 915 916 /* must be aligned on a 4K-page boundary */ 917 err = wpi_alloc_dma_mem(sc, sizeof (wpi_shared_t), 918 &sh_dma_attr, &wpi_dma_accattr, 919 DDI_DMA_RDWR | DDI_DMA_CONSISTENT, 920 &sc->sc_dma_sh); 921 if (err != DDI_SUCCESS) 922 goto fail; 923 sc->sc_shared = (wpi_shared_t *)sc->sc_dma_sh.mem_va; 924 return (err); 925 926 fail: 927 wpi_free_shared(sc); 928 return (err); 929 } 930 931 static void 932 wpi_free_shared(wpi_sc_t *sc) 933 { 934 wpi_free_dma_mem(&sc->sc_dma_sh); 935 } 936 937 static int 938 wpi_alloc_rx_ring(wpi_sc_t *sc) 939 { 940 wpi_rx_ring_t *ring; 941 wpi_rx_data_t *data; 942 int i, err = DDI_SUCCESS; 943 944 ring = &sc->sc_rxq; 945 ring->cur = 0; 946 947 err = wpi_alloc_dma_mem(sc, WPI_RX_RING_COUNT * sizeof (uint32_t), 948 &ring_desc_dma_attr, &wpi_dma_accattr, 949 DDI_DMA_RDWR | DDI_DMA_CONSISTENT, 950 &ring->dma_desc); 951 if (err != DDI_SUCCESS) { 952 WPI_DBG((WPI_DEBUG_DMA, "dma alloc rx ring desc failed\n")); 953 goto fail; 954 } 955 ring->desc = (uint32_t *)ring->dma_desc.mem_va; 956 957 /* 958 * Allocate Rx buffers. 959 */ 960 for (i = 0; i < WPI_RX_RING_COUNT; i++) { 961 data = &ring->data[i]; 962 err = wpi_alloc_dma_mem(sc, sc->sc_dmabuf_sz, 963 &rx_buffer_dma_attr, &wpi_dma_accattr, 964 DDI_DMA_READ | DDI_DMA_STREAMING, 965 &data->dma_data); 966 if (err != DDI_SUCCESS) { 967 WPI_DBG((WPI_DEBUG_DMA, "dma alloc rx ring buf[%d] " 968 "failed\n", i)); 969 goto fail; 970 } 971 972 ring->desc[i] = LE_32(data->dma_data.cookie.dmac_address); 973 } 974 975 WPI_DMA_SYNC(ring->dma_desc, DDI_DMA_SYNC_FORDEV); 976 977 return (err); 978 979 fail: 980 wpi_free_rx_ring(sc); 981 return (err); 982 } 983 984 static void 985 wpi_reset_rx_ring(wpi_sc_t *sc) 986 { 987 int ntries; 988 989 wpi_mem_lock(sc); 990 991 WPI_WRITE(sc, WPI_RX_CONFIG, 0); 992 for (ntries = 0; ntries < 2000; ntries++) { 993 if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE) 994 break; 995 DELAY(1000); 996 } 997 if (ntries == 2000) 998 WPI_DBG((WPI_DEBUG_DMA, "timeout resetting Rx ring\n")); 999 1000 wpi_mem_unlock(sc); 1001 1002 sc->sc_rxq.cur = 0; 1003 } 1004 1005 static void 1006 wpi_free_rx_ring(wpi_sc_t *sc) 1007 { 1008 int i; 1009 1010 for (i = 0; i < WPI_RX_RING_COUNT; i++) { 1011 if (sc->sc_rxq.data[i].dma_data.dma_hdl) 1012 WPI_DMA_SYNC(sc->sc_rxq.data[i].dma_data, 1013 DDI_DMA_SYNC_FORCPU); 1014 wpi_free_dma_mem(&sc->sc_rxq.data[i].dma_data); 1015 } 1016 1017 if (sc->sc_rxq.dma_desc.dma_hdl) 1018 WPI_DMA_SYNC(sc->sc_rxq.dma_desc, DDI_DMA_SYNC_FORDEV); 1019 wpi_free_dma_mem(&sc->sc_rxq.dma_desc); 1020 } 1021 1022 static int 1023 wpi_alloc_tx_ring(wpi_sc_t *sc, wpi_tx_ring_t *ring, int count, int qid) 1024 { 1025 wpi_tx_data_t *data; 1026 wpi_tx_desc_t *desc_h; 1027 uint32_t paddr_desc_h; 1028 wpi_tx_cmd_t *cmd_h; 1029 uint32_t paddr_cmd_h; 1030 int i, err = DDI_SUCCESS; 1031 1032 ring->qid = qid; 1033 ring->count = count; 1034 ring->queued = 0; 1035 ring->cur = 0; 1036 1037 err = wpi_alloc_dma_mem(sc, count * sizeof (wpi_tx_desc_t), 1038 &ring_desc_dma_attr, &wpi_dma_accattr, 1039 DDI_DMA_RDWR | DDI_DMA_CONSISTENT, 1040 &ring->dma_desc); 1041 if (err != DDI_SUCCESS) { 1042 WPI_DBG((WPI_DEBUG_DMA, "dma alloc tx ring desc[%d] failed\n", 1043 qid)); 1044 goto fail; 1045 } 1046 1047 /* update shared page with ring's base address */ 1048 sc->sc_shared->txbase[qid] = ring->dma_desc.cookie.dmac_address; 1049 1050 desc_h = (wpi_tx_desc_t *)ring->dma_desc.mem_va; 1051 paddr_desc_h = ring->dma_desc.cookie.dmac_address; 1052 1053 err = wpi_alloc_dma_mem(sc, count * sizeof (wpi_tx_cmd_t), 1054 &tx_cmd_dma_attr, &wpi_dma_accattr, 1055 DDI_DMA_RDWR | DDI_DMA_CONSISTENT, 1056 &ring->dma_cmd); 1057 if (err != DDI_SUCCESS) { 1058 WPI_DBG((WPI_DEBUG_DMA, "dma alloc tx ring cmd[%d] failed\n", 1059 qid)); 1060 goto fail; 1061 } 1062 1063 cmd_h = (wpi_tx_cmd_t *)ring->dma_cmd.mem_va; 1064 paddr_cmd_h = ring->dma_cmd.cookie.dmac_address; 1065 1066 /* 1067 * Allocate Tx buffers. 1068 */ 1069 ring->data = kmem_zalloc(sizeof (wpi_tx_data_t) * count, KM_NOSLEEP); 1070 if (ring->data == NULL) { 1071 WPI_DBG((WPI_DEBUG_DMA, "could not allocate tx data slots\n")); 1072 goto fail; 1073 } 1074 1075 for (i = 0; i < count; i++) { 1076 data = &ring->data[i]; 1077 err = wpi_alloc_dma_mem(sc, sc->sc_dmabuf_sz, 1078 &tx_buffer_dma_attr, &wpi_dma_accattr, 1079 DDI_DMA_WRITE | DDI_DMA_STREAMING, 1080 &data->dma_data); 1081 if (err != DDI_SUCCESS) { 1082 WPI_DBG((WPI_DEBUG_DMA, "dma alloc tx ring buf[%d] " 1083 "failed\n", i)); 1084 goto fail; 1085 } 1086 1087 data->desc = desc_h + i; 1088 data->paddr_desc = paddr_desc_h + 1089 ((uintptr_t)data->desc - (uintptr_t)desc_h); 1090 data->cmd = cmd_h + i; 1091 data->paddr_cmd = paddr_cmd_h + 1092 ((uintptr_t)data->cmd - (uintptr_t)cmd_h); 1093 } 1094 1095 return (err); 1096 1097 fail: 1098 wpi_free_tx_ring(sc, ring); 1099 return (err); 1100 } 1101 1102 static void 1103 wpi_reset_tx_ring(wpi_sc_t *sc, wpi_tx_ring_t *ring) 1104 { 1105 wpi_tx_data_t *data; 1106 int i, ntries; 1107 1108 wpi_mem_lock(sc); 1109 1110 WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0); 1111 for (ntries = 0; ntries < 100; ntries++) { 1112 if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid)) 1113 break; 1114 DELAY(10); 1115 } 1116 #ifdef DEBUG 1117 if (ntries == 100 && wpi_dbg_flags > 0) { 1118 WPI_DBG((WPI_DEBUG_DMA, "timeout resetting Tx ring %d\n", 1119 ring->qid)); 1120 } 1121 #endif 1122 wpi_mem_unlock(sc); 1123 1124 for (i = 0; i < ring->count; i++) { 1125 data = &ring->data[i]; 1126 WPI_DMA_SYNC(data->dma_data, DDI_DMA_SYNC_FORDEV); 1127 } 1128 1129 ring->queued = 0; 1130 ring->cur = 0; 1131 } 1132 1133 /*ARGSUSED*/ 1134 static void 1135 wpi_free_tx_ring(wpi_sc_t *sc, wpi_tx_ring_t *ring) 1136 { 1137 int i; 1138 1139 if (ring->dma_desc.dma_hdl != NULL) 1140 WPI_DMA_SYNC(ring->dma_desc, DDI_DMA_SYNC_FORDEV); 1141 wpi_free_dma_mem(&ring->dma_desc); 1142 1143 if (ring->dma_cmd.dma_hdl != NULL) 1144 WPI_DMA_SYNC(ring->dma_cmd, DDI_DMA_SYNC_FORDEV); 1145 wpi_free_dma_mem(&ring->dma_cmd); 1146 1147 if (ring->data != NULL) { 1148 for (i = 0; i < ring->count; i++) { 1149 if (ring->data[i].dma_data.dma_hdl) 1150 WPI_DMA_SYNC(ring->data[i].dma_data, 1151 DDI_DMA_SYNC_FORDEV); 1152 wpi_free_dma_mem(&ring->data[i].dma_data); 1153 } 1154 kmem_free(ring->data, ring->count * sizeof (wpi_tx_data_t)); 1155 ring->data = NULL; 1156 } 1157 } 1158 1159 static int 1160 wpi_ring_init(wpi_sc_t *sc) 1161 { 1162 int i, err = DDI_SUCCESS; 1163 1164 for (i = 0; i < 4; i++) { 1165 err = wpi_alloc_tx_ring(sc, &sc->sc_txq[i], WPI_TX_RING_COUNT, 1166 i); 1167 if (err != DDI_SUCCESS) 1168 goto fail; 1169 } 1170 err = wpi_alloc_tx_ring(sc, &sc->sc_cmdq, WPI_CMD_RING_COUNT, 4); 1171 if (err != DDI_SUCCESS) 1172 goto fail; 1173 err = wpi_alloc_tx_ring(sc, &sc->sc_svcq, WPI_SVC_RING_COUNT, 5); 1174 if (err != DDI_SUCCESS) 1175 goto fail; 1176 err = wpi_alloc_rx_ring(sc); 1177 if (err != DDI_SUCCESS) 1178 goto fail; 1179 return (err); 1180 1181 fail: 1182 return (err); 1183 } 1184 1185 static void 1186 wpi_ring_free(wpi_sc_t *sc) 1187 { 1188 int i = 4; 1189 1190 wpi_free_rx_ring(sc); 1191 wpi_free_tx_ring(sc, &sc->sc_svcq); 1192 wpi_free_tx_ring(sc, &sc->sc_cmdq); 1193 while (--i >= 0) { 1194 wpi_free_tx_ring(sc, &sc->sc_txq[i]); 1195 } 1196 } 1197 1198 /* ARGSUSED */ 1199 static ieee80211_node_t * 1200 wpi_node_alloc(ieee80211com_t *ic) 1201 { 1202 wpi_amrr_t *amrr; 1203 1204 amrr = kmem_zalloc(sizeof (wpi_amrr_t), KM_SLEEP); 1205 if (amrr != NULL) 1206 wpi_amrr_init(amrr); 1207 return (&amrr->in); 1208 } 1209 1210 static void 1211 wpi_node_free(ieee80211_node_t *in) 1212 { 1213 ieee80211com_t *ic = in->in_ic; 1214 1215 ic->ic_node_cleanup(in); 1216 if (in->in_wpa_ie != NULL) 1217 ieee80211_free(in->in_wpa_ie); 1218 kmem_free(in, sizeof (wpi_amrr_t)); 1219 } 1220 1221 /*ARGSUSED*/ 1222 static int 1223 wpi_newstate(ieee80211com_t *ic, enum ieee80211_state nstate, int arg) 1224 { 1225 wpi_sc_t *sc = (wpi_sc_t *)ic; 1226 ieee80211_node_t *in = ic->ic_bss; 1227 enum ieee80211_state ostate; 1228 int i, err = WPI_SUCCESS; 1229 1230 mutex_enter(&sc->sc_glock); 1231 ostate = ic->ic_state; 1232 switch (nstate) { 1233 case IEEE80211_S_SCAN: 1234 switch (ostate) { 1235 case IEEE80211_S_INIT: 1236 { 1237 wpi_node_t node; 1238 1239 sc->sc_flags |= WPI_F_SCANNING; 1240 sc->sc_scan_next = 0; 1241 1242 /* make the link LED blink while we're scanning */ 1243 wpi_set_led(sc, WPI_LED_LINK, 20, 2); 1244 1245 /* 1246 * clear association to receive beacons from all 1247 * BSS'es 1248 */ 1249 sc->sc_config.state = 0; 1250 sc->sc_config.filter &= ~LE_32(WPI_FILTER_BSS); 1251 1252 WPI_DBG((WPI_DEBUG_80211, "config chan %d flags %x " 1253 "filter %x\n", 1254 sc->sc_config.chan, sc->sc_config.flags, 1255 sc->sc_config.filter)); 1256 1257 err = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->sc_config, 1258 sizeof (wpi_config_t), 1); 1259 if (err != WPI_SUCCESS) { 1260 cmn_err(CE_WARN, 1261 "could not clear association\n"); 1262 sc->sc_flags &= ~WPI_F_SCANNING; 1263 mutex_exit(&sc->sc_glock); 1264 return (err); 1265 } 1266 1267 /* add broadcast node to send probe request */ 1268 (void) memset(&node, 0, sizeof (node)); 1269 (void) memset(&node.bssid, 0xff, IEEE80211_ADDR_LEN); 1270 node.id = WPI_ID_BROADCAST; 1271 1272 err = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, 1273 sizeof (node), 1); 1274 if (err != WPI_SUCCESS) { 1275 cmn_err(CE_WARN, 1276 "could not add broadcast node\n"); 1277 sc->sc_flags &= ~WPI_F_SCANNING; 1278 mutex_exit(&sc->sc_glock); 1279 return (err); 1280 } 1281 break; 1282 } 1283 case IEEE80211_S_SCAN: 1284 mutex_exit(&sc->sc_glock); 1285 /* step to next channel before actual FW scan */ 1286 err = sc->sc_newstate(ic, nstate, arg); 1287 mutex_enter(&sc->sc_glock); 1288 if ((err != 0) || ((err = wpi_scan(sc)) != 0)) { 1289 cmn_err(CE_WARN, 1290 "could not initiate scan\n"); 1291 sc->sc_flags &= ~WPI_F_SCANNING; 1292 ieee80211_cancel_scan(ic); 1293 } 1294 mutex_exit(&sc->sc_glock); 1295 return (err); 1296 default: 1297 break; 1298 } 1299 sc->sc_clk = 0; 1300 break; 1301 1302 case IEEE80211_S_AUTH: 1303 if (ostate == IEEE80211_S_SCAN) { 1304 sc->sc_flags &= ~WPI_F_SCANNING; 1305 } 1306 1307 /* reset state to handle reassociations correctly */ 1308 sc->sc_config.state = 0; 1309 sc->sc_config.filter &= ~LE_32(WPI_FILTER_BSS); 1310 1311 if ((err = wpi_auth(sc)) != 0) { 1312 WPI_DBG((WPI_DEBUG_80211, 1313 "could not send authentication request\n")); 1314 mutex_exit(&sc->sc_glock); 1315 return (err); 1316 } 1317 break; 1318 1319 case IEEE80211_S_RUN: 1320 if (ostate == IEEE80211_S_SCAN) { 1321 sc->sc_flags &= ~WPI_F_SCANNING; 1322 } 1323 1324 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 1325 /* link LED blinks while monitoring */ 1326 wpi_set_led(sc, WPI_LED_LINK, 5, 5); 1327 break; 1328 } 1329 1330 if (ic->ic_opmode != IEEE80211_M_STA) { 1331 (void) wpi_auth(sc); 1332 /* need setup beacon here */ 1333 } 1334 WPI_DBG((WPI_DEBUG_80211, "wpi: associated.")); 1335 1336 /* update adapter's configuration */ 1337 sc->sc_config.state = LE_16(WPI_CONFIG_ASSOCIATED); 1338 /* short preamble/slot time are negotiated when associating */ 1339 sc->sc_config.flags &= ~LE_32(WPI_CONFIG_SHPREAMBLE | 1340 WPI_CONFIG_SHSLOT); 1341 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1342 sc->sc_config.flags |= LE_32(WPI_CONFIG_SHSLOT); 1343 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1344 sc->sc_config.flags |= LE_32(WPI_CONFIG_SHPREAMBLE); 1345 sc->sc_config.filter |= LE_32(WPI_FILTER_BSS); 1346 if (ic->ic_opmode != IEEE80211_M_STA) 1347 sc->sc_config.filter |= LE_32(WPI_FILTER_BEACON); 1348 1349 WPI_DBG((WPI_DEBUG_80211, "config chan %d flags %x\n", 1350 sc->sc_config.chan, sc->sc_config.flags)); 1351 err = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->sc_config, 1352 sizeof (wpi_config_t), 1); 1353 if (err != WPI_SUCCESS) { 1354 WPI_DBG((WPI_DEBUG_80211, 1355 "could not update configuration\n")); 1356 mutex_exit(&sc->sc_glock); 1357 return (err); 1358 } 1359 1360 /* start automatic rate control */ 1361 mutex_enter(&sc->sc_mt_lock); 1362 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) { 1363 sc->sc_flags |= WPI_F_RATE_AUTO_CTL; 1364 /* set rate to some reasonable initial value */ 1365 i = in->in_rates.ir_nrates - 1; 1366 while (i > 0 && IEEE80211_RATE(i) > 72) 1367 i--; 1368 in->in_txrate = i; 1369 } else { 1370 sc->sc_flags &= ~WPI_F_RATE_AUTO_CTL; 1371 } 1372 mutex_exit(&sc->sc_mt_lock); 1373 1374 /* link LED always on while associated */ 1375 wpi_set_led(sc, WPI_LED_LINK, 0, 1); 1376 break; 1377 1378 case IEEE80211_S_INIT: 1379 sc->sc_flags &= ~WPI_F_SCANNING; 1380 break; 1381 1382 case IEEE80211_S_ASSOC: 1383 sc->sc_flags &= ~WPI_F_SCANNING; 1384 break; 1385 } 1386 1387 mutex_exit(&sc->sc_glock); 1388 return (sc->sc_newstate(ic, nstate, arg)); 1389 } 1390 1391 /*ARGSUSED*/ 1392 static int wpi_key_set(ieee80211com_t *ic, const struct ieee80211_key *k, 1393 const uint8_t mac[IEEE80211_ADDR_LEN]) 1394 { 1395 wpi_sc_t *sc = (wpi_sc_t *)ic; 1396 wpi_node_t node; 1397 int err; 1398 1399 switch (k->wk_cipher->ic_cipher) { 1400 case IEEE80211_CIPHER_WEP: 1401 case IEEE80211_CIPHER_TKIP: 1402 return (1); /* sofeware do it. */ 1403 case IEEE80211_CIPHER_AES_CCM: 1404 break; 1405 default: 1406 return (0); 1407 } 1408 sc->sc_config.filter &= ~(WPI_FILTER_NODECRYPTUNI | 1409 WPI_FILTER_NODECRYPTMUL); 1410 1411 mutex_enter(&sc->sc_glock); 1412 1413 /* update ap/multicast node */ 1414 (void) memset(&node, 0, sizeof (node)); 1415 if (IEEE80211_IS_MULTICAST(mac)) { 1416 (void) memset(node.bssid, 0xff, 6); 1417 node.id = WPI_ID_BROADCAST; 1418 } else { 1419 IEEE80211_ADDR_COPY(node.bssid, ic->ic_bss->in_bssid); 1420 node.id = WPI_ID_BSS; 1421 } 1422 if (k->wk_flags & IEEE80211_KEY_XMIT) { 1423 node.key_flags = 0; 1424 node.keyp = k->wk_keyix; 1425 } else { 1426 node.key_flags = (1 << 14); 1427 node.keyp = k->wk_keyix + 4; 1428 } 1429 (void) memcpy(node.key, k->wk_key, k->wk_keylen); 1430 node.key_flags |= (2 | (1 << 3) | (k->wk_keyix << 8)); 1431 node.sta_mask = 1; 1432 node.control = 1; 1433 err = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof (node), 1); 1434 if (err != WPI_SUCCESS) { 1435 cmn_err(CE_WARN, "wpi_key_set():" 1436 "failed to update ap node\n"); 1437 mutex_exit(&sc->sc_glock); 1438 return (0); 1439 } 1440 mutex_exit(&sc->sc_glock); 1441 return (1); 1442 } 1443 1444 /* 1445 * Grab exclusive access to NIC memory. 1446 */ 1447 static void 1448 wpi_mem_lock(wpi_sc_t *sc) 1449 { 1450 uint32_t tmp; 1451 int ntries; 1452 1453 tmp = WPI_READ(sc, WPI_GPIO_CTL); 1454 WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC); 1455 1456 /* spin until we actually get the lock */ 1457 for (ntries = 0; ntries < 1000; ntries++) { 1458 if ((WPI_READ(sc, WPI_GPIO_CTL) & 1459 (WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK) 1460 break; 1461 DELAY(10); 1462 } 1463 if (ntries == 1000) 1464 WPI_DBG((WPI_DEBUG_PIO, "could not lock memory\n")); 1465 } 1466 1467 /* 1468 * Release lock on NIC memory. 1469 */ 1470 static void 1471 wpi_mem_unlock(wpi_sc_t *sc) 1472 { 1473 uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL); 1474 WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC); 1475 } 1476 1477 static uint32_t 1478 wpi_mem_read(wpi_sc_t *sc, uint16_t addr) 1479 { 1480 WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr); 1481 return (WPI_READ(sc, WPI_READ_MEM_DATA)); 1482 } 1483 1484 static void 1485 wpi_mem_write(wpi_sc_t *sc, uint16_t addr, uint32_t data) 1486 { 1487 WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr); 1488 WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data); 1489 } 1490 1491 static void 1492 wpi_mem_write_region_4(wpi_sc_t *sc, uint16_t addr, 1493 const uint32_t *data, int wlen) 1494 { 1495 for (; wlen > 0; wlen--, data++, addr += 4) 1496 wpi_mem_write(sc, addr, *data); 1497 } 1498 1499 /* 1500 * Read 16 bits from the EEPROM. We access EEPROM through the MAC instead of 1501 * using the traditional bit-bang method. 1502 */ 1503 static uint16_t 1504 wpi_read_prom_word(wpi_sc_t *sc, uint32_t addr) 1505 { 1506 uint32_t val; 1507 int ntries; 1508 1509 WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2); 1510 1511 wpi_mem_lock(sc); 1512 for (ntries = 0; ntries < 10; ntries++) { 1513 if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) & WPI_EEPROM_READY) 1514 break; 1515 DELAY(10); 1516 } 1517 wpi_mem_unlock(sc); 1518 1519 if (ntries == 10) { 1520 WPI_DBG((WPI_DEBUG_PIO, "could not read EEPROM\n")); 1521 return (0xdead); 1522 } 1523 return (val >> 16); 1524 } 1525 1526 /* 1527 * The firmware boot code is small and is intended to be copied directly into 1528 * the NIC internal memory. 1529 */ 1530 static int 1531 wpi_load_microcode(wpi_sc_t *sc) 1532 { 1533 const char *ucode; 1534 int size; 1535 1536 ucode = sc->sc_boot; 1537 size = LE_32(sc->sc_hdr->bootsz); 1538 /* check that microcode size is a multiple of 4 */ 1539 if (size & 3) 1540 return (EINVAL); 1541 1542 size /= sizeof (uint32_t); 1543 1544 wpi_mem_lock(sc); 1545 1546 /* copy microcode image into NIC memory */ 1547 wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE, (const uint32_t *)ucode, 1548 size); 1549 1550 wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0); 1551 wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT); 1552 wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size); 1553 1554 /* run microcode */ 1555 wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN); 1556 1557 wpi_mem_unlock(sc); 1558 1559 return (WPI_SUCCESS); 1560 } 1561 1562 /* 1563 * The firmware text and data segments are transferred to the NIC using DMA. 1564 * The driver just copies the firmware into DMA-safe memory and tells the NIC 1565 * where to find it. Once the NIC has copied the firmware into its internal 1566 * memory, we can free our local copy in the driver. 1567 */ 1568 static int 1569 wpi_load_firmware(wpi_sc_t *sc, uint32_t target) 1570 { 1571 const char *fw; 1572 int size; 1573 wpi_dma_t *dma_p; 1574 ddi_dma_cookie_t *cookie; 1575 wpi_tx_desc_t desc; 1576 int i, ntries, err = WPI_SUCCESS; 1577 1578 /* only text and data here */ 1579 if (target == WPI_FW_TEXT) { 1580 fw = sc->sc_text; 1581 size = LE_32(sc->sc_hdr->textsz); 1582 dma_p = &sc->sc_dma_fw_text; 1583 cookie = sc->sc_fw_text_cookie; 1584 } else { 1585 fw = sc->sc_data; 1586 size = LE_32(sc->sc_hdr->datasz); 1587 dma_p = &sc->sc_dma_fw_data; 1588 cookie = sc->sc_fw_data_cookie; 1589 } 1590 1591 /* copy firmware image to DMA-safe memory */ 1592 (void) memcpy(dma_p->mem_va, fw, size); 1593 1594 /* make sure the adapter will get up-to-date values */ 1595 (void) ddi_dma_sync(dma_p->dma_hdl, 0, size, DDI_DMA_SYNC_FORDEV); 1596 1597 (void) memset(&desc, 0, sizeof (desc)); 1598 desc.flags = LE_32(WPI_PAD32(size) << 28 | dma_p->ncookies << 24); 1599 for (i = 0; i < dma_p->ncookies; i++) { 1600 WPI_DBG((WPI_DEBUG_DMA, "cookie%d addr:%x size:%x\n", 1601 i, cookie[i].dmac_address, cookie[i].dmac_size)); 1602 desc.segs[i].addr = cookie[i].dmac_address; 1603 desc.segs[i].len = (uint32_t)cookie[i].dmac_size; 1604 } 1605 1606 wpi_mem_lock(sc); 1607 1608 /* tell adapter where to copy image in its internal memory */ 1609 WPI_WRITE(sc, WPI_FW_TARGET, target); 1610 1611 WPI_WRITE(sc, WPI_TX_CONFIG(6), 0); 1612 1613 /* copy firmware descriptor into NIC memory */ 1614 WPI_WRITE_REGION_4(sc, WPI_TX_DESC(6), (uint32_t *)&desc, 1615 sizeof desc / sizeof (uint32_t)); 1616 1617 WPI_WRITE(sc, WPI_TX_CREDIT(6), 0xfffff); 1618 WPI_WRITE(sc, WPI_TX_STATE(6), 0x4001); 1619 WPI_WRITE(sc, WPI_TX_CONFIG(6), 0x80000001); 1620 1621 /* wait while the adapter is busy copying the firmware */ 1622 for (ntries = 0; ntries < 100; ntries++) { 1623 if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(6)) 1624 break; 1625 DELAY(1000); 1626 } 1627 if (ntries == 100) { 1628 WPI_DBG((WPI_DEBUG_FW, "timeout transferring firmware\n")); 1629 err = ETIMEDOUT; 1630 } 1631 1632 WPI_WRITE(sc, WPI_TX_CREDIT(6), 0); 1633 1634 wpi_mem_unlock(sc); 1635 1636 return (err); 1637 } 1638 1639 /*ARGSUSED*/ 1640 static void 1641 wpi_rx_intr(wpi_sc_t *sc, wpi_rx_desc_t *desc, wpi_rx_data_t *data) 1642 { 1643 ieee80211com_t *ic = &sc->sc_ic; 1644 wpi_rx_ring_t *ring = &sc->sc_rxq; 1645 wpi_rx_stat_t *stat; 1646 wpi_rx_head_t *head; 1647 wpi_rx_tail_t *tail; 1648 ieee80211_node_t *in; 1649 struct ieee80211_frame *wh; 1650 mblk_t *mp; 1651 uint16_t len; 1652 1653 stat = (wpi_rx_stat_t *)(desc + 1); 1654 1655 if (stat->len > WPI_STAT_MAXLEN) { 1656 WPI_DBG((WPI_DEBUG_RX, "invalid rx statistic header\n")); 1657 return; 1658 } 1659 1660 head = (wpi_rx_head_t *)((caddr_t)(stat + 1) + stat->len); 1661 tail = (wpi_rx_tail_t *)((caddr_t)(head + 1) + LE_16(head->len)); 1662 1663 len = LE_16(head->len); 1664 1665 WPI_DBG((WPI_DEBUG_RX, "rx intr: idx=%d len=%d stat len=%d rssi=%d " 1666 "rate=%x chan=%d tstamp=%llu", ring->cur, LE_32(desc->len), 1667 len, (int8_t)stat->rssi, head->rate, head->chan, 1668 LE_64(tail->tstamp))); 1669 1670 if ((len < 20) || (len > sc->sc_dmabuf_sz)) { 1671 sc->sc_rx_err++; 1672 return; 1673 } 1674 1675 /* 1676 * Discard Rx frames with bad CRC early 1677 */ 1678 if ((LE_32(tail->flags) & WPI_RX_NOERROR) != WPI_RX_NOERROR) { 1679 WPI_DBG((WPI_DEBUG_RX, "rx tail flags error %x\n", 1680 LE_32(tail->flags))); 1681 sc->sc_rx_err++; 1682 return; 1683 } 1684 1685 /* update Rx descriptor */ 1686 /* ring->desc[ring->cur] = LE_32(data->dma_data.cookie.dmac_address); */ 1687 1688 #ifdef WPI_BPF 1689 #ifndef WPI_CURRENT 1690 if (sc->sc_drvbpf != NULL) { 1691 #else 1692 if (bpf_peers_present(sc->sc_drvbpf)) { 1693 #endif 1694 struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap; 1695 1696 tap->wr_flags = 0; 1697 tap->wr_rate = head->rate; 1698 tap->wr_chan_freq = 1699 LE_16(ic->ic_channels[head->chan].ic_freq); 1700 tap->wr_chan_flags = 1701 LE_16(ic->ic_channels[head->chan].ic_flags); 1702 tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET); 1703 tap->wr_dbm_antnoise = (int8_t)LE_16(stat->noise); 1704 tap->wr_tsft = tail->tstamp; 1705 tap->wr_antenna = (LE_16(head->flags) >> 4) & 0xf; 1706 switch (head->rate) { 1707 /* CCK rates */ 1708 case 10: tap->wr_rate = 2; break; 1709 case 20: tap->wr_rate = 4; break; 1710 case 55: tap->wr_rate = 11; break; 1711 case 110: tap->wr_rate = 22; break; 1712 /* OFDM rates */ 1713 case 0xd: tap->wr_rate = 12; break; 1714 case 0xf: tap->wr_rate = 18; break; 1715 case 0x5: tap->wr_rate = 24; break; 1716 case 0x7: tap->wr_rate = 36; break; 1717 case 0x9: tap->wr_rate = 48; break; 1718 case 0xb: tap->wr_rate = 72; break; 1719 case 0x1: tap->wr_rate = 96; break; 1720 case 0x3: tap->wr_rate = 108; break; 1721 /* unknown rate: should not happen */ 1722 default: tap->wr_rate = 0; 1723 } 1724 if (LE_16(head->flags) & 0x4) 1725 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 1726 1727 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 1728 } 1729 #endif 1730 /* grab a reference to the source node */ 1731 wh = (struct ieee80211_frame *)(head + 1); 1732 1733 #ifdef DEBUG 1734 if (wpi_dbg_flags & WPI_DEBUG_RX) 1735 ieee80211_dump_pkt((uint8_t *)wh, len, 0, 0); 1736 #endif 1737 1738 in = ieee80211_find_rxnode(ic, wh); 1739 mp = allocb(len, BPRI_MED); 1740 if (mp) { 1741 (void) memcpy(mp->b_wptr, wh, len); 1742 mp->b_wptr += len; 1743 1744 /* send the frame to the 802.11 layer */ 1745 (void) ieee80211_input(ic, mp, in, stat->rssi, 0); 1746 } else { 1747 sc->sc_rx_nobuf++; 1748 WPI_DBG((WPI_DEBUG_RX, 1749 "wpi_rx_intr(): alloc rx buf failed\n")); 1750 } 1751 /* release node reference */ 1752 ieee80211_free_node(in); 1753 } 1754 1755 /*ARGSUSED*/ 1756 static void 1757 wpi_tx_intr(wpi_sc_t *sc, wpi_rx_desc_t *desc, wpi_rx_data_t *data) 1758 { 1759 ieee80211com_t *ic = &sc->sc_ic; 1760 wpi_tx_ring_t *ring = &sc->sc_txq[desc->qid & 0x3]; 1761 /* wpi_tx_data_t *txdata = &ring->data[desc->idx]; */ 1762 wpi_tx_stat_t *stat = (wpi_tx_stat_t *)(desc + 1); 1763 wpi_amrr_t *amrr = (wpi_amrr_t *)ic->ic_bss; 1764 1765 WPI_DBG((WPI_DEBUG_TX, "tx done: qid=%d idx=%d retries=%d nkill=%d " 1766 "rate=%x duration=%d status=%x\n", 1767 desc->qid, desc->idx, stat->ntries, stat->nkill, stat->rate, 1768 LE_32(stat->duration), LE_32(stat->status))); 1769 1770 amrr->txcnt++; 1771 WPI_DBG((WPI_DEBUG_RATECTL, "tx: %d cnt\n", amrr->txcnt)); 1772 if (stat->ntries > 0) { 1773 amrr->retrycnt++; 1774 sc->sc_tx_retries++; 1775 WPI_DBG((WPI_DEBUG_RATECTL, "tx: %d retries\n", 1776 amrr->retrycnt)); 1777 } 1778 1779 sc->sc_tx_timer = 0; 1780 1781 mutex_enter(&sc->sc_tx_lock); 1782 ring->queued--; 1783 if (ring->queued < 0) 1784 ring->queued = 0; 1785 if ((sc->sc_need_reschedule) && (ring->queued <= (ring->count << 3))) { 1786 sc->sc_need_reschedule = 0; 1787 mutex_exit(&sc->sc_tx_lock); 1788 mac_tx_update(ic->ic_mach); 1789 mutex_enter(&sc->sc_tx_lock); 1790 } 1791 mutex_exit(&sc->sc_tx_lock); 1792 } 1793 1794 static void 1795 wpi_cmd_intr(wpi_sc_t *sc, wpi_rx_desc_t *desc) 1796 { 1797 if ((desc->qid & 7) != 4) { 1798 return; /* not a command ack */ 1799 } 1800 mutex_enter(&sc->sc_glock); 1801 sc->sc_flags |= WPI_F_CMD_DONE; 1802 cv_signal(&sc->sc_cmd_cv); 1803 mutex_exit(&sc->sc_glock); 1804 } 1805 1806 static uint_t 1807 wpi_notif_softintr(caddr_t arg) 1808 { 1809 wpi_sc_t *sc = (wpi_sc_t *)arg; 1810 wpi_rx_desc_t *desc; 1811 wpi_rx_data_t *data; 1812 uint32_t hw; 1813 1814 mutex_enter(&sc->sc_glock); 1815 if (sc->sc_notif_softint_pending != 1) { 1816 mutex_exit(&sc->sc_glock); 1817 return (DDI_INTR_UNCLAIMED); 1818 } 1819 mutex_exit(&sc->sc_glock); 1820 1821 hw = LE_32(sc->sc_shared->next); 1822 1823 while (sc->sc_rxq.cur != hw) { 1824 data = &sc->sc_rxq.data[sc->sc_rxq.cur]; 1825 desc = (wpi_rx_desc_t *)data->dma_data.mem_va; 1826 1827 WPI_DBG((WPI_DEBUG_INTR, "rx notification hw = %d cur = %d " 1828 "qid=%x idx=%d flags=%x type=%d len=%d\n", 1829 hw, sc->sc_rxq.cur, desc->qid, desc->idx, desc->flags, 1830 desc->type, LE_32(desc->len))); 1831 1832 if (!(desc->qid & 0x80)) /* reply to a command */ 1833 wpi_cmd_intr(sc, desc); 1834 1835 switch (desc->type) { 1836 case WPI_RX_DONE: 1837 /* a 802.11 frame was received */ 1838 wpi_rx_intr(sc, desc, data); 1839 break; 1840 1841 case WPI_TX_DONE: 1842 /* a 802.11 frame has been transmitted */ 1843 wpi_tx_intr(sc, desc, data); 1844 break; 1845 1846 case WPI_UC_READY: 1847 { 1848 wpi_ucode_info_t *uc = 1849 (wpi_ucode_info_t *)(desc + 1); 1850 1851 /* the microcontroller is ready */ 1852 WPI_DBG((WPI_DEBUG_FW, 1853 "microcode alive notification version %x " 1854 "alive %x\n", LE_32(uc->version), 1855 LE_32(uc->valid))); 1856 1857 if (LE_32(uc->valid) != 1) { 1858 WPI_DBG((WPI_DEBUG_FW, 1859 "microcontroller initialization failed\n")); 1860 } 1861 break; 1862 } 1863 case WPI_STATE_CHANGED: 1864 { 1865 uint32_t *status = (uint32_t *)(desc + 1); 1866 1867 /* enabled/disabled notification */ 1868 WPI_DBG((WPI_DEBUG_RADIO, "state changed to %x\n", 1869 LE_32(*status))); 1870 1871 if (LE_32(*status) & 1) { 1872 /* 1873 * the radio button has to be pushed(OFF). It 1874 * is considered as a hw error, the 1875 * wpi_thread() tries to recover it after the 1876 * button is pushed again(ON) 1877 */ 1878 cmn_err(CE_NOTE, 1879 "wpi: Radio transmitter is off\n"); 1880 sc->sc_ostate = sc->sc_ic.ic_state; 1881 ieee80211_new_state(&sc->sc_ic, 1882 IEEE80211_S_INIT, -1); 1883 sc->sc_flags |= 1884 (WPI_F_HW_ERR_RECOVER | WPI_F_RADIO_OFF); 1885 } 1886 break; 1887 } 1888 case WPI_START_SCAN: 1889 { 1890 wpi_start_scan_t *scan = 1891 (wpi_start_scan_t *)(desc + 1); 1892 1893 WPI_DBG((WPI_DEBUG_SCAN, 1894 "scanning channel %d status %x\n", 1895 scan->chan, LE_32(scan->status))); 1896 1897 break; 1898 } 1899 case WPI_STOP_SCAN: 1900 { 1901 wpi_stop_scan_t *scan = 1902 (wpi_stop_scan_t *)(desc + 1); 1903 1904 WPI_DBG((WPI_DEBUG_SCAN, 1905 "completed channel %d (burst of %d) status %02x\n", 1906 scan->chan, scan->nchan, scan->status)); 1907 1908 sc->sc_scan_pending = 0; 1909 sc->sc_scan_next++; 1910 break; 1911 } 1912 default: 1913 break; 1914 } 1915 1916 sc->sc_rxq.cur = (sc->sc_rxq.cur + 1) % WPI_RX_RING_COUNT; 1917 } 1918 1919 /* tell the firmware what we have processed */ 1920 hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1; 1921 WPI_WRITE(sc, WPI_RX_WIDX, hw & (~7)); 1922 mutex_enter(&sc->sc_glock); 1923 sc->sc_notif_softint_pending = 0; 1924 mutex_exit(&sc->sc_glock); 1925 1926 return (DDI_INTR_CLAIMED); 1927 } 1928 1929 static uint_t 1930 wpi_intr(caddr_t arg) 1931 { 1932 wpi_sc_t *sc = (wpi_sc_t *)arg; 1933 uint32_t r, rfh; 1934 1935 mutex_enter(&sc->sc_glock); 1936 if (sc->sc_flags & WPI_F_SUSPEND) { 1937 mutex_exit(&sc->sc_glock); 1938 return (DDI_INTR_UNCLAIMED); 1939 } 1940 1941 r = WPI_READ(sc, WPI_INTR); 1942 if (r == 0 || r == 0xffffffff) { 1943 mutex_exit(&sc->sc_glock); 1944 return (DDI_INTR_UNCLAIMED); 1945 } 1946 1947 WPI_DBG((WPI_DEBUG_INTR, "interrupt reg %x\n", r)); 1948 1949 rfh = WPI_READ(sc, WPI_INTR_STATUS); 1950 /* disable interrupts */ 1951 WPI_WRITE(sc, WPI_MASK, 0); 1952 /* ack interrupts */ 1953 WPI_WRITE(sc, WPI_INTR, r); 1954 WPI_WRITE(sc, WPI_INTR_STATUS, rfh); 1955 1956 if (sc->sc_notif_softint_id == NULL) { 1957 mutex_exit(&sc->sc_glock); 1958 return (DDI_INTR_CLAIMED); 1959 } 1960 1961 if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) { 1962 WPI_DBG((WPI_DEBUG_FW, "fatal firmware error\n")); 1963 mutex_exit(&sc->sc_glock); 1964 wpi_stop(sc); 1965 if (!(sc->sc_flags & WPI_F_HW_ERR_RECOVER)) { 1966 sc->sc_ostate = sc->sc_ic.ic_state; 1967 } 1968 ieee80211_new_state(&sc->sc_ic, IEEE80211_S_INIT, -1); 1969 sc->sc_flags |= WPI_F_HW_ERR_RECOVER; 1970 return (DDI_INTR_CLAIMED); 1971 } 1972 1973 if ((r & (WPI_RX_INTR | WPI_RX_SWINT)) || 1974 (rfh & 0x40070000)) { 1975 sc->sc_notif_softint_pending = 1; 1976 ddi_trigger_softintr(sc->sc_notif_softint_id); 1977 } 1978 1979 if (r & WPI_ALIVE_INTR) { /* firmware initialized */ 1980 sc->sc_flags |= WPI_F_FW_INIT; 1981 cv_signal(&sc->sc_fw_cv); 1982 } 1983 1984 /* re-enable interrupts */ 1985 WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK); 1986 mutex_exit(&sc->sc_glock); 1987 1988 return (DDI_INTR_CLAIMED); 1989 } 1990 1991 static uint8_t 1992 wpi_plcp_signal(int rate) 1993 { 1994 switch (rate) { 1995 /* CCK rates (returned values are device-dependent) */ 1996 case 2: return (10); 1997 case 4: return (20); 1998 case 11: return (55); 1999 case 22: return (110); 2000 2001 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 2002 /* R1-R4 (ral/ural is R4-R1) */ 2003 case 12: return (0xd); 2004 case 18: return (0xf); 2005 case 24: return (0x5); 2006 case 36: return (0x7); 2007 case 48: return (0x9); 2008 case 72: return (0xb); 2009 case 96: return (0x1); 2010 case 108: return (0x3); 2011 2012 /* unsupported rates (should not get there) */ 2013 default: return (0); 2014 } 2015 } 2016 2017 static mblk_t * 2018 wpi_m_tx(void *arg, mblk_t *mp) 2019 { 2020 wpi_sc_t *sc = (wpi_sc_t *)arg; 2021 ieee80211com_t *ic = &sc->sc_ic; 2022 mblk_t *next; 2023 2024 if (sc->sc_flags & WPI_F_SUSPEND) { 2025 freemsgchain(mp); 2026 return (NULL); 2027 } 2028 2029 if (ic->ic_state != IEEE80211_S_RUN) { 2030 freemsgchain(mp); 2031 return (NULL); 2032 } 2033 2034 while (mp != NULL) { 2035 next = mp->b_next; 2036 mp->b_next = NULL; 2037 if (wpi_send(ic, mp, IEEE80211_FC0_TYPE_DATA) != 0) { 2038 mp->b_next = next; 2039 break; 2040 } 2041 mp = next; 2042 } 2043 return (mp); 2044 } 2045 2046 /* ARGSUSED */ 2047 static int 2048 wpi_send(ieee80211com_t *ic, mblk_t *mp, uint8_t type) 2049 { 2050 wpi_sc_t *sc = (wpi_sc_t *)ic; 2051 wpi_tx_ring_t *ring; 2052 wpi_tx_desc_t *desc; 2053 wpi_tx_data_t *data; 2054 wpi_tx_cmd_t *cmd; 2055 wpi_cmd_data_t *tx; 2056 ieee80211_node_t *in; 2057 struct ieee80211_frame *wh; 2058 struct ieee80211_key *k; 2059 mblk_t *m, *m0; 2060 int rate, hdrlen, len, mblen, off, err = WPI_SUCCESS; 2061 2062 ring = ((type & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_DATA) ? 2063 (&sc->sc_txq[0]) : (&sc->sc_txq[1]); 2064 data = &ring->data[ring->cur]; 2065 desc = data->desc; 2066 cmd = data->cmd; 2067 bzero(desc, sizeof (*desc)); 2068 bzero(cmd, sizeof (*cmd)); 2069 2070 mutex_enter(&sc->sc_tx_lock); 2071 if (sc->sc_flags & WPI_F_SUSPEND) { 2072 mutex_exit(&sc->sc_tx_lock); 2073 if ((type & IEEE80211_FC0_TYPE_MASK) != 2074 IEEE80211_FC0_TYPE_DATA) { 2075 freemsg(mp); 2076 } 2077 err = ENXIO; 2078 goto exit; 2079 } 2080 2081 if (ring->queued > ring->count - 64) { 2082 WPI_DBG((WPI_DEBUG_TX, "wpi_send(): no txbuf\n")); 2083 sc->sc_need_reschedule = 1; 2084 mutex_exit(&sc->sc_tx_lock); 2085 if ((type & IEEE80211_FC0_TYPE_MASK) != 2086 IEEE80211_FC0_TYPE_DATA) { 2087 freemsg(mp); 2088 } 2089 sc->sc_tx_nobuf++; 2090 err = ENOMEM; 2091 goto exit; 2092 } 2093 mutex_exit(&sc->sc_tx_lock); 2094 2095 hdrlen = sizeof (struct ieee80211_frame); 2096 2097 m = allocb(msgdsize(mp) + 32, BPRI_MED); 2098 if (m == NULL) { /* can not alloc buf, drop this package */ 2099 cmn_err(CE_WARN, 2100 "wpi_send(): failed to allocate msgbuf\n"); 2101 freemsg(mp); 2102 err = WPI_SUCCESS; 2103 goto exit; 2104 } 2105 for (off = 0, m0 = mp; m0 != NULL; m0 = m0->b_cont) { 2106 mblen = MBLKL(m0); 2107 (void) memcpy(m->b_rptr + off, m0->b_rptr, mblen); 2108 off += mblen; 2109 } 2110 m->b_wptr += off; 2111 freemsg(mp); 2112 2113 wh = (struct ieee80211_frame *)m->b_rptr; 2114 2115 in = ieee80211_find_txnode(ic, wh->i_addr1); 2116 if (in == NULL) { 2117 cmn_err(CE_WARN, "wpi_send(): failed to find tx node\n"); 2118 freemsg(m); 2119 sc->sc_tx_err++; 2120 err = WPI_SUCCESS; 2121 goto exit; 2122 } 2123 2124 (void) ieee80211_encap(ic, m, in); 2125 2126 cmd->code = WPI_CMD_TX_DATA; 2127 cmd->flags = 0; 2128 cmd->qid = ring->qid; 2129 cmd->idx = ring->cur; 2130 2131 tx = (wpi_cmd_data_t *)cmd->data; 2132 tx->flags = 0; 2133 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 2134 tx->flags |= LE_32(WPI_TX_NEED_ACK); 2135 } else { 2136 tx->flags &= ~(LE_32(WPI_TX_NEED_ACK)); 2137 } 2138 2139 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 2140 k = ieee80211_crypto_encap(ic, m); 2141 if (k == NULL) { 2142 freemsg(m); 2143 sc->sc_tx_err++; 2144 err = WPI_SUCCESS; 2145 goto exit; 2146 } 2147 2148 if (k->wk_cipher->ic_cipher == IEEE80211_CIPHER_AES_CCM) { 2149 tx->security = 2; /* for CCMP */ 2150 tx->flags |= LE_32(WPI_TX_NEED_ACK); 2151 (void) memcpy(&tx->key, k->wk_key, k->wk_keylen); 2152 } 2153 2154 /* packet header may have moved, reset our local pointer */ 2155 wh = (struct ieee80211_frame *)m->b_rptr; 2156 } 2157 2158 len = msgdsize(m); 2159 2160 #ifdef DEBUG 2161 if (wpi_dbg_flags & WPI_DEBUG_TX) 2162 ieee80211_dump_pkt((uint8_t *)wh, hdrlen, 0, 0); 2163 #endif 2164 2165 /* pickup a rate */ 2166 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 2167 IEEE80211_FC0_TYPE_MGT) { 2168 /* mgmt frames are sent at the lowest available bit-rate */ 2169 rate = 2; 2170 } else { 2171 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) { 2172 rate = ic->ic_fixed_rate; 2173 } else 2174 rate = in->in_rates.ir_rates[in->in_txrate]; 2175 } 2176 rate &= IEEE80211_RATE_VAL; 2177 WPI_DBG((WPI_DEBUG_RATECTL, "tx rate[%d of %d] = %x", 2178 in->in_txrate, in->in_rates.ir_nrates, rate)); 2179 #ifdef WPI_BPF 2180 #ifndef WPI_CURRENT 2181 if (sc->sc_drvbpf != NULL) { 2182 #else 2183 if (bpf_peers_present(sc->sc_drvbpf)) { 2184 #endif 2185 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap; 2186 2187 tap->wt_flags = 0; 2188 tap->wt_chan_freq = LE_16(ic->ic_curchan->ic_freq); 2189 tap->wt_chan_flags = LE_16(ic->ic_curchan->ic_flags); 2190 tap->wt_rate = rate; 2191 if (wh->i_fc[1] & IEEE80211_FC1_WEP) 2192 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP; 2193 2194 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 2195 } 2196 #endif 2197 2198 tx->flags |= (LE_32(WPI_TX_AUTO_SEQ)); 2199 tx->flags |= LE_32(WPI_TX_BT_DISABLE | WPI_TX_CALIBRATION); 2200 2201 /* retrieve destination node's id */ 2202 tx->id = IEEE80211_IS_MULTICAST(wh->i_addr1) ? WPI_ID_BROADCAST : 2203 WPI_ID_BSS; 2204 2205 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 2206 IEEE80211_FC0_TYPE_MGT) { 2207 /* tell h/w to set timestamp in probe responses */ 2208 if ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 2209 IEEE80211_FC0_SUBTYPE_PROBE_RESP) 2210 tx->flags |= LE_32(WPI_TX_INSERT_TSTAMP); 2211 2212 if (((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 2213 IEEE80211_FC0_SUBTYPE_ASSOC_REQ) || 2214 ((wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 2215 IEEE80211_FC0_SUBTYPE_REASSOC_REQ)) 2216 tx->timeout = 3; 2217 else 2218 tx->timeout = 2; 2219 } else 2220 tx->timeout = 0; 2221 2222 tx->rate = wpi_plcp_signal(rate); 2223 2224 /* be very persistant at sending frames out */ 2225 tx->rts_ntries = 7; 2226 tx->data_ntries = 15; 2227 2228 tx->cck_mask = 0x0f; 2229 tx->ofdm_mask = 0xff; 2230 tx->lifetime = LE_32(0xffffffff); 2231 2232 tx->len = LE_16(len); 2233 2234 /* save and trim IEEE802.11 header */ 2235 (void) memcpy(tx + 1, m->b_rptr, hdrlen); 2236 m->b_rptr += hdrlen; 2237 (void) memcpy(data->dma_data.mem_va, m->b_rptr, len - hdrlen); 2238 2239 WPI_DBG((WPI_DEBUG_TX, "sending data: qid=%d idx=%d len=%d", ring->qid, 2240 ring->cur, len)); 2241 2242 /* first scatter/gather segment is used by the tx data command */ 2243 desc->flags = LE_32(WPI_PAD32(len) << 28 | (2) << 24); 2244 desc->segs[0].addr = LE_32(data->paddr_cmd); 2245 desc->segs[0].len = LE_32( 2246 roundup(4 + sizeof (wpi_cmd_data_t) + hdrlen, 4)); 2247 desc->segs[1].addr = LE_32(data->dma_data.cookie.dmac_address); 2248 desc->segs[1].len = LE_32(len - hdrlen); 2249 2250 WPI_DMA_SYNC(data->dma_data, DDI_DMA_SYNC_FORDEV); 2251 WPI_DMA_SYNC(ring->dma_desc, DDI_DMA_SYNC_FORDEV); 2252 2253 mutex_enter(&sc->sc_tx_lock); 2254 ring->queued++; 2255 mutex_exit(&sc->sc_tx_lock); 2256 2257 /* kick ring */ 2258 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT; 2259 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2260 freemsg(m); 2261 /* release node reference */ 2262 ieee80211_free_node(in); 2263 2264 ic->ic_stats.is_tx_bytes += len; 2265 ic->ic_stats.is_tx_frags++; 2266 2267 if (sc->sc_tx_timer == 0) 2268 sc->sc_tx_timer = 5; 2269 exit: 2270 return (err); 2271 } 2272 2273 static void 2274 wpi_m_ioctl(void* arg, queue_t *wq, mblk_t *mp) 2275 { 2276 wpi_sc_t *sc = (wpi_sc_t *)arg; 2277 ieee80211com_t *ic = &sc->sc_ic; 2278 int err; 2279 2280 err = ieee80211_ioctl(ic, wq, mp); 2281 if (err == ENETRESET) { 2282 /* 2283 * This is special for the hidden AP connection. 2284 * In any case, we should make sure only one 'scan' 2285 * in the driver for a 'connect' CLI command. So 2286 * when connecting to a hidden AP, the scan is just 2287 * sent out to the air when we know the desired 2288 * essid of the AP we want to connect. 2289 */ 2290 if (ic->ic_des_esslen) { 2291 if (sc->sc_flags & WPI_F_RUNNING) { 2292 wpi_m_stop(sc); 2293 (void) wpi_m_start(sc); 2294 (void) ieee80211_new_state(ic, 2295 IEEE80211_S_SCAN, -1); 2296 } 2297 } 2298 } 2299 } 2300 2301 /* 2302 * Callback functions for get/set properties 2303 */ 2304 /* ARGSUSED */ 2305 static int 2306 wpi_m_getprop(void *arg, const char *pr_name, mac_prop_id_t wldp_pr_name, 2307 uint_t pr_flags, uint_t wldp_length, void *wldp_buf, uint_t *perm) 2308 { 2309 int err = 0; 2310 wpi_sc_t *sc = (wpi_sc_t *)arg; 2311 2312 err = ieee80211_getprop(&sc->sc_ic, pr_name, wldp_pr_name, 2313 pr_flags, wldp_length, wldp_buf, perm); 2314 2315 return (err); 2316 } 2317 static int 2318 wpi_m_setprop(void *arg, const char *pr_name, mac_prop_id_t wldp_pr_name, 2319 uint_t wldp_length, const void *wldp_buf) 2320 { 2321 int err; 2322 wpi_sc_t *sc = (wpi_sc_t *)arg; 2323 ieee80211com_t *ic = &sc->sc_ic; 2324 2325 err = ieee80211_setprop(ic, pr_name, wldp_pr_name, 2326 wldp_length, wldp_buf); 2327 2328 if (err == ENETRESET) { 2329 if (ic->ic_des_esslen) { 2330 if (sc->sc_flags & WPI_F_RUNNING) { 2331 wpi_m_stop(sc); 2332 (void) wpi_m_start(sc); 2333 (void) ieee80211_new_state(ic, 2334 IEEE80211_S_SCAN, -1); 2335 } 2336 } 2337 2338 err = 0; 2339 } 2340 2341 return (err); 2342 } 2343 2344 /*ARGSUSED*/ 2345 static int 2346 wpi_m_stat(void *arg, uint_t stat, uint64_t *val) 2347 { 2348 wpi_sc_t *sc = (wpi_sc_t *)arg; 2349 ieee80211com_t *ic = &sc->sc_ic; 2350 ieee80211_node_t *in; 2351 2352 mutex_enter(&sc->sc_glock); 2353 switch (stat) { 2354 case MAC_STAT_IFSPEED: 2355 in = ic->ic_bss; 2356 *val = ((ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) ? 2357 IEEE80211_RATE(in->in_txrate) : 2358 ic->ic_fixed_rate) / 2 * 1000000; 2359 break; 2360 case MAC_STAT_NOXMTBUF: 2361 *val = sc->sc_tx_nobuf; 2362 break; 2363 case MAC_STAT_NORCVBUF: 2364 *val = sc->sc_rx_nobuf; 2365 break; 2366 case MAC_STAT_IERRORS: 2367 *val = sc->sc_rx_err; 2368 break; 2369 case MAC_STAT_RBYTES: 2370 *val = ic->ic_stats.is_rx_bytes; 2371 break; 2372 case MAC_STAT_IPACKETS: 2373 *val = ic->ic_stats.is_rx_frags; 2374 break; 2375 case MAC_STAT_OBYTES: 2376 *val = ic->ic_stats.is_tx_bytes; 2377 break; 2378 case MAC_STAT_OPACKETS: 2379 *val = ic->ic_stats.is_tx_frags; 2380 break; 2381 case MAC_STAT_OERRORS: 2382 case WIFI_STAT_TX_FAILED: 2383 *val = sc->sc_tx_err; 2384 break; 2385 case WIFI_STAT_TX_RETRANS: 2386 *val = sc->sc_tx_retries; 2387 break; 2388 case WIFI_STAT_FCS_ERRORS: 2389 case WIFI_STAT_WEP_ERRORS: 2390 case WIFI_STAT_TX_FRAGS: 2391 case WIFI_STAT_MCAST_TX: 2392 case WIFI_STAT_RTS_SUCCESS: 2393 case WIFI_STAT_RTS_FAILURE: 2394 case WIFI_STAT_ACK_FAILURE: 2395 case WIFI_STAT_RX_FRAGS: 2396 case WIFI_STAT_MCAST_RX: 2397 case WIFI_STAT_RX_DUPS: 2398 mutex_exit(&sc->sc_glock); 2399 return (ieee80211_stat(ic, stat, val)); 2400 default: 2401 mutex_exit(&sc->sc_glock); 2402 return (ENOTSUP); 2403 } 2404 mutex_exit(&sc->sc_glock); 2405 2406 return (WPI_SUCCESS); 2407 2408 } 2409 2410 static int 2411 wpi_m_start(void *arg) 2412 { 2413 wpi_sc_t *sc = (wpi_sc_t *)arg; 2414 ieee80211com_t *ic = &sc->sc_ic; 2415 int err; 2416 2417 err = wpi_init(sc); 2418 if (err != WPI_SUCCESS) { 2419 wpi_stop(sc); 2420 DELAY(1000000); 2421 err = wpi_init(sc); 2422 } 2423 2424 if (err) { 2425 /* 2426 * The hw init err(eg. RF is OFF). Return Success to make 2427 * the 'plumb' succeed. The wpi_thread() tries to re-init 2428 * background. 2429 */ 2430 mutex_enter(&sc->sc_glock); 2431 sc->sc_flags |= WPI_F_HW_ERR_RECOVER; 2432 mutex_exit(&sc->sc_glock); 2433 return (WPI_SUCCESS); 2434 } 2435 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2436 mutex_enter(&sc->sc_glock); 2437 sc->sc_flags |= WPI_F_RUNNING; 2438 mutex_exit(&sc->sc_glock); 2439 2440 return (WPI_SUCCESS); 2441 } 2442 2443 static void 2444 wpi_m_stop(void *arg) 2445 { 2446 wpi_sc_t *sc = (wpi_sc_t *)arg; 2447 ieee80211com_t *ic = &sc->sc_ic; 2448 2449 wpi_stop(sc); 2450 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2451 mutex_enter(&sc->sc_mt_lock); 2452 sc->sc_flags &= ~WPI_F_HW_ERR_RECOVER; 2453 sc->sc_flags &= ~WPI_F_RATE_AUTO_CTL; 2454 mutex_exit(&sc->sc_mt_lock); 2455 mutex_enter(&sc->sc_glock); 2456 sc->sc_flags &= ~WPI_F_RUNNING; 2457 mutex_exit(&sc->sc_glock); 2458 } 2459 2460 /*ARGSUSED*/ 2461 static int 2462 wpi_m_unicst(void *arg, const uint8_t *macaddr) 2463 { 2464 wpi_sc_t *sc = (wpi_sc_t *)arg; 2465 ieee80211com_t *ic = &sc->sc_ic; 2466 int err; 2467 2468 if (!IEEE80211_ADDR_EQ(ic->ic_macaddr, macaddr)) { 2469 IEEE80211_ADDR_COPY(ic->ic_macaddr, macaddr); 2470 mutex_enter(&sc->sc_glock); 2471 err = wpi_config(sc); 2472 mutex_exit(&sc->sc_glock); 2473 if (err != WPI_SUCCESS) { 2474 cmn_err(CE_WARN, 2475 "wpi_m_unicst(): " 2476 "failed to configure device\n"); 2477 goto fail; 2478 } 2479 } 2480 return (WPI_SUCCESS); 2481 fail: 2482 return (err); 2483 } 2484 2485 /*ARGSUSED*/ 2486 static int 2487 wpi_m_multicst(void *arg, boolean_t add, const uint8_t *m) 2488 { 2489 return (WPI_SUCCESS); 2490 } 2491 2492 /*ARGSUSED*/ 2493 static int 2494 wpi_m_promisc(void *arg, boolean_t on) 2495 { 2496 return (WPI_SUCCESS); 2497 } 2498 2499 static void 2500 wpi_thread(wpi_sc_t *sc) 2501 { 2502 ieee80211com_t *ic = &sc->sc_ic; 2503 clock_t clk; 2504 int times = 0, err, n = 0, timeout = 0; 2505 uint32_t tmp; 2506 2507 mutex_enter(&sc->sc_mt_lock); 2508 while (sc->sc_mf_thread_switch) { 2509 tmp = WPI_READ(sc, WPI_GPIO_CTL); 2510 if (tmp & WPI_GPIO_HW_RF_KILL) { 2511 sc->sc_flags &= ~WPI_F_RADIO_OFF; 2512 } else { 2513 sc->sc_flags |= WPI_F_RADIO_OFF; 2514 } 2515 /* 2516 * If in SUSPEND or the RF is OFF, do nothing 2517 */ 2518 if ((sc->sc_flags & WPI_F_SUSPEND) || 2519 (sc->sc_flags & WPI_F_RADIO_OFF)) { 2520 mutex_exit(&sc->sc_mt_lock); 2521 delay(drv_usectohz(100000)); 2522 mutex_enter(&sc->sc_mt_lock); 2523 continue; 2524 } 2525 2526 /* 2527 * recovery fatal error 2528 */ 2529 if (ic->ic_mach && 2530 (sc->sc_flags & WPI_F_HW_ERR_RECOVER)) { 2531 2532 WPI_DBG((WPI_DEBUG_FW, 2533 "wpi_thread(): " 2534 "try to recover fatal hw error: %d\n", times++)); 2535 2536 wpi_stop(sc); 2537 mutex_exit(&sc->sc_mt_lock); 2538 2539 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2540 delay(drv_usectohz(2000000)); 2541 2542 mutex_enter(&sc->sc_mt_lock); 2543 err = wpi_init(sc); 2544 if (err != WPI_SUCCESS) { 2545 n++; 2546 if (n < 3) 2547 continue; 2548 } 2549 n = 0; 2550 if (!err) 2551 sc->sc_flags |= WPI_F_RUNNING; 2552 sc->sc_flags &= ~WPI_F_HW_ERR_RECOVER; 2553 mutex_exit(&sc->sc_mt_lock); 2554 delay(drv_usectohz(2000000)); 2555 if (sc->sc_ostate != IEEE80211_S_INIT) 2556 ieee80211_new_state(ic, IEEE80211_S_SCAN, 0); 2557 mutex_enter(&sc->sc_mt_lock); 2558 } 2559 2560 /* 2561 * scan next channel 2562 */ 2563 if (ic->ic_mach && 2564 (sc->sc_flags & WPI_F_SCANNING) && sc->sc_scan_next) { 2565 2566 WPI_DBG((WPI_DEBUG_SCAN, 2567 "wpi_thread(): " 2568 "wait for probe response\n")); 2569 2570 sc->sc_scan_next--; 2571 mutex_exit(&sc->sc_mt_lock); 2572 delay(drv_usectohz(200000)); 2573 ieee80211_next_scan(ic); 2574 mutex_enter(&sc->sc_mt_lock); 2575 } 2576 2577 /* 2578 * rate ctl 2579 */ 2580 if (ic->ic_mach && 2581 (sc->sc_flags & WPI_F_RATE_AUTO_CTL)) { 2582 clk = ddi_get_lbolt(); 2583 if (clk > sc->sc_clk + drv_usectohz(500000)) { 2584 wpi_amrr_timeout(sc); 2585 } 2586 } 2587 mutex_exit(&sc->sc_mt_lock); 2588 delay(drv_usectohz(100000)); 2589 mutex_enter(&sc->sc_mt_lock); 2590 if (sc->sc_tx_timer) { 2591 timeout++; 2592 if (timeout == 10) { 2593 sc->sc_tx_timer--; 2594 if (sc->sc_tx_timer == 0) { 2595 sc->sc_flags |= WPI_F_HW_ERR_RECOVER; 2596 sc->sc_ostate = IEEE80211_S_RUN; 2597 WPI_DBG((WPI_DEBUG_FW, 2598 "wpi_thread(): send fail\n")); 2599 } 2600 timeout = 0; 2601 } 2602 } 2603 } 2604 sc->sc_mf_thread = NULL; 2605 cv_signal(&sc->sc_mt_cv); 2606 mutex_exit(&sc->sc_mt_lock); 2607 } 2608 2609 /* 2610 * Extract various information from EEPROM. 2611 */ 2612 static void 2613 wpi_read_eeprom(wpi_sc_t *sc) 2614 { 2615 ieee80211com_t *ic = &sc->sc_ic; 2616 uint16_t val; 2617 int i; 2618 2619 /* read MAC address */ 2620 val = wpi_read_prom_word(sc, WPI_EEPROM_MAC + 0); 2621 ic->ic_macaddr[0] = val & 0xff; 2622 ic->ic_macaddr[1] = val >> 8; 2623 val = wpi_read_prom_word(sc, WPI_EEPROM_MAC + 1); 2624 ic->ic_macaddr[2] = val & 0xff; 2625 ic->ic_macaddr[3] = val >> 8; 2626 val = wpi_read_prom_word(sc, WPI_EEPROM_MAC + 2); 2627 ic->ic_macaddr[4] = val & 0xff; 2628 ic->ic_macaddr[5] = val >> 8; 2629 2630 WPI_DBG((WPI_DEBUG_EEPROM, 2631 "mac:%2x:%2x:%2x:%2x:%2x:%2x\n", 2632 ic->ic_macaddr[0], ic->ic_macaddr[1], 2633 ic->ic_macaddr[2], ic->ic_macaddr[3], 2634 ic->ic_macaddr[4], ic->ic_macaddr[5])); 2635 /* read power settings for 2.4GHz channels */ 2636 for (i = 0; i < 14; i++) { 2637 sc->sc_pwr1[i] = wpi_read_prom_word(sc, WPI_EEPROM_PWR1 + i); 2638 sc->sc_pwr2[i] = wpi_read_prom_word(sc, WPI_EEPROM_PWR2 + i); 2639 WPI_DBG((WPI_DEBUG_EEPROM, 2640 "channel %d pwr1 0x%04x pwr2 0x%04x\n", i + 1, 2641 sc->sc_pwr1[i], sc->sc_pwr2[i])); 2642 } 2643 } 2644 2645 /* 2646 * Send a command to the firmware. 2647 */ 2648 static int 2649 wpi_cmd(wpi_sc_t *sc, int code, const void *buf, int size, int async) 2650 { 2651 wpi_tx_ring_t *ring = &sc->sc_cmdq; 2652 wpi_tx_desc_t *desc; 2653 wpi_tx_cmd_t *cmd; 2654 2655 ASSERT(size <= sizeof (cmd->data)); 2656 ASSERT(mutex_owned(&sc->sc_glock)); 2657 2658 WPI_DBG((WPI_DEBUG_CMD, "wpi_cmd() # code[%d]", code)); 2659 desc = ring->data[ring->cur].desc; 2660 cmd = ring->data[ring->cur].cmd; 2661 2662 cmd->code = (uint8_t)code; 2663 cmd->flags = 0; 2664 cmd->qid = ring->qid; 2665 cmd->idx = ring->cur; 2666 (void) memcpy(cmd->data, buf, size); 2667 2668 desc->flags = LE_32(WPI_PAD32(size) << 28 | 1 << 24); 2669 desc->segs[0].addr = ring->data[ring->cur].paddr_cmd; 2670 desc->segs[0].len = 4 + size; 2671 2672 /* kick cmd ring */ 2673 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT; 2674 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2675 2676 if (async) 2677 return (WPI_SUCCESS); 2678 else { 2679 clock_t clk; 2680 sc->sc_flags &= ~WPI_F_CMD_DONE; 2681 clk = ddi_get_lbolt() + drv_usectohz(2000000); 2682 while (!(sc->sc_flags & WPI_F_CMD_DONE)) { 2683 if (cv_timedwait(&sc->sc_cmd_cv, &sc->sc_glock, clk) 2684 < 0) 2685 break; 2686 } 2687 if (sc->sc_flags & WPI_F_CMD_DONE) 2688 return (WPI_SUCCESS); 2689 else 2690 return (WPI_FAIL); 2691 } 2692 } 2693 2694 /* 2695 * Configure h/w multi-rate retries. 2696 */ 2697 static int 2698 wpi_mrr_setup(wpi_sc_t *sc) 2699 { 2700 wpi_mrr_setup_t mrr; 2701 int i, err; 2702 2703 /* CCK rates (not used with 802.11a) */ 2704 for (i = WPI_CCK1; i <= WPI_CCK11; i++) { 2705 mrr.rates[i].flags = 0; 2706 mrr.rates[i].signal = wpi_ridx_to_signal[i]; 2707 /* fallback to the immediate lower CCK rate (if any) */ 2708 mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1; 2709 /* try one time at this rate before falling back to "next" */ 2710 mrr.rates[i].ntries = 1; 2711 } 2712 2713 /* OFDM rates (not used with 802.11b) */ 2714 for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) { 2715 mrr.rates[i].flags = 0; 2716 mrr.rates[i].signal = wpi_ridx_to_signal[i]; 2717 /* fallback to the immediate lower OFDM rate (if any) */ 2718 mrr.rates[i].next = (i == WPI_OFDM6) ? WPI_OFDM6 : i - 1; 2719 /* try one time at this rate before falling back to "next" */ 2720 mrr.rates[i].ntries = 1; 2721 } 2722 2723 /* setup MRR for control frames */ 2724 mrr.which = LE_32(WPI_MRR_CTL); 2725 err = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof (mrr), 1); 2726 if (err != WPI_SUCCESS) { 2727 WPI_DBG((WPI_DEBUG_MRR, 2728 "could not setup MRR for control frames\n")); 2729 return (err); 2730 } 2731 2732 /* setup MRR for data frames */ 2733 mrr.which = LE_32(WPI_MRR_DATA); 2734 err = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof (mrr), 1); 2735 if (err != WPI_SUCCESS) { 2736 WPI_DBG((WPI_DEBUG_MRR, 2737 "could not setup MRR for data frames\n")); 2738 return (err); 2739 } 2740 2741 return (WPI_SUCCESS); 2742 } 2743 2744 static void 2745 wpi_set_led(wpi_sc_t *sc, uint8_t which, uint8_t off, uint8_t on) 2746 { 2747 wpi_cmd_led_t led; 2748 2749 led.which = which; 2750 led.unit = LE_32(100000); /* on/off in unit of 100ms */ 2751 led.off = off; 2752 led.on = on; 2753 2754 (void) wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof (led), 1); 2755 } 2756 2757 static int 2758 wpi_auth(wpi_sc_t *sc) 2759 { 2760 ieee80211com_t *ic = &sc->sc_ic; 2761 ieee80211_node_t *in = ic->ic_bss; 2762 wpi_node_t node; 2763 int err; 2764 2765 /* update adapter's configuration */ 2766 IEEE80211_ADDR_COPY(sc->sc_config.bssid, in->in_bssid); 2767 sc->sc_config.chan = ieee80211_chan2ieee(ic, in->in_chan); 2768 if (ic->ic_curmode == IEEE80211_MODE_11B) { 2769 sc->sc_config.cck_mask = 0x03; 2770 sc->sc_config.ofdm_mask = 0; 2771 } else if ((in->in_chan != IEEE80211_CHAN_ANYC) && 2772 (IEEE80211_IS_CHAN_5GHZ(in->in_chan))) { 2773 sc->sc_config.cck_mask = 0; 2774 sc->sc_config.ofdm_mask = 0x15; 2775 } else { /* assume 802.11b/g */ 2776 sc->sc_config.cck_mask = 0x0f; 2777 sc->sc_config.ofdm_mask = 0xff; 2778 } 2779 2780 WPI_DBG((WPI_DEBUG_80211, "config chan %d flags %x cck %x ofdm %x" 2781 " bssid:%02x:%02x:%02x:%02x:%02x:%2x\n", 2782 sc->sc_config.chan, sc->sc_config.flags, 2783 sc->sc_config.cck_mask, sc->sc_config.ofdm_mask, 2784 sc->sc_config.bssid[0], sc->sc_config.bssid[1], 2785 sc->sc_config.bssid[2], sc->sc_config.bssid[3], 2786 sc->sc_config.bssid[4], sc->sc_config.bssid[5])); 2787 err = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->sc_config, 2788 sizeof (wpi_config_t), 1); 2789 if (err != WPI_SUCCESS) { 2790 cmn_err(CE_WARN, "wpi_auth(): failed to configurate chan%d\n", 2791 sc->sc_config.chan); 2792 return (err); 2793 } 2794 2795 /* add default node */ 2796 (void) memset(&node, 0, sizeof (node)); 2797 IEEE80211_ADDR_COPY(node.bssid, in->in_bssid); 2798 node.id = WPI_ID_BSS; 2799 node.rate = wpi_plcp_signal(2); 2800 err = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof (node), 1); 2801 if (err != WPI_SUCCESS) { 2802 cmn_err(CE_WARN, "wpi_auth(): failed to add BSS node\n"); 2803 return (err); 2804 } 2805 2806 err = wpi_mrr_setup(sc); 2807 if (err != WPI_SUCCESS) { 2808 cmn_err(CE_WARN, "wpi_auth(): failed to setup MRR\n"); 2809 return (err); 2810 } 2811 2812 return (WPI_SUCCESS); 2813 } 2814 2815 /* 2816 * Send a scan request to the firmware. 2817 */ 2818 static int 2819 wpi_scan(wpi_sc_t *sc) 2820 { 2821 ieee80211com_t *ic = &sc->sc_ic; 2822 wpi_tx_ring_t *ring = &sc->sc_cmdq; 2823 wpi_tx_desc_t *desc; 2824 wpi_tx_data_t *data; 2825 wpi_tx_cmd_t *cmd; 2826 wpi_scan_hdr_t *hdr; 2827 wpi_scan_chan_t *chan; 2828 struct ieee80211_frame *wh; 2829 ieee80211_node_t *in = ic->ic_bss; 2830 uint8_t essid[IEEE80211_NWID_LEN+1]; 2831 struct ieee80211_rateset *rs; 2832 enum ieee80211_phymode mode; 2833 uint8_t *frm; 2834 int i, pktlen, nrates; 2835 2836 /* previous scan not completed */ 2837 if (sc->sc_scan_pending) { 2838 WPI_DBG((WPI_DEBUG_SCAN, "previous scan not completed\n")); 2839 return (WPI_SUCCESS); 2840 } 2841 2842 data = &ring->data[ring->cur]; 2843 desc = data->desc; 2844 cmd = (wpi_tx_cmd_t *)data->dma_data.mem_va; 2845 2846 cmd->code = WPI_CMD_SCAN; 2847 cmd->flags = 0; 2848 cmd->qid = ring->qid; 2849 cmd->idx = ring->cur; 2850 2851 hdr = (wpi_scan_hdr_t *)cmd->data; 2852 (void) memset(hdr, 0, sizeof (wpi_scan_hdr_t)); 2853 hdr->first = 1; 2854 hdr->nchan = 1; 2855 hdr->len = hdr->nchan * sizeof (wpi_scan_chan_t); 2856 hdr->quiet = LE_16(50); 2857 hdr->threshold = LE_16(1); 2858 hdr->filter = LE_32(5); 2859 hdr->rate = wpi_plcp_signal(2); 2860 hdr->id = WPI_ID_BROADCAST; 2861 hdr->mask = LE_32(0xffffffff); 2862 hdr->esslen = ic->ic_des_esslen; 2863 2864 if (ic->ic_des_esslen) { 2865 bcopy(ic->ic_des_essid, essid, ic->ic_des_esslen); 2866 essid[ic->ic_des_esslen] = '\0'; 2867 WPI_DBG((WPI_DEBUG_SCAN, "directed scan %s\n", essid)); 2868 2869 bcopy(ic->ic_des_essid, hdr->essid, ic->ic_des_esslen); 2870 } else { 2871 bzero(hdr->essid, sizeof (hdr->essid)); 2872 } 2873 2874 /* 2875 * Build a probe request frame. Most of the following code is a 2876 * copy & paste of what is done in net80211. Unfortunately, the 2877 * functions to add IEs are static and thus can't be reused here. 2878 */ 2879 wh = (struct ieee80211_frame *)(hdr + 1); 2880 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 2881 IEEE80211_FC0_SUBTYPE_PROBE_REQ; 2882 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2883 (void) memset(wh->i_addr1, 0xff, 6); 2884 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_macaddr); 2885 (void) memset(wh->i_addr3, 0xff, 6); 2886 *(uint16_t *)&wh->i_dur[0] = 0; /* filled by h/w */ 2887 *(uint16_t *)&wh->i_seq[0] = 0; /* filled by h/w */ 2888 2889 frm = (uint8_t *)(wh + 1); 2890 2891 /* add essid IE */ 2892 if (in->in_esslen) { 2893 bcopy(in->in_essid, essid, in->in_esslen); 2894 essid[in->in_esslen] = '\0'; 2895 WPI_DBG((WPI_DEBUG_SCAN, "probe with ESSID %s\n", 2896 essid)); 2897 } 2898 *frm++ = IEEE80211_ELEMID_SSID; 2899 *frm++ = in->in_esslen; 2900 (void) memcpy(frm, in->in_essid, in->in_esslen); 2901 frm += in->in_esslen; 2902 2903 mode = ieee80211_chan2mode(ic, ic->ic_curchan); 2904 rs = &ic->ic_sup_rates[mode]; 2905 2906 /* add supported rates IE */ 2907 *frm++ = IEEE80211_ELEMID_RATES; 2908 nrates = rs->ir_nrates; 2909 if (nrates > IEEE80211_RATE_SIZE) 2910 nrates = IEEE80211_RATE_SIZE; 2911 *frm++ = (uint8_t)nrates; 2912 (void) memcpy(frm, rs->ir_rates, nrates); 2913 frm += nrates; 2914 2915 /* add supported xrates IE */ 2916 if (rs->ir_nrates > IEEE80211_RATE_SIZE) { 2917 nrates = rs->ir_nrates - IEEE80211_RATE_SIZE; 2918 *frm++ = IEEE80211_ELEMID_XRATES; 2919 *frm++ = (uint8_t)nrates; 2920 (void) memcpy(frm, rs->ir_rates + IEEE80211_RATE_SIZE, nrates); 2921 frm += nrates; 2922 } 2923 2924 /* add optionnal IE (usually an RSN IE) */ 2925 if (ic->ic_opt_ie != NULL) { 2926 (void) memcpy(frm, ic->ic_opt_ie, ic->ic_opt_ie_len); 2927 frm += ic->ic_opt_ie_len; 2928 } 2929 2930 /* setup length of probe request */ 2931 hdr->pbrlen = LE_16((uintptr_t)frm - (uintptr_t)wh); 2932 2933 /* align on a 4-byte boundary */ 2934 chan = (wpi_scan_chan_t *)frm; 2935 for (i = 1; i <= hdr->nchan; i++, chan++) { 2936 if (ic->ic_des_esslen) { 2937 chan->flags = 0x3; 2938 } else { 2939 chan->flags = 0x1; 2940 } 2941 chan->chan = ieee80211_chan2ieee(ic, ic->ic_curchan); 2942 chan->magic = LE_16(0x62ab); 2943 chan->active = LE_16(50); 2944 chan->passive = LE_16(120); 2945 2946 frm += sizeof (wpi_scan_chan_t); 2947 } 2948 2949 pktlen = (uintptr_t)frm - (uintptr_t)cmd; 2950 2951 desc->flags = LE_32(WPI_PAD32(pktlen) << 28 | 1 << 24); 2952 desc->segs[0].addr = LE_32(data->dma_data.cookie.dmac_address); 2953 desc->segs[0].len = LE_32(pktlen); 2954 2955 WPI_DMA_SYNC(data->dma_data, DDI_DMA_SYNC_FORDEV); 2956 WPI_DMA_SYNC(ring->dma_desc, DDI_DMA_SYNC_FORDEV); 2957 2958 /* kick cmd ring */ 2959 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT; 2960 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2961 2962 sc->sc_scan_pending = 1; 2963 2964 return (WPI_SUCCESS); /* will be notified async. of failure/success */ 2965 } 2966 2967 static int 2968 wpi_config(wpi_sc_t *sc) 2969 { 2970 ieee80211com_t *ic = &sc->sc_ic; 2971 wpi_txpower_t txpower; 2972 wpi_power_t power; 2973 #ifdef WPI_BLUE_COEXISTENCE 2974 wpi_bluetooth_t bluetooth; 2975 #endif 2976 wpi_node_t node; 2977 int err; 2978 2979 /* Intel's binary only daemon is a joke.. */ 2980 2981 /* set Tx power for 2.4GHz channels (values read from EEPROM) */ 2982 (void) memset(&txpower, 0, sizeof (txpower)); 2983 (void) memcpy(txpower.pwr1, sc->sc_pwr1, 14 * sizeof (uint16_t)); 2984 (void) memcpy(txpower.pwr2, sc->sc_pwr2, 14 * sizeof (uint16_t)); 2985 err = wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof (txpower), 0); 2986 if (err != WPI_SUCCESS) { 2987 cmn_err(CE_WARN, "wpi_config(): failed to set txpower\n"); 2988 return (err); 2989 } 2990 2991 /* set power mode */ 2992 (void) memset(&power, 0, sizeof (power)); 2993 power.flags = LE_32(0x8); 2994 err = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof (power), 0); 2995 if (err != WPI_SUCCESS) { 2996 cmn_err(CE_WARN, "wpi_config(): failed to set power mode\n"); 2997 return (err); 2998 } 2999 #ifdef WPI_BLUE_COEXISTENCE 3000 /* configure bluetooth coexistence */ 3001 (void) memset(&bluetooth, 0, sizeof (bluetooth)); 3002 bluetooth.flags = 3; 3003 bluetooth.lead = 0xaa; 3004 bluetooth.kill = 1; 3005 err = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, 3006 sizeof (bluetooth), 0); 3007 if (err != WPI_SUCCESS) { 3008 cmn_err(CE_WARN, 3009 "wpi_config(): " 3010 "failed to configurate bluetooth coexistence\n"); 3011 return (err); 3012 } 3013 #endif 3014 /* configure adapter */ 3015 (void) memset(&sc->sc_config, 0, sizeof (wpi_config_t)); 3016 IEEE80211_ADDR_COPY(sc->sc_config.myaddr, ic->ic_macaddr); 3017 sc->sc_config.chan = ieee80211_chan2ieee(ic, ic->ic_curchan); 3018 sc->sc_config.flags = LE_32(WPI_CONFIG_TSF | WPI_CONFIG_AUTO | 3019 WPI_CONFIG_24GHZ); 3020 sc->sc_config.filter = 0; 3021 switch (ic->ic_opmode) { 3022 case IEEE80211_M_STA: 3023 sc->sc_config.mode = WPI_MODE_STA; 3024 sc->sc_config.filter |= LE_32(WPI_FILTER_MULTICAST); 3025 break; 3026 case IEEE80211_M_IBSS: 3027 case IEEE80211_M_AHDEMO: 3028 sc->sc_config.mode = WPI_MODE_IBSS; 3029 break; 3030 case IEEE80211_M_HOSTAP: 3031 sc->sc_config.mode = WPI_MODE_HOSTAP; 3032 break; 3033 case IEEE80211_M_MONITOR: 3034 sc->sc_config.mode = WPI_MODE_MONITOR; 3035 sc->sc_config.filter |= LE_32(WPI_FILTER_MULTICAST | 3036 WPI_FILTER_CTL | WPI_FILTER_PROMISC); 3037 break; 3038 } 3039 sc->sc_config.cck_mask = 0x0f; /* not yet negotiated */ 3040 sc->sc_config.ofdm_mask = 0xff; /* not yet negotiated */ 3041 err = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->sc_config, 3042 sizeof (wpi_config_t), 0); 3043 if (err != WPI_SUCCESS) { 3044 cmn_err(CE_WARN, "wpi_config(): " 3045 "failed to set configure command\n"); 3046 return (err); 3047 } 3048 3049 /* add broadcast node */ 3050 (void) memset(&node, 0, sizeof (node)); 3051 (void) memset(node.bssid, 0xff, 6); 3052 node.id = WPI_ID_BROADCAST; 3053 node.rate = wpi_plcp_signal(2); 3054 err = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof (node), 0); 3055 if (err != WPI_SUCCESS) { 3056 cmn_err(CE_WARN, "wpi_config(): " 3057 "failed to add broadcast node\n"); 3058 return (err); 3059 } 3060 3061 return (WPI_SUCCESS); 3062 } 3063 3064 static void 3065 wpi_stop_master(wpi_sc_t *sc) 3066 { 3067 uint32_t tmp; 3068 int ntries; 3069 3070 tmp = WPI_READ(sc, WPI_RESET); 3071 WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER); 3072 3073 tmp = WPI_READ(sc, WPI_GPIO_CTL); 3074 if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP) 3075 return; /* already asleep */ 3076 3077 for (ntries = 0; ntries < 2000; ntries++) { 3078 if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED) 3079 break; 3080 DELAY(1000); 3081 } 3082 if (ntries == 2000) 3083 WPI_DBG((WPI_DEBUG_HW, "timeout waiting for master\n")); 3084 } 3085 3086 static int 3087 wpi_power_up(wpi_sc_t *sc) 3088 { 3089 uint32_t tmp; 3090 int ntries; 3091 3092 wpi_mem_lock(sc); 3093 tmp = wpi_mem_read(sc, WPI_MEM_POWER); 3094 wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000); 3095 wpi_mem_unlock(sc); 3096 3097 for (ntries = 0; ntries < 5000; ntries++) { 3098 if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED) 3099 break; 3100 DELAY(10); 3101 } 3102 if (ntries == 5000) { 3103 cmn_err(CE_WARN, 3104 "wpi_power_up(): timeout waiting for NIC to power up\n"); 3105 return (ETIMEDOUT); 3106 } 3107 return (WPI_SUCCESS); 3108 } 3109 3110 static int 3111 wpi_reset(wpi_sc_t *sc) 3112 { 3113 uint32_t tmp; 3114 int ntries; 3115 3116 /* clear any pending interrupts */ 3117 WPI_WRITE(sc, WPI_INTR, 0xffffffff); 3118 3119 tmp = WPI_READ(sc, WPI_PLL_CTL); 3120 WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT); 3121 3122 tmp = WPI_READ(sc, WPI_CHICKEN); 3123 WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS); 3124 3125 tmp = WPI_READ(sc, WPI_GPIO_CTL); 3126 WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT); 3127 3128 /* wait for clock stabilization */ 3129 for (ntries = 0; ntries < 1000; ntries++) { 3130 if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK) 3131 break; 3132 DELAY(10); 3133 } 3134 if (ntries == 1000) { 3135 cmn_err(CE_WARN, 3136 "wpi_reset(): timeout waiting for clock stabilization\n"); 3137 return (ETIMEDOUT); 3138 } 3139 3140 /* initialize EEPROM */ 3141 tmp = WPI_READ(sc, WPI_EEPROM_STATUS); 3142 if ((tmp & WPI_EEPROM_VERSION) == 0) { 3143 cmn_err(CE_WARN, "wpi_reset(): EEPROM not found\n"); 3144 return (EIO); 3145 } 3146 WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED); 3147 3148 return (WPI_SUCCESS); 3149 } 3150 3151 static void 3152 wpi_hw_config(wpi_sc_t *sc) 3153 { 3154 uint16_t val; 3155 uint32_t hw; 3156 3157 /* voodoo from the Linux "driver".. */ 3158 hw = WPI_READ(sc, WPI_HWCONFIG); 3159 3160 if ((sc->sc_rev & 0xc0) == 0x40) 3161 hw |= WPI_HW_ALM_MB; 3162 else if (!(sc->sc_rev & 0x80)) 3163 hw |= WPI_HW_ALM_MM; 3164 3165 val = wpi_read_prom_word(sc, WPI_EEPROM_CAPABILITIES); 3166 if ((val & 0xff) == 0x80) 3167 hw |= WPI_HW_SKU_MRC; 3168 3169 val = wpi_read_prom_word(sc, WPI_EEPROM_REVISION); 3170 hw &= ~WPI_HW_REV_D; 3171 if ((val & 0xf0) == 0xd0) 3172 hw |= WPI_HW_REV_D; 3173 3174 val = wpi_read_prom_word(sc, WPI_EEPROM_TYPE); 3175 if ((val & 0xff) > 1) 3176 hw |= WPI_HW_TYPE_B; 3177 3178 WPI_DBG((WPI_DEBUG_HW, "setting h/w config %x\n", hw)); 3179 WPI_WRITE(sc, WPI_HWCONFIG, hw); 3180 } 3181 3182 static int 3183 wpi_init(wpi_sc_t *sc) 3184 { 3185 uint32_t tmp; 3186 int qid, ntries, err; 3187 clock_t clk; 3188 3189 mutex_enter(&sc->sc_glock); 3190 sc->sc_flags &= ~WPI_F_FW_INIT; 3191 3192 (void) wpi_reset(sc); 3193 3194 wpi_mem_lock(sc); 3195 wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00); 3196 DELAY(20); 3197 tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV); 3198 wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800); 3199 wpi_mem_unlock(sc); 3200 3201 (void) wpi_power_up(sc); 3202 wpi_hw_config(sc); 3203 3204 tmp = WPI_READ(sc, WPI_GPIO_CTL); 3205 if (!(tmp & WPI_GPIO_HW_RF_KILL)) { 3206 cmn_err(CE_WARN, "wpi_init(): Radio transmitter is off\n"); 3207 goto fail1; 3208 } 3209 3210 /* init Rx ring */ 3211 wpi_mem_lock(sc); 3212 WPI_WRITE(sc, WPI_RX_BASE, sc->sc_rxq.dma_desc.cookie.dmac_address); 3213 WPI_WRITE(sc, WPI_RX_RIDX_PTR, 3214 (uint32_t)(sc->sc_dma_sh.cookie.dmac_address + 3215 offsetof(wpi_shared_t, next))); 3216 WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & (~7)); 3217 WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010); 3218 wpi_mem_unlock(sc); 3219 3220 /* init Tx rings */ 3221 wpi_mem_lock(sc); 3222 wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */ 3223 wpi_mem_write(sc, WPI_MEM_RA, 1); /* enable RA0 */ 3224 wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */ 3225 wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000); 3226 wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002); 3227 wpi_mem_write(sc, WPI_MEM_MAGIC4, 4); 3228 wpi_mem_write(sc, WPI_MEM_MAGIC5, 5); 3229 3230 WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->sc_dma_sh.cookie.dmac_address); 3231 WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5); 3232 3233 for (qid = 0; qid < 6; qid++) { 3234 WPI_WRITE(sc, WPI_TX_CTL(qid), 0); 3235 WPI_WRITE(sc, WPI_TX_BASE(qid), 0); 3236 WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008); 3237 } 3238 wpi_mem_unlock(sc); 3239 3240 /* clear "radio off" and "disable command" bits (reversed logic) */ 3241 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF); 3242 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD); 3243 3244 /* clear any pending interrupts */ 3245 WPI_WRITE(sc, WPI_INTR, 0xffffffff); 3246 3247 /* enable interrupts */ 3248 WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK); 3249 3250 /* load firmware boot code into NIC */ 3251 err = wpi_load_microcode(sc); 3252 if (err != WPI_SUCCESS) { 3253 cmn_err(CE_WARN, "wpi_init(): failed to load microcode\n"); 3254 goto fail1; 3255 } 3256 3257 /* load firmware .text segment into NIC */ 3258 err = wpi_load_firmware(sc, WPI_FW_TEXT); 3259 if (err != WPI_SUCCESS) { 3260 cmn_err(CE_WARN, "wpi_init(): " 3261 "failed to load firmware(text)\n"); 3262 goto fail1; 3263 } 3264 3265 /* load firmware .data segment into NIC */ 3266 err = wpi_load_firmware(sc, WPI_FW_DATA); 3267 if (err != WPI_SUCCESS) { 3268 cmn_err(CE_WARN, "wpi_init(): " 3269 "failed to load firmware(data)\n"); 3270 goto fail1; 3271 } 3272 3273 /* now press "execute" ;-) */ 3274 tmp = WPI_READ(sc, WPI_RESET); 3275 tmp &= ~(WPI_MASTER_DISABLED | WPI_STOP_MASTER | WPI_NEVO_RESET); 3276 WPI_WRITE(sc, WPI_RESET, tmp); 3277 3278 /* ..and wait at most one second for adapter to initialize */ 3279 clk = ddi_get_lbolt() + drv_usectohz(2000000); 3280 while (!(sc->sc_flags & WPI_F_FW_INIT)) { 3281 if (cv_timedwait(&sc->sc_fw_cv, &sc->sc_glock, clk) < 0) 3282 break; 3283 } 3284 if (!(sc->sc_flags & WPI_F_FW_INIT)) { 3285 cmn_err(CE_WARN, 3286 "wpi_init(): timeout waiting for firmware init\n"); 3287 goto fail1; 3288 } 3289 3290 /* wait for thermal sensors to calibrate */ 3291 for (ntries = 0; ntries < 1000; ntries++) { 3292 if (WPI_READ(sc, WPI_TEMPERATURE) != 0) 3293 break; 3294 DELAY(10); 3295 } 3296 3297 if (ntries == 1000) { 3298 WPI_DBG((WPI_DEBUG_HW, 3299 "wpi_init(): timeout waiting for thermal sensors " 3300 "calibration\n")); 3301 } 3302 3303 WPI_DBG((WPI_DEBUG_HW, "temperature %d\n", 3304 (int)WPI_READ(sc, WPI_TEMPERATURE))); 3305 3306 err = wpi_config(sc); 3307 if (err) { 3308 cmn_err(CE_WARN, "wpi_init(): failed to configure device\n"); 3309 goto fail1; 3310 } 3311 3312 mutex_exit(&sc->sc_glock); 3313 return (WPI_SUCCESS); 3314 3315 fail1: 3316 err = WPI_FAIL; 3317 mutex_exit(&sc->sc_glock); 3318 return (err); 3319 } 3320 3321 static void 3322 wpi_stop(wpi_sc_t *sc) 3323 { 3324 uint32_t tmp; 3325 int ac; 3326 3327 3328 mutex_enter(&sc->sc_glock); 3329 /* disable interrupts */ 3330 WPI_WRITE(sc, WPI_MASK, 0); 3331 WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK); 3332 WPI_WRITE(sc, WPI_INTR_STATUS, 0xff); 3333 WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000); 3334 3335 wpi_mem_lock(sc); 3336 wpi_mem_write(sc, WPI_MEM_MODE, 0); 3337 wpi_mem_unlock(sc); 3338 3339 /* reset all Tx rings */ 3340 for (ac = 0; ac < 4; ac++) 3341 wpi_reset_tx_ring(sc, &sc->sc_txq[ac]); 3342 wpi_reset_tx_ring(sc, &sc->sc_cmdq); 3343 wpi_reset_tx_ring(sc, &sc->sc_svcq); 3344 3345 /* reset Rx ring */ 3346 wpi_reset_rx_ring(sc); 3347 3348 wpi_mem_lock(sc); 3349 wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200); 3350 wpi_mem_unlock(sc); 3351 3352 DELAY(5); 3353 3354 wpi_stop_master(sc); 3355 3356 sc->sc_tx_timer = 0; 3357 sc->sc_flags &= ~WPI_F_SCANNING; 3358 sc->sc_scan_pending = 0; 3359 sc->sc_scan_next = 0; 3360 3361 tmp = WPI_READ(sc, WPI_RESET); 3362 WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET); 3363 3364 mutex_exit(&sc->sc_glock); 3365 } 3366 3367 /* 3368 * Naive implementation of the Adaptive Multi Rate Retry algorithm: 3369 * "IEEE 802.11 Rate Adaptation: A Practical Approach" 3370 * Mathieu Lacage, Hossein Manshaei, Thierry Turletti 3371 * INRIA Sophia - Projet Planete 3372 * http://www-sop.inria.fr/rapports/sophia/RR-5208.html 3373 */ 3374 #define is_success(amrr) \ 3375 ((amrr)->retrycnt < (amrr)->txcnt / 10) 3376 #define is_failure(amrr) \ 3377 ((amrr)->retrycnt > (amrr)->txcnt / 3) 3378 #define is_enough(amrr) \ 3379 ((amrr)->txcnt > 100) 3380 #define is_min_rate(in) \ 3381 ((in)->in_txrate == 0) 3382 #define is_max_rate(in) \ 3383 ((in)->in_txrate == (in)->in_rates.ir_nrates - 1) 3384 #define increase_rate(in) \ 3385 ((in)->in_txrate++) 3386 #define decrease_rate(in) \ 3387 ((in)->in_txrate--) 3388 #define reset_cnt(amrr) \ 3389 { (amrr)->txcnt = (amrr)->retrycnt = 0; } 3390 3391 #define WPI_AMRR_MIN_SUCCESS_THRESHOLD 1 3392 #define WPI_AMRR_MAX_SUCCESS_THRESHOLD 15 3393 3394 static void 3395 wpi_amrr_init(wpi_amrr_t *amrr) 3396 { 3397 amrr->success = 0; 3398 amrr->recovery = 0; 3399 amrr->txcnt = amrr->retrycnt = 0; 3400 amrr->success_threshold = WPI_AMRR_MIN_SUCCESS_THRESHOLD; 3401 } 3402 3403 static void 3404 wpi_amrr_timeout(wpi_sc_t *sc) 3405 { 3406 ieee80211com_t *ic = &sc->sc_ic; 3407 3408 WPI_DBG((WPI_DEBUG_RATECTL, "wpi_amrr_timeout() enter\n")); 3409 if (ic->ic_opmode == IEEE80211_M_STA) 3410 wpi_amrr_ratectl(NULL, ic->ic_bss); 3411 else 3412 ieee80211_iterate_nodes(&ic->ic_sta, wpi_amrr_ratectl, NULL); 3413 sc->sc_clk = ddi_get_lbolt(); 3414 } 3415 3416 /* ARGSUSED */ 3417 static void 3418 wpi_amrr_ratectl(void *arg, ieee80211_node_t *in) 3419 { 3420 wpi_amrr_t *amrr = (wpi_amrr_t *)in; 3421 int need_change = 0; 3422 3423 if (is_success(amrr) && is_enough(amrr)) { 3424 amrr->success++; 3425 if (amrr->success >= amrr->success_threshold && 3426 !is_max_rate(in)) { 3427 amrr->recovery = 1; 3428 amrr->success = 0; 3429 increase_rate(in); 3430 WPI_DBG((WPI_DEBUG_RATECTL, 3431 "AMRR increasing rate %d (txcnt=%d retrycnt=%d)\n", 3432 in->in_txrate, amrr->txcnt, amrr->retrycnt)); 3433 need_change = 1; 3434 } else { 3435 amrr->recovery = 0; 3436 } 3437 } else if (is_failure(amrr)) { 3438 amrr->success = 0; 3439 if (!is_min_rate(in)) { 3440 if (amrr->recovery) { 3441 amrr->success_threshold++; 3442 if (amrr->success_threshold > 3443 WPI_AMRR_MAX_SUCCESS_THRESHOLD) 3444 amrr->success_threshold = 3445 WPI_AMRR_MAX_SUCCESS_THRESHOLD; 3446 } else { 3447 amrr->success_threshold = 3448 WPI_AMRR_MIN_SUCCESS_THRESHOLD; 3449 } 3450 decrease_rate(in); 3451 WPI_DBG((WPI_DEBUG_RATECTL, 3452 "AMRR decreasing rate %d (txcnt=%d retrycnt=%d)\n", 3453 in->in_txrate, amrr->txcnt, amrr->retrycnt)); 3454 need_change = 1; 3455 } 3456 amrr->recovery = 0; /* paper is incorrect */ 3457 } 3458 3459 if (is_enough(amrr) || need_change) 3460 reset_cnt(amrr); 3461 } 3462