1 /*- 2 * Copyright (c) 2007 3 * Damien Bergamini <damien.bergamini@free.fr> 4 * Copyright (c) 2008 5 * Benjamin Close <benjsc@FreeBSD.org> 6 * Copyright (c) 2008 Sam Leffler, Errno Consulting 7 * 8 * Permission to use, copy, modify, and distribute this software for any 9 * purpose with or without fee is hereby granted, provided that the above 10 * copyright notice and this permission notice appear in all copies. 11 * 12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 13 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 14 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 15 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 16 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 17 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 18 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 19 */ 20 21 /* 22 * Driver for Intel Wireless WiFi Link 4965AGN 802.11 network adapters. 23 */ 24 25 #include <sys/cdefs.h> 26 __FBSDID("$FreeBSD$"); 27 28 #include <sys/param.h> 29 #include <sys/sockio.h> 30 #include <sys/sysctl.h> 31 #include <sys/mbuf.h> 32 #include <sys/kernel.h> 33 #include <sys/socket.h> 34 #include <sys/systm.h> 35 #include <sys/malloc.h> 36 #include <sys/bus.h> 37 #include <sys/rman.h> 38 #include <sys/endian.h> 39 #include <sys/firmware.h> 40 #include <sys/limits.h> 41 #include <sys/module.h> 42 #include <sys/queue.h> 43 #include <sys/taskqueue.h> 44 45 #include <machine/bus.h> 46 #include <machine/resource.h> 47 #include <machine/clock.h> 48 49 #include <dev/pci/pcireg.h> 50 #include <dev/pci/pcivar.h> 51 52 #include <net/bpf.h> 53 #include <net/if.h> 54 #include <net/if_arp.h> 55 #include <net/ethernet.h> 56 #include <net/if_dl.h> 57 #include <net/if_media.h> 58 #include <net/if_types.h> 59 60 #include <netinet/in.h> 61 #include <netinet/in_systm.h> 62 #include <netinet/in_var.h> 63 #include <netinet/if_ether.h> 64 #include <netinet/ip.h> 65 66 #include <net80211/ieee80211_var.h> 67 #include <net80211/ieee80211_amrr.h> 68 #include <net80211/ieee80211_radiotap.h> 69 #include <net80211/ieee80211_regdomain.h> 70 71 #include <dev/iwn/if_iwnreg.h> 72 #include <dev/iwn/if_iwnvar.h> 73 74 static int iwn_probe(device_t); 75 static int iwn_attach(device_t); 76 static int iwn_detach(device_t); 77 static int iwn_cleanup(device_t); 78 static struct ieee80211vap *iwn_vap_create(struct ieee80211com *, 79 const char name[IFNAMSIZ], int unit, int opmode, 80 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], 81 const uint8_t mac[IEEE80211_ADDR_LEN]); 82 static void iwn_vap_delete(struct ieee80211vap *); 83 static int iwn_shutdown(device_t); 84 static int iwn_suspend(device_t); 85 static int iwn_resume(device_t); 86 static int iwn_dma_contig_alloc(struct iwn_softc *, struct iwn_dma_info *, 87 void **, bus_size_t, bus_size_t, int); 88 static void iwn_dma_contig_free(struct iwn_dma_info *); 89 int iwn_alloc_shared(struct iwn_softc *); 90 void iwn_free_shared(struct iwn_softc *); 91 int iwn_alloc_kw(struct iwn_softc *); 92 void iwn_free_kw(struct iwn_softc *); 93 int iwn_alloc_fwmem(struct iwn_softc *); 94 void iwn_free_fwmem(struct iwn_softc *); 95 struct iwn_rbuf *iwn_alloc_rbuf(struct iwn_softc *); 96 void iwn_free_rbuf(void *, void *); 97 int iwn_alloc_rpool(struct iwn_softc *); 98 void iwn_free_rpool(struct iwn_softc *); 99 int iwn_alloc_rx_ring(struct iwn_softc *, struct iwn_rx_ring *); 100 void iwn_reset_rx_ring(struct iwn_softc *, struct iwn_rx_ring *); 101 void iwn_free_rx_ring(struct iwn_softc *, struct iwn_rx_ring *); 102 int iwn_alloc_tx_ring(struct iwn_softc *, struct iwn_tx_ring *, 103 int); 104 void iwn_reset_tx_ring(struct iwn_softc *, struct iwn_tx_ring *); 105 void iwn_free_tx_ring(struct iwn_softc *, struct iwn_tx_ring *); 106 struct ieee80211_node *iwn_node_alloc(struct ieee80211_node_table *); 107 void iwn_newassoc(struct ieee80211_node *, int); 108 int iwn_media_change(struct ifnet *); 109 int iwn_newstate(struct ieee80211vap *, enum ieee80211_state, int); 110 void iwn_mem_lock(struct iwn_softc *); 111 void iwn_mem_unlock(struct iwn_softc *); 112 uint32_t iwn_mem_read(struct iwn_softc *, uint32_t); 113 void iwn_mem_write(struct iwn_softc *, uint32_t, uint32_t); 114 void iwn_mem_write_region_4(struct iwn_softc *, uint32_t, 115 const uint32_t *, int); 116 int iwn_eeprom_lock(struct iwn_softc *); 117 void iwn_eeprom_unlock(struct iwn_softc *); 118 int iwn_read_prom_data(struct iwn_softc *, uint32_t, void *, int); 119 int iwn_transfer_microcode(struct iwn_softc *, const uint8_t *, int); 120 int iwn_transfer_firmware(struct iwn_softc *); 121 int iwn_load_firmware(struct iwn_softc *); 122 void iwn_unload_firmware(struct iwn_softc *); 123 static void iwn_timer_timeout(void *); 124 static void iwn_calib_reset(struct iwn_softc *); 125 void iwn_ampdu_rx_start(struct iwn_softc *, struct iwn_rx_desc *); 126 void iwn_rx_intr(struct iwn_softc *, struct iwn_rx_desc *, 127 struct iwn_rx_data *); 128 void iwn_rx_statistics(struct iwn_softc *, struct iwn_rx_desc *); 129 void iwn_tx_intr(struct iwn_softc *, struct iwn_rx_desc *); 130 void iwn_cmd_intr(struct iwn_softc *, struct iwn_rx_desc *); 131 static void iwn_bmiss(void *, int); 132 void iwn_notif_intr(struct iwn_softc *); 133 void iwn_intr(void *); 134 void iwn_read_eeprom(struct iwn_softc *); 135 static void iwn_read_eeprom_channels(struct iwn_softc *); 136 void iwn_print_power_group(struct iwn_softc *, int); 137 uint8_t iwn_plcp_signal(int); 138 int iwn_tx_data(struct iwn_softc *, struct mbuf *, 139 struct ieee80211_node *, struct iwn_tx_ring *); 140 void iwn_start(struct ifnet *); 141 static int iwn_raw_xmit(struct ieee80211_node *, struct mbuf *, 142 const struct ieee80211_bpf_params *); 143 static void iwn_watchdog(struct iwn_softc *); 144 int iwn_ioctl(struct ifnet *, u_long, caddr_t); 145 int iwn_cmd(struct iwn_softc *, int, const void *, int, int); 146 int iwn_set_link_quality(struct iwn_softc *, uint8_t, 147 const struct ieee80211_channel *, int); 148 int iwn_set_key(struct ieee80211com *, struct ieee80211_node *, 149 const struct ieee80211_key *); 150 int iwn_wme_update(struct ieee80211com *); 151 void iwn_set_led(struct iwn_softc *, uint8_t, uint8_t, uint8_t); 152 int iwn_set_critical_temp(struct iwn_softc *); 153 void iwn_enable_tsf(struct iwn_softc *, struct ieee80211_node *); 154 void iwn_power_calibration(struct iwn_softc *, int); 155 int iwn_set_txpower(struct iwn_softc *, 156 struct ieee80211_channel *, int); 157 int8_t iwn_get_rssi(struct iwn_softc *, const struct iwn_rx_stat *); 158 int iwn_get_noise(const struct iwn_rx_general_stats *); 159 int iwn_get_temperature(struct iwn_softc *); 160 int iwn_init_sensitivity(struct iwn_softc *); 161 void iwn_compute_differential_gain(struct iwn_softc *, 162 const struct iwn_rx_general_stats *); 163 void iwn_tune_sensitivity(struct iwn_softc *, 164 const struct iwn_rx_stats *); 165 int iwn_send_sensitivity(struct iwn_softc *); 166 int iwn_auth(struct iwn_softc *); 167 int iwn_run(struct iwn_softc *); 168 int iwn_scan(struct iwn_softc *); 169 int iwn_config(struct iwn_softc *); 170 void iwn_post_alive(struct iwn_softc *); 171 void iwn_stop_master(struct iwn_softc *); 172 int iwn_reset(struct iwn_softc *); 173 void iwn_hw_config(struct iwn_softc *); 174 void iwn_init_locked(struct iwn_softc *); 175 void iwn_init(void *); 176 void iwn_stop_locked(struct iwn_softc *); 177 void iwn_stop(struct iwn_softc *); 178 static void iwn_scan_start(struct ieee80211com *); 179 static void iwn_scan_end(struct ieee80211com *); 180 static void iwn_set_channel(struct ieee80211com *); 181 static void iwn_scan_curchan(struct ieee80211_scan_state *, unsigned long); 182 static void iwn_scan_mindwell(struct ieee80211_scan_state *); 183 static void iwn_ops(void *, int); 184 static int iwn_queue_cmd( struct iwn_softc *, int, int, int); 185 static void iwn_bpfattach(struct iwn_softc *); 186 static void iwn_sysctlattach(struct iwn_softc *); 187 188 #define IWN_DEBUG 189 #ifdef IWN_DEBUG 190 enum { 191 IWN_DEBUG_XMIT = 0x00000001, /* basic xmit operation */ 192 IWN_DEBUG_RECV = 0x00000002, /* basic recv operation */ 193 IWN_DEBUG_STATE = 0x00000004, /* 802.11 state transitions */ 194 IWN_DEBUG_TXPOW = 0x00000008, /* tx power processing */ 195 IWN_DEBUG_RESET = 0x00000010, /* reset processing */ 196 IWN_DEBUG_OPS = 0x00000020, /* iwn_ops processing */ 197 IWN_DEBUG_BEACON = 0x00000040, /* beacon handling */ 198 IWN_DEBUG_WATCHDOG = 0x00000080, /* watchdog timeout */ 199 IWN_DEBUG_INTR = 0x00000100, /* ISR */ 200 IWN_DEBUG_CALIBRATE = 0x00000200, /* periodic calibration */ 201 IWN_DEBUG_NODE = 0x00000400, /* node management */ 202 IWN_DEBUG_LED = 0x00000800, /* led management */ 203 IWN_DEBUG_CMD = 0x00001000, /* cmd submission */ 204 IWN_DEBUG_FATAL = 0x80000000, /* fatal errors */ 205 IWN_DEBUG_ANY = 0xffffffff 206 }; 207 208 #define DPRINTF(sc, m, fmt, ...) do { \ 209 if (sc->sc_debug & (m)) \ 210 printf(fmt, __VA_ARGS__); \ 211 } while (0) 212 213 static const char *iwn_ops_str(int); 214 static const char *iwn_intr_str(uint8_t); 215 #else 216 #define DPRINTF(sc, m, fmt, ...) do { (void) sc; } while (0) 217 #endif 218 219 struct iwn_ident { 220 uint16_t vendor; 221 uint16_t device; 222 const char *name; 223 }; 224 225 static const struct iwn_ident iwn_ident_table [] = { 226 { 0x8086, 0x4229, "Intel(R) PRO/Wireless 4965BGN" }, 227 { 0x8086, 0x422D, "Intel(R) PRO/Wireless 4965BGN" }, 228 { 0x8086, 0x4230, "Intel(R) PRO/Wireless 4965BGN" }, 229 { 0x8086, 0x4233, "Intel(R) PRO/Wireless 4965BGN" }, 230 { 0, 0, NULL } 231 }; 232 233 static int 234 iwn_probe(device_t dev) 235 { 236 const struct iwn_ident *ident; 237 238 for (ident = iwn_ident_table; ident->name != NULL; ident++) { 239 if (pci_get_vendor(dev) == ident->vendor && 240 pci_get_device(dev) == ident->device) { 241 device_set_desc(dev, ident->name); 242 return 0; 243 } 244 } 245 return ENXIO; 246 } 247 248 static int 249 iwn_attach(device_t dev) 250 { 251 struct iwn_softc *sc = (struct iwn_softc *)device_get_softc(dev); 252 struct ieee80211com *ic; 253 struct ifnet *ifp; 254 int i, error; 255 256 sc->sc_dev = dev; 257 258 /* XXX */ 259 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 260 device_printf(dev, "chip is in D%d power mode " 261 "-- setting to D0\n", pci_get_powerstate(dev)); 262 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 263 } 264 265 /* clear device specific PCI configuration register 0x41 */ 266 pci_write_config(dev, 0x41, 0, 1); 267 268 /* enable bus-mastering */ 269 pci_enable_busmaster(dev); 270 271 sc->mem_rid= PCIR_BAR(0); 272 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid, 273 RF_ACTIVE); 274 if (sc->mem == NULL ) { 275 device_printf(dev, "could not allocate memory resources\n"); 276 error = ENOMEM; 277 return error; 278 } 279 280 sc->sc_st = rman_get_bustag(sc->mem); 281 sc->sc_sh = rman_get_bushandle(sc->mem); 282 sc->irq_rid = 0; 283 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 284 RF_ACTIVE | RF_SHAREABLE); 285 if (sc->irq == NULL) { 286 device_printf(dev, "could not allocate interrupt resource\n"); 287 error = ENOMEM; 288 return error; 289 } 290 291 IWN_LOCK_INIT(sc); 292 IWN_CMD_LOCK_INIT(sc); 293 callout_init_mtx(&sc->sc_timer_to, &sc->sc_mtx, 0); 294 295 /* 296 * Create the taskqueues used by the driver. Primarily 297 * sc_tq handles most the task 298 */ 299 sc->sc_tq = taskqueue_create("iwn_taskq", M_NOWAIT | M_ZERO, 300 taskqueue_thread_enqueue, &sc->sc_tq); 301 taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq", 302 device_get_nameunit(dev)); 303 304 TASK_INIT(&sc->sc_ops_task, 0, iwn_ops, sc ); 305 TASK_INIT(&sc->sc_bmiss_task, 0, iwn_bmiss, sc); 306 307 /* 308 * Put adapter into a known state. 309 */ 310 error = iwn_reset(sc); 311 if (error != 0) { 312 device_printf(dev, 313 "could not reset adapter, error %d\n", error); 314 goto fail; 315 } 316 317 /* 318 * Allocate DMA memory for firmware transfers. 319 */ 320 error = iwn_alloc_fwmem(sc); 321 if (error != 0) { 322 device_printf(dev, 323 "could not allocate firmware memory, error %d\n", error); 324 goto fail; 325 } 326 327 /* 328 * Allocate a "keep warm" page. 329 */ 330 error = iwn_alloc_kw(sc); 331 if (error != 0) { 332 device_printf(dev, 333 "could not allocate keep-warm page, error %d\n", error); 334 goto fail; 335 } 336 337 /* 338 * Allocate shared area (communication area). 339 */ 340 error = iwn_alloc_shared(sc); 341 if (error != 0) { 342 device_printf(dev, 343 "could not allocate shared area, error %d\n", error); 344 goto fail; 345 } 346 347 /* 348 * Allocate Tx rings. 349 */ 350 for (i = 0; i < IWN_NTXQUEUES; i++) { 351 error = iwn_alloc_tx_ring(sc, &sc->txq[i], i); 352 if (error != 0) { 353 device_printf(dev, 354 "could not allocate Tx ring %d, error %d\n", 355 i, error); 356 goto fail; 357 } 358 } 359 360 error = iwn_alloc_rx_ring(sc, &sc->rxq); 361 if (error != 0 ){ 362 device_printf(dev, 363 "could not allocate Rx ring, error %d\n", error); 364 goto fail; 365 } 366 367 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 368 if (ifp == NULL) { 369 device_printf(dev, "can not allocate ifnet structure\n"); 370 goto fail; 371 } 372 ic = ifp->if_l2com; 373 374 ic->ic_ifp = ifp; 375 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 376 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 377 378 /* set device capabilities */ 379 ic->ic_caps = 380 IEEE80211_C_MONITOR /* monitor mode supported */ 381 | IEEE80211_C_TXPMGT /* tx power management */ 382 | IEEE80211_C_SHSLOT /* short slot time supported */ 383 | IEEE80211_C_WPA 384 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 385 #if 0 386 | IEEE80211_C_BGSCAN /* background scanning */ 387 | IEEE80211_C_IBSS /* ibss/adhoc mode */ 388 #endif 389 | IEEE80211_C_WME /* WME */ 390 ; 391 #if 0 392 /* XXX disable until HT channel setup works */ 393 ic->ic_htcaps = 394 IEEE80211_HTCAP_SMPS_ENA /* SM PS mode enabled */ 395 | IEEE80211_HTCAP_CHWIDTH40 /* 40MHz channel width */ 396 | IEEE80211_HTCAP_SHORTGI20 /* short GI in 20MHz */ 397 | IEEE80211_HTCAP_SHORTGI40 /* short GI in 40MHz */ 398 | IEEE80211_HTCAP_RXSTBC_2STREAM/* 1-2 spatial streams */ 399 | IEEE80211_HTCAP_MAXAMSDU_3839 /* max A-MSDU length */ 400 /* s/w capabilities */ 401 | IEEE80211_HTC_HT /* HT operation */ 402 | IEEE80211_HTC_AMPDU /* tx A-MPDU */ 403 | IEEE80211_HTC_AMSDU /* tx A-MSDU */ 404 ; 405 #endif 406 /* read supported channels and MAC address from EEPROM */ 407 iwn_read_eeprom(sc); 408 409 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 410 ifp->if_softc = sc; 411 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 412 ifp->if_init = iwn_init; 413 ifp->if_ioctl = iwn_ioctl; 414 ifp->if_start = iwn_start; 415 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 416 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 417 IFQ_SET_READY(&ifp->if_snd); 418 419 ieee80211_ifattach(ic); 420 ic->ic_vap_create = iwn_vap_create; 421 ic->ic_vap_delete = iwn_vap_delete; 422 ic->ic_raw_xmit = iwn_raw_xmit; 423 ic->ic_node_alloc = iwn_node_alloc; 424 ic->ic_newassoc = iwn_newassoc; 425 ic->ic_wme.wme_update = iwn_wme_update; 426 ic->ic_scan_start = iwn_scan_start; 427 ic->ic_scan_end = iwn_scan_end; 428 ic->ic_set_channel = iwn_set_channel; 429 ic->ic_scan_curchan = iwn_scan_curchan; 430 ic->ic_scan_mindwell = iwn_scan_mindwell; 431 432 iwn_bpfattach(sc); 433 iwn_sysctlattach(sc); 434 435 /* 436 * Hook our interrupt after all initialization is complete. 437 */ 438 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 439 NULL, iwn_intr, sc, &sc->sc_ih); 440 if (error != 0) { 441 device_printf(dev, "could not set up interrupt, error %d\n", error); 442 goto fail; 443 } 444 445 ieee80211_announce(ic); 446 return 0; 447 fail: 448 iwn_cleanup(dev); 449 return error; 450 } 451 452 static int 453 iwn_detach(device_t dev) 454 { 455 iwn_cleanup(dev); 456 return 0; 457 } 458 459 /* 460 * Cleanup any device resources that were allocated 461 */ 462 int 463 iwn_cleanup(device_t dev) 464 { 465 struct iwn_softc *sc = device_get_softc(dev); 466 struct ifnet *ifp = sc->sc_ifp; 467 struct ieee80211com *ic = ifp->if_l2com; 468 int i; 469 470 if (ifp != NULL) { 471 iwn_stop(sc); 472 callout_drain(&sc->sc_timer_to); 473 bpfdetach(ifp); 474 ieee80211_ifdetach(ic); 475 } 476 477 iwn_unload_firmware(sc); 478 479 iwn_free_rx_ring(sc, &sc->rxq); 480 for (i = 0; i < IWN_NTXQUEUES; i++) 481 iwn_free_tx_ring(sc, &sc->txq[i]); 482 iwn_free_kw(sc); 483 iwn_free_fwmem(sc); 484 if (sc->irq != NULL) { 485 bus_teardown_intr(dev, sc->irq, sc->sc_ih); 486 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 487 } 488 if (sc->mem != NULL) 489 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 490 if (ifp != NULL) 491 if_free(ifp); 492 taskqueue_free(sc->sc_tq); 493 IWN_CMD_LOCK_DESTROY(sc); 494 IWN_LOCK_DESTROY(sc); 495 return 0; 496 } 497 498 static struct ieee80211vap * 499 iwn_vap_create(struct ieee80211com *ic, 500 const char name[IFNAMSIZ], int unit, int opmode, int flags, 501 const uint8_t bssid[IEEE80211_ADDR_LEN], 502 const uint8_t mac[IEEE80211_ADDR_LEN]) 503 { 504 struct iwn_vap *ivp; 505 struct ieee80211vap *vap; 506 507 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 508 return NULL; 509 ivp = (struct iwn_vap *) malloc(sizeof(struct iwn_vap), 510 M_80211_VAP, M_NOWAIT | M_ZERO); 511 if (ivp == NULL) 512 return NULL; 513 vap = &ivp->iv_vap; 514 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac); 515 vap->iv_bmissthreshold = 10; /* override default */ 516 /* override with driver methods */ 517 ivp->iv_newstate = vap->iv_newstate; 518 vap->iv_newstate = iwn_newstate; 519 520 ieee80211_amrr_init(&ivp->iv_amrr, vap, 521 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD, 522 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD, 523 500 /*ms*/); 524 525 /* complete setup */ 526 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status); 527 ic->ic_opmode = opmode; 528 return vap; 529 } 530 531 static void 532 iwn_vap_delete(struct ieee80211vap *vap) 533 { 534 struct iwn_vap *ivp = IWN_VAP(vap); 535 536 ieee80211_amrr_cleanup(&ivp->iv_amrr); 537 ieee80211_vap_detach(vap); 538 free(ivp, M_80211_VAP); 539 } 540 541 static int 542 iwn_shutdown(device_t dev) 543 { 544 struct iwn_softc *sc = device_get_softc(dev); 545 546 iwn_stop(sc); 547 return 0; 548 } 549 550 static int 551 iwn_suspend(device_t dev) 552 { 553 struct iwn_softc *sc = device_get_softc(dev); 554 555 iwn_stop(sc); 556 return 0; 557 } 558 559 static int 560 iwn_resume(device_t dev) 561 { 562 struct iwn_softc *sc = device_get_softc(dev); 563 struct ifnet *ifp = sc->sc_ifp; 564 565 pci_write_config(dev, 0x41, 0, 1); 566 567 if (ifp->if_flags & IFF_UP) 568 iwn_init(sc); 569 return 0; 570 } 571 572 static void 573 iwn_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 574 { 575 if (error != 0) 576 return; 577 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs)); 578 *(bus_addr_t *)arg = segs[0].ds_addr; 579 } 580 581 static int 582 iwn_dma_contig_alloc(struct iwn_softc *sc, struct iwn_dma_info *dma, 583 void **kvap, bus_size_t size, bus_size_t alignment, int flags) 584 { 585 int error, lalignment, i; 586 587 /* 588 * FreeBSD can't guarrenty 16k alignment at the moment (11/2007) so 589 * we allocate an extra 12k with 4k alignement and walk through 590 * it trying to find where the alignment is. It's a nasty fix for 591 * a bigger problem. 592 */ 593 DPRINTF(sc, IWN_DEBUG_RESET, 594 "Size: %zd - alignment %zd\n", size, alignment); 595 if (alignment == 0x4000) { 596 size += 12*1024; 597 lalignment = 4096; 598 DPRINTF(sc, IWN_DEBUG_RESET, "%s\n", 599 "Attempting to find a 16k boundary"); 600 } else 601 lalignment = alignment; 602 dma->size = size; 603 dma->tag = NULL; 604 605 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), lalignment, 606 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, size, 607 1, size, flags, NULL, NULL, &dma->tag); 608 if (error != 0) { 609 device_printf(sc->sc_dev, 610 "%s: bus_dma_tag_create failed, error %d\n", 611 __func__, error); 612 goto fail; 613 } 614 error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr, 615 flags | BUS_DMA_ZERO, &dma->map); 616 if (error != 0) { 617 device_printf(sc->sc_dev, 618 "%s: bus_dmamem_alloc failed, error %d\n", 619 __func__, error); 620 goto fail; 621 } 622 if (alignment == 0x4000) { 623 for (i = 0; i < 3 && (((uintptr_t)dma->vaddr) & 0x3fff); i++) { 624 DPRINTF(sc, IWN_DEBUG_RESET, "%s\n", 625 "Memory Unaligned, shifting pointer by 4k"); 626 dma->vaddr += 4096; 627 size -= 4096; 628 } 629 if ((((uintptr_t)dma->vaddr ) & (alignment-1))) { 630 DPRINTF(sc, IWN_DEBUG_ANY, 631 "%s: failed to align memory, vaddr %p, align %zd\n", 632 __func__, dma->vaddr, alignment); 633 error = ENOMEM; 634 goto fail; 635 } 636 } 637 638 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, 639 size, iwn_dma_map_addr, &dma->paddr, flags); 640 if (error != 0) { 641 device_printf(sc->sc_dev, 642 "%s: bus_dmamap_load failed, error %d\n", __func__, error); 643 goto fail; 644 } 645 646 if (kvap != NULL) 647 *kvap = dma->vaddr; 648 return 0; 649 fail: 650 iwn_dma_contig_free(dma); 651 return error; 652 } 653 654 static void 655 iwn_dma_contig_free(struct iwn_dma_info *dma) 656 { 657 if (dma->tag != NULL) { 658 if (dma->map != NULL) { 659 if (dma->paddr == 0) { 660 bus_dmamap_sync(dma->tag, dma->map, 661 BUS_DMASYNC_POSTREAD|BUS_DMASYNC_POSTWRITE); 662 bus_dmamap_unload(dma->tag, dma->map); 663 } 664 bus_dmamem_free(dma->tag, &dma->vaddr, dma->map); 665 } 666 bus_dma_tag_destroy(dma->tag); 667 } 668 } 669 670 int 671 iwn_alloc_shared(struct iwn_softc *sc) 672 { 673 /* must be aligned on a 1KB boundary */ 674 return iwn_dma_contig_alloc(sc, &sc->shared_dma, 675 (void **)&sc->shared, sizeof (struct iwn_shared), 1024, 676 BUS_DMA_NOWAIT); 677 } 678 679 void 680 iwn_free_shared(struct iwn_softc *sc) 681 { 682 iwn_dma_contig_free(&sc->shared_dma); 683 } 684 685 int 686 iwn_alloc_kw(struct iwn_softc *sc) 687 { 688 /* must be aligned on a 4k boundary */ 689 return iwn_dma_contig_alloc(sc, &sc->kw_dma, NULL, 690 PAGE_SIZE, PAGE_SIZE, BUS_DMA_NOWAIT); 691 } 692 693 void 694 iwn_free_kw(struct iwn_softc *sc) 695 { 696 iwn_dma_contig_free(&sc->kw_dma); 697 } 698 699 int 700 iwn_alloc_fwmem(struct iwn_softc *sc) 701 { 702 /* allocate enough contiguous space to store text and data */ 703 return iwn_dma_contig_alloc(sc, &sc->fw_dma, NULL, 704 IWN_FW_MAIN_TEXT_MAXSZ + IWN_FW_MAIN_DATA_MAXSZ, 16, 705 BUS_DMA_NOWAIT); 706 } 707 708 void 709 iwn_free_fwmem(struct iwn_softc *sc) 710 { 711 iwn_dma_contig_free(&sc->fw_dma); 712 } 713 714 int 715 iwn_alloc_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring) 716 { 717 int i, error; 718 719 ring->cur = 0; 720 721 error = iwn_dma_contig_alloc(sc, &ring->desc_dma, 722 (void **)&ring->desc, IWN_RX_RING_COUNT * sizeof (uint32_t), 723 IWN_RING_DMA_ALIGN, BUS_DMA_NOWAIT); 724 if (error != 0) { 725 device_printf(sc->sc_dev, 726 "%s: could not allocate rx ring DMA memory, error %d\n", 727 __func__, error); 728 goto fail; 729 } 730 731 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 732 BUS_SPACE_MAXADDR_32BIT, 733 BUS_SPACE_MAXADDR, NULL, NULL, MJUMPAGESIZE, 1, 734 MJUMPAGESIZE, BUS_DMA_NOWAIT, NULL, NULL, &ring->data_dmat); 735 if (error != 0) { 736 device_printf(sc->sc_dev, 737 "%s: bus_dma_tag_create_failed, error %d\n", 738 __func__, error); 739 goto fail; 740 } 741 742 /* 743 * Setup Rx buffers. 744 */ 745 for (i = 0; i < IWN_RX_RING_COUNT; i++) { 746 struct iwn_rx_data *data = &ring->data[i]; 747 struct mbuf *m; 748 bus_addr_t paddr; 749 750 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 751 if (error != 0) { 752 device_printf(sc->sc_dev, 753 "%s: bus_dmamap_create failed, error %d\n", 754 __func__, error); 755 goto fail; 756 } 757 m = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 758 if (m == NULL) { 759 device_printf(sc->sc_dev, 760 "%s: could not allocate rx mbuf\n", __func__); 761 error = ENOMEM; 762 goto fail; 763 } 764 /* map page */ 765 error = bus_dmamap_load(ring->data_dmat, data->map, 766 mtod(m, caddr_t), MJUMPAGESIZE, 767 iwn_dma_map_addr, &paddr, BUS_DMA_NOWAIT); 768 if (error != 0 && error != EFBIG) { 769 device_printf(sc->sc_dev, 770 "%s: bus_dmamap_load failed, error %d\n", 771 __func__, error); 772 m_freem(m); 773 error = ENOMEM; /* XXX unique code */ 774 goto fail; 775 } 776 bus_dmamap_sync(ring->data_dmat, data->map, 777 BUS_DMASYNC_PREWRITE); 778 779 data->m = m; 780 /* Rx buffers are aligned on a 256-byte boundary */ 781 ring->desc[i] = htole32(paddr >> 8); 782 } 783 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 784 BUS_DMASYNC_PREWRITE); 785 return 0; 786 fail: 787 iwn_free_rx_ring(sc, ring); 788 return error; 789 } 790 791 void 792 iwn_reset_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring) 793 { 794 int ntries; 795 796 iwn_mem_lock(sc); 797 798 IWN_WRITE(sc, IWN_RX_CONFIG, 0); 799 for (ntries = 0; ntries < 100; ntries++) { 800 if (IWN_READ(sc, IWN_RX_STATUS) & IWN_RX_IDLE) 801 break; 802 DELAY(10); 803 } 804 #ifdef IWN_DEBUG 805 if (ntries == 100) 806 DPRINTF(sc, IWN_DEBUG_ANY, "%s\n", "timeout resetting Rx ring"); 807 #endif 808 iwn_mem_unlock(sc); 809 810 ring->cur = 0; 811 } 812 813 void 814 iwn_free_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring) 815 { 816 int i; 817 818 iwn_dma_contig_free(&ring->desc_dma); 819 820 for (i = 0; i < IWN_RX_RING_COUNT; i++) 821 if (ring->data[i].m != NULL) 822 m_freem(ring->data[i].m); 823 } 824 825 int 826 iwn_alloc_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring, int qid) 827 { 828 bus_size_t size; 829 int i, error; 830 831 ring->qid = qid; 832 ring->queued = 0; 833 ring->cur = 0; 834 835 size = IWN_TX_RING_COUNT * sizeof(struct iwn_tx_desc); 836 error = iwn_dma_contig_alloc(sc, &ring->desc_dma, 837 (void **)&ring->desc, size, IWN_RING_DMA_ALIGN, BUS_DMA_NOWAIT); 838 if (error != 0) { 839 device_printf(sc->sc_dev, 840 "%s: could not allocate tx ring DMA memory, error %d\n", 841 __func__, error); 842 goto fail; 843 } 844 845 size = IWN_TX_RING_COUNT * sizeof(struct iwn_tx_cmd); 846 error = iwn_dma_contig_alloc(sc, &ring->cmd_dma, 847 (void **)&ring->cmd, size, 4, BUS_DMA_NOWAIT); 848 if (error != 0) { 849 device_printf(sc->sc_dev, 850 "%s: could not allocate tx cmd DMA memory, error %d\n", 851 __func__, error); 852 goto fail; 853 } 854 855 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 856 BUS_SPACE_MAXADDR_32BIT, 857 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IWN_MAX_SCATTER - 1, 858 MCLBYTES, BUS_DMA_NOWAIT, NULL, NULL, &ring->data_dmat); 859 if (error != 0) { 860 device_printf(sc->sc_dev, 861 "%s: bus_dma_tag_create_failed, error %d\n", 862 __func__, error); 863 goto fail; 864 } 865 866 for (i = 0; i < IWN_TX_RING_COUNT; i++) { 867 struct iwn_tx_data *data = &ring->data[i]; 868 869 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 870 if (error != 0) { 871 device_printf(sc->sc_dev, 872 "%s: bus_dmamap_create failed, error %d\n", 873 __func__, error); 874 goto fail; 875 } 876 bus_dmamap_sync(ring->data_dmat, data->map, 877 BUS_DMASYNC_PREWRITE); 878 } 879 return 0; 880 fail: 881 iwn_free_tx_ring(sc, ring); 882 return error; 883 } 884 885 void 886 iwn_reset_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring) 887 { 888 uint32_t tmp; 889 int i, ntries; 890 891 iwn_mem_lock(sc); 892 893 IWN_WRITE(sc, IWN_TX_CONFIG(ring->qid), 0); 894 for (ntries = 0; ntries < 20; ntries++) { 895 tmp = IWN_READ(sc, IWN_TX_STATUS); 896 if ((tmp & IWN_TX_IDLE(ring->qid)) == IWN_TX_IDLE(ring->qid)) 897 break; 898 DELAY(10); 899 } 900 #ifdef IWN_DEBUG 901 if (ntries == 20) 902 DPRINTF(sc, IWN_DEBUG_RESET, 903 "%s: timeout resetting Tx ring %d\n", __func__, ring->qid); 904 #endif 905 iwn_mem_unlock(sc); 906 907 for (i = 0; i < IWN_TX_RING_COUNT; i++) { 908 struct iwn_tx_data *data = &ring->data[i]; 909 910 if (data->m != NULL) { 911 bus_dmamap_unload(ring->data_dmat, data->map); 912 m_freem(data->m); 913 data->m = NULL; 914 } 915 } 916 917 ring->queued = 0; 918 ring->cur = 0; 919 } 920 921 void 922 iwn_free_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring) 923 { 924 int i; 925 926 iwn_dma_contig_free(&ring->desc_dma); 927 iwn_dma_contig_free(&ring->cmd_dma); 928 929 if (ring->data != NULL) { 930 for (i = 0; i < IWN_TX_RING_COUNT; i++) { 931 struct iwn_tx_data *data = &ring->data[i]; 932 933 if (data->m != NULL) { 934 bus_dmamap_unload(ring->data_dmat, data->map); 935 m_freem(data->m); 936 } 937 } 938 } 939 } 940 941 struct ieee80211_node * 942 iwn_node_alloc(struct ieee80211_node_table *ic) 943 { 944 return malloc(sizeof (struct iwn_node), M_80211_NODE,M_NOWAIT | M_ZERO); 945 } 946 947 void 948 iwn_newassoc(struct ieee80211_node *ni, int isnew) 949 { 950 struct ieee80211vap *vap = ni->ni_vap; 951 952 ieee80211_amrr_node_init(&IWN_VAP(vap)->iv_amrr, 953 &IWN_NODE(ni)->amn, ni); 954 } 955 956 int 957 iwn_media_change(struct ifnet *ifp) 958 { 959 int error = ieee80211_media_change(ifp); 960 /* NB: only the fixed rate can change and that doesn't need a reset */ 961 return (error == ENETRESET ? 0 : error); 962 } 963 964 int 965 iwn_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 966 { 967 struct iwn_vap *ivp = IWN_VAP(vap); 968 struct ieee80211com *ic = vap->iv_ic; 969 struct iwn_softc *sc = ic->ic_ifp->if_softc; 970 int error; 971 972 DPRINTF(sc, IWN_DEBUG_STATE, "%s: %s -> %s\n", __func__, 973 ieee80211_state_name[vap->iv_state], 974 ieee80211_state_name[nstate]); 975 976 IWN_LOCK(sc); 977 callout_stop(&sc->sc_timer_to); 978 IWN_UNLOCK(sc); 979 980 /* 981 * Some state transitions require issuing a configure request 982 * to the adapter. This must be done in a blocking context 983 * so we toss control to the task q thread where the state 984 * change will be finished after the command completes. 985 */ 986 if (nstate == IEEE80211_S_AUTH && vap->iv_state != IEEE80211_S_AUTH) { 987 /* !AUTH -> AUTH requires adapter config */ 988 error = iwn_queue_cmd(sc, IWN_AUTH, arg, IWN_QUEUE_NORMAL); 989 return (error != 0 ? error : EINPROGRESS); 990 } 991 if (nstate == IEEE80211_S_RUN && vap->iv_state != IEEE80211_S_RUN) { 992 /* 993 * !RUN -> RUN requires setting the association id 994 * which is done with a firmware cmd. We also defer 995 * starting the timers until that work is done. 996 */ 997 error = iwn_queue_cmd(sc, IWN_RUN, arg, IWN_QUEUE_NORMAL); 998 return (error != 0 ? error : EINPROGRESS); 999 } 1000 if (nstate == IEEE80211_S_RUN) { 1001 /* 1002 * RUN -> RUN transition; just restart the timers. 1003 */ 1004 iwn_calib_reset(sc); 1005 } 1006 return ivp->iv_newstate(vap, nstate, arg); 1007 } 1008 1009 /* 1010 * Grab exclusive access to NIC memory. 1011 */ 1012 void 1013 iwn_mem_lock(struct iwn_softc *sc) 1014 { 1015 uint32_t tmp; 1016 int ntries; 1017 1018 tmp = IWN_READ(sc, IWN_GPIO_CTL); 1019 IWN_WRITE(sc, IWN_GPIO_CTL, tmp | IWN_GPIO_MAC); 1020 1021 /* spin until we actually get the lock */ 1022 for (ntries = 0; ntries < 1000; ntries++) { 1023 if ((IWN_READ(sc, IWN_GPIO_CTL) & 1024 (IWN_GPIO_CLOCK | IWN_GPIO_SLEEP)) == IWN_GPIO_CLOCK) 1025 break; 1026 DELAY(10); 1027 } 1028 if (ntries == 1000) 1029 device_printf(sc->sc_dev, 1030 "%s: could not lock memory\n", __func__); 1031 } 1032 1033 /* 1034 * Release lock on NIC memory. 1035 */ 1036 void 1037 iwn_mem_unlock(struct iwn_softc *sc) 1038 { 1039 uint32_t tmp = IWN_READ(sc, IWN_GPIO_CTL); 1040 IWN_WRITE(sc, IWN_GPIO_CTL, tmp & ~IWN_GPIO_MAC); 1041 } 1042 1043 uint32_t 1044 iwn_mem_read(struct iwn_softc *sc, uint32_t addr) 1045 { 1046 IWN_WRITE(sc, IWN_READ_MEM_ADDR, IWN_MEM_4 | addr); 1047 return IWN_READ(sc, IWN_READ_MEM_DATA); 1048 } 1049 1050 void 1051 iwn_mem_write(struct iwn_softc *sc, uint32_t addr, uint32_t data) 1052 { 1053 IWN_WRITE(sc, IWN_WRITE_MEM_ADDR, IWN_MEM_4 | addr); 1054 IWN_WRITE(sc, IWN_WRITE_MEM_DATA, data); 1055 } 1056 1057 void 1058 iwn_mem_write_region_4(struct iwn_softc *sc, uint32_t addr, 1059 const uint32_t *data, int wlen) 1060 { 1061 for (; wlen > 0; wlen--, data++, addr += 4) 1062 iwn_mem_write(sc, addr, *data); 1063 } 1064 1065 int 1066 iwn_eeprom_lock(struct iwn_softc *sc) 1067 { 1068 uint32_t tmp; 1069 int ntries; 1070 1071 tmp = IWN_READ(sc, IWN_HWCONFIG); 1072 IWN_WRITE(sc, IWN_HWCONFIG, tmp | IWN_HW_EEPROM_LOCKED); 1073 1074 /* spin until we actually get the lock */ 1075 for (ntries = 0; ntries < 100; ntries++) { 1076 if (IWN_READ(sc, IWN_HWCONFIG) & IWN_HW_EEPROM_LOCKED) 1077 return 0; 1078 DELAY(10); 1079 } 1080 return ETIMEDOUT; 1081 } 1082 1083 void 1084 iwn_eeprom_unlock(struct iwn_softc *sc) 1085 { 1086 uint32_t tmp = IWN_READ(sc, IWN_HWCONFIG); 1087 IWN_WRITE(sc, IWN_HWCONFIG, tmp & ~IWN_HW_EEPROM_LOCKED); 1088 } 1089 1090 /* 1091 * Read `len' bytes from the EEPROM. We access the EEPROM through the MAC 1092 * instead of using the traditional bit-bang method. 1093 */ 1094 int 1095 iwn_read_prom_data(struct iwn_softc *sc, uint32_t addr, void *data, int len) 1096 { 1097 uint8_t *out = data; 1098 uint32_t val; 1099 int ntries, tmp; 1100 1101 iwn_mem_lock(sc); 1102 for (; len > 0; len -= 2, addr++) { 1103 IWN_WRITE(sc, IWN_EEPROM_CTL, addr << 2); 1104 tmp = IWN_READ(sc, IWN_EEPROM_CTL); 1105 IWN_WRITE(sc, IWN_EEPROM_CTL, tmp & ~IWN_EEPROM_MSK ); 1106 1107 for (ntries = 0; ntries < 10; ntries++) { 1108 if ((val = IWN_READ(sc, IWN_EEPROM_CTL)) & 1109 IWN_EEPROM_READY) 1110 break; 1111 DELAY(5); 1112 } 1113 if (ntries == 10) { 1114 device_printf(sc->sc_dev,"could not read EEPROM\n"); 1115 return ETIMEDOUT; 1116 } 1117 *out++ = val >> 16; 1118 if (len > 1) 1119 *out++ = val >> 24; 1120 } 1121 iwn_mem_unlock(sc); 1122 1123 return 0; 1124 } 1125 1126 /* 1127 * The firmware boot code is small and is intended to be copied directly into 1128 * the NIC internal memory. 1129 */ 1130 int 1131 iwn_transfer_microcode(struct iwn_softc *sc, const uint8_t *ucode, int size) 1132 { 1133 int ntries; 1134 1135 size /= sizeof (uint32_t); 1136 1137 iwn_mem_lock(sc); 1138 1139 /* copy microcode image into NIC memory */ 1140 iwn_mem_write_region_4(sc, IWN_MEM_UCODE_BASE, 1141 (const uint32_t *)ucode, size); 1142 1143 iwn_mem_write(sc, IWN_MEM_UCODE_SRC, 0); 1144 iwn_mem_write(sc, IWN_MEM_UCODE_DST, IWN_FW_TEXT); 1145 iwn_mem_write(sc, IWN_MEM_UCODE_SIZE, size); 1146 1147 /* run microcode */ 1148 iwn_mem_write(sc, IWN_MEM_UCODE_CTL, IWN_UC_RUN); 1149 1150 /* wait for transfer to complete */ 1151 for (ntries = 0; ntries < 1000; ntries++) { 1152 if (!(iwn_mem_read(sc, IWN_MEM_UCODE_CTL) & IWN_UC_RUN)) 1153 break; 1154 DELAY(10); 1155 } 1156 if (ntries == 1000) { 1157 iwn_mem_unlock(sc); 1158 device_printf(sc->sc_dev, 1159 "%s: could not load boot firmware\n", __func__); 1160 return ETIMEDOUT; 1161 } 1162 iwn_mem_write(sc, IWN_MEM_UCODE_CTL, IWN_UC_ENABLE); 1163 1164 iwn_mem_unlock(sc); 1165 1166 return 0; 1167 } 1168 1169 int 1170 iwn_load_firmware(struct iwn_softc *sc) 1171 { 1172 int error; 1173 1174 KASSERT(sc->fw_fp == NULL, ("firmware already loaded")); 1175 1176 IWN_UNLOCK(sc); 1177 /* load firmware image from disk */ 1178 sc->fw_fp = firmware_get("iwnfw"); 1179 if (sc->fw_fp == NULL) { 1180 device_printf(sc->sc_dev, 1181 "%s: could not load firmare image \"iwnfw\"\n", __func__); 1182 error = EINVAL; 1183 } else 1184 error = 0; 1185 IWN_LOCK(sc); 1186 return error; 1187 } 1188 1189 int 1190 iwn_transfer_firmware(struct iwn_softc *sc) 1191 { 1192 struct iwn_dma_info *dma = &sc->fw_dma; 1193 const struct iwn_firmware_hdr *hdr; 1194 const uint8_t *init_text, *init_data, *main_text, *main_data; 1195 const uint8_t *boot_text; 1196 uint32_t init_textsz, init_datasz, main_textsz, main_datasz; 1197 uint32_t boot_textsz; 1198 int error = 0; 1199 const struct firmware *fp = sc->fw_fp; 1200 1201 /* extract firmware header information */ 1202 if (fp->datasize < sizeof (struct iwn_firmware_hdr)) { 1203 device_printf(sc->sc_dev, 1204 "%s: truncated firmware header: %zu bytes, expecting %zu\n", 1205 __func__, fp->datasize, sizeof (struct iwn_firmware_hdr)); 1206 error = EINVAL; 1207 goto fail; 1208 } 1209 hdr = (const struct iwn_firmware_hdr *)fp->data; 1210 main_textsz = le32toh(hdr->main_textsz); 1211 main_datasz = le32toh(hdr->main_datasz); 1212 init_textsz = le32toh(hdr->init_textsz); 1213 init_datasz = le32toh(hdr->init_datasz); 1214 boot_textsz = le32toh(hdr->boot_textsz); 1215 1216 /* sanity-check firmware segments sizes */ 1217 if (main_textsz > IWN_FW_MAIN_TEXT_MAXSZ || 1218 main_datasz > IWN_FW_MAIN_DATA_MAXSZ || 1219 init_textsz > IWN_FW_INIT_TEXT_MAXSZ || 1220 init_datasz > IWN_FW_INIT_DATA_MAXSZ || 1221 boot_textsz > IWN_FW_BOOT_TEXT_MAXSZ || 1222 (boot_textsz & 3) != 0) { 1223 device_printf(sc->sc_dev, 1224 "%s: invalid firmware header, main [%d,%d], init [%d,%d] " 1225 "boot %d\n", __func__, main_textsz, main_datasz, 1226 init_textsz, init_datasz, boot_textsz); 1227 error = EINVAL; 1228 goto fail; 1229 } 1230 1231 /* check that all firmware segments are present */ 1232 if (fp->datasize < sizeof (struct iwn_firmware_hdr) + main_textsz + 1233 main_datasz + init_textsz + init_datasz + boot_textsz) { 1234 device_printf(sc->sc_dev, "%s: firmware file too short: " 1235 "%zu bytes, main [%d, %d], init [%d,%d] boot %d\n", 1236 __func__, fp->datasize, main_textsz, main_datasz, 1237 init_textsz, init_datasz, boot_textsz); 1238 error = EINVAL; 1239 goto fail; 1240 } 1241 1242 /* get pointers to firmware segments */ 1243 main_text = (const uint8_t *)(hdr + 1); 1244 main_data = main_text + main_textsz; 1245 init_text = main_data + main_datasz; 1246 init_data = init_text + init_textsz; 1247 boot_text = init_data + init_datasz; 1248 1249 /* copy initialization images into pre-allocated DMA-safe memory */ 1250 memcpy(dma->vaddr, init_data, init_datasz); 1251 memcpy(dma->vaddr + IWN_FW_INIT_DATA_MAXSZ, init_text, init_textsz); 1252 1253 /* tell adapter where to find initialization images */ 1254 iwn_mem_lock(sc); 1255 iwn_mem_write(sc, IWN_MEM_DATA_BASE, dma->paddr >> 4); 1256 iwn_mem_write(sc, IWN_MEM_DATA_SIZE, init_datasz); 1257 iwn_mem_write(sc, IWN_MEM_TEXT_BASE, 1258 (dma->paddr + IWN_FW_INIT_DATA_MAXSZ) >> 4); 1259 iwn_mem_write(sc, IWN_MEM_TEXT_SIZE, init_textsz); 1260 iwn_mem_unlock(sc); 1261 1262 /* load firmware boot code */ 1263 error = iwn_transfer_microcode(sc, boot_text, boot_textsz); 1264 if (error != 0) { 1265 device_printf(sc->sc_dev, 1266 "%s: could not load boot firmware, error %d\n", 1267 __func__, error); 1268 goto fail; 1269 } 1270 1271 /* now press "execute" ;-) */ 1272 IWN_WRITE(sc, IWN_RESET, 0); 1273 1274 /* wait at most one second for first alive notification */ 1275 error = msleep(sc, &sc->sc_mtx, PCATCH, "iwninit", hz); 1276 if (error != 0) { 1277 /* this isn't what was supposed to happen.. */ 1278 device_printf(sc->sc_dev, 1279 "%s: timeout waiting for first alive notice, error %d\n", 1280 __func__, error); 1281 goto fail; 1282 } 1283 1284 /* copy runtime images into pre-allocated DMA-safe memory */ 1285 memcpy(dma->vaddr, main_data, main_datasz); 1286 memcpy(dma->vaddr + IWN_FW_MAIN_DATA_MAXSZ, main_text, main_textsz); 1287 1288 /* tell adapter where to find runtime images */ 1289 iwn_mem_lock(sc); 1290 iwn_mem_write(sc, IWN_MEM_DATA_BASE, dma->paddr >> 4); 1291 iwn_mem_write(sc, IWN_MEM_DATA_SIZE, main_datasz); 1292 iwn_mem_write(sc, IWN_MEM_TEXT_BASE, 1293 (dma->paddr + IWN_FW_MAIN_DATA_MAXSZ) >> 4); 1294 iwn_mem_write(sc, IWN_MEM_TEXT_SIZE, IWN_FW_UPDATED | main_textsz); 1295 iwn_mem_unlock(sc); 1296 1297 /* wait at most one second for second alive notification */ 1298 error = msleep(sc, &sc->sc_mtx, PCATCH, "iwninit", hz); 1299 if (error != 0) { 1300 /* this isn't what was supposed to happen.. */ 1301 device_printf(sc->sc_dev, 1302 "%s: timeout waiting for second alive notice, error %d\n", 1303 __func__, error); 1304 goto fail; 1305 } 1306 return 0; 1307 fail: 1308 return error; 1309 } 1310 1311 void 1312 iwn_unload_firmware(struct iwn_softc *sc) 1313 { 1314 if (sc->fw_fp != NULL) { 1315 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD); 1316 sc->fw_fp = NULL; 1317 } 1318 } 1319 1320 static void 1321 iwn_timer_timeout(void *arg) 1322 { 1323 struct iwn_softc *sc = arg; 1324 1325 IWN_LOCK_ASSERT(sc); 1326 1327 if (sc->calib_cnt && --sc->calib_cnt == 0) { 1328 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s\n", 1329 "send statistics request"); 1330 (void) iwn_cmd(sc, IWN_CMD_GET_STATISTICS, NULL, 0, 1); 1331 sc->calib_cnt = 60; /* do calibration every 60s */ 1332 } 1333 iwn_watchdog(sc); /* NB: piggyback tx watchdog */ 1334 callout_reset(&sc->sc_timer_to, hz, iwn_timer_timeout, sc); 1335 } 1336 1337 static void 1338 iwn_calib_reset(struct iwn_softc *sc) 1339 { 1340 callout_reset(&sc->sc_timer_to, hz, iwn_timer_timeout, sc); 1341 sc->calib_cnt = 60; /* do calibration every 60s */ 1342 } 1343 1344 void 1345 iwn_ampdu_rx_start(struct iwn_softc *sc, struct iwn_rx_desc *desc) 1346 { 1347 struct iwn_rx_stat *stat; 1348 1349 DPRINTF(sc, IWN_DEBUG_RECV, "%s\n", "received AMPDU stats"); 1350 /* save Rx statistics, they will be used on IWN_AMPDU_RX_DONE */ 1351 stat = (struct iwn_rx_stat *)(desc + 1); 1352 memcpy(&sc->last_rx_stat, stat, sizeof (*stat)); 1353 sc->last_rx_valid = 1; 1354 } 1355 1356 static __inline int 1357 maprate(int iwnrate) 1358 { 1359 switch (iwnrate) { 1360 /* CCK rates */ 1361 case 10: return 2; 1362 case 20: return 4; 1363 case 55: return 11; 1364 case 110: return 22; 1365 /* OFDM rates */ 1366 case 0xd: return 12; 1367 case 0xf: return 18; 1368 case 0x5: return 24; 1369 case 0x7: return 36; 1370 case 0x9: return 48; 1371 case 0xb: return 72; 1372 case 0x1: return 96; 1373 case 0x3: return 108; 1374 /* XXX MCS */ 1375 } 1376 /* unknown rate: should not happen */ 1377 return 0; 1378 } 1379 1380 void 1381 iwn_rx_intr(struct iwn_softc *sc, struct iwn_rx_desc *desc, 1382 struct iwn_rx_data *data) 1383 { 1384 struct ifnet *ifp = sc->sc_ifp; 1385 struct ieee80211com *ic = ifp->if_l2com; 1386 struct iwn_rx_ring *ring = &sc->rxq; 1387 struct ieee80211_frame *wh; 1388 struct ieee80211_node *ni; 1389 struct mbuf *m, *mnew; 1390 struct iwn_rx_stat *stat; 1391 caddr_t head; 1392 uint32_t *tail; 1393 int8_t rssi, nf; 1394 int len, error; 1395 bus_addr_t paddr; 1396 1397 if (desc->type == IWN_AMPDU_RX_DONE) { 1398 /* check for prior AMPDU_RX_START */ 1399 if (!sc->last_rx_valid) { 1400 DPRINTF(sc, IWN_DEBUG_ANY, 1401 "%s: missing AMPDU_RX_START\n", __func__); 1402 ifp->if_ierrors++; 1403 return; 1404 } 1405 sc->last_rx_valid = 0; 1406 stat = &sc->last_rx_stat; 1407 } else 1408 stat = (struct iwn_rx_stat *)(desc + 1); 1409 1410 if (stat->cfg_phy_len > IWN_STAT_MAXLEN) { 1411 device_printf(sc->sc_dev, 1412 "%s: invalid rx statistic header, len %d\n", 1413 __func__, stat->cfg_phy_len); 1414 ifp->if_ierrors++; 1415 return; 1416 } 1417 if (desc->type == IWN_AMPDU_RX_DONE) { 1418 struct iwn_rx_ampdu *ampdu = (struct iwn_rx_ampdu *)(desc + 1); 1419 head = (caddr_t)(ampdu + 1); 1420 len = le16toh(ampdu->len); 1421 } else { 1422 head = (caddr_t)(stat + 1) + stat->cfg_phy_len; 1423 len = le16toh(stat->len); 1424 } 1425 1426 /* discard Rx frames with bad CRC early */ 1427 tail = (uint32_t *)(head + len); 1428 if ((le32toh(*tail) & IWN_RX_NOERROR) != IWN_RX_NOERROR) { 1429 DPRINTF(sc, IWN_DEBUG_RECV, "%s: rx flags error %x\n", 1430 __func__, le32toh(*tail)); 1431 ifp->if_ierrors++; 1432 return; 1433 } 1434 if (len < sizeof (struct ieee80211_frame)) { 1435 DPRINTF(sc, IWN_DEBUG_RECV, "%s: frame too short: %d\n", 1436 __func__, len); 1437 ic->ic_stats.is_rx_tooshort++; 1438 ifp->if_ierrors++; 1439 return; 1440 } 1441 1442 /* XXX don't need mbuf, just dma buffer */ 1443 mnew = m_getjcl(M_DONTWAIT, MT_DATA, M_PKTHDR, MJUMPAGESIZE); 1444 if (mnew == NULL) { 1445 DPRINTF(sc, IWN_DEBUG_ANY, "%s: no mbuf to restock ring\n", 1446 __func__); 1447 ic->ic_stats.is_rx_nobuf++; 1448 ifp->if_ierrors++; 1449 return; 1450 } 1451 error = bus_dmamap_load(ring->data_dmat, data->map, 1452 mtod(mnew, caddr_t), MJUMPAGESIZE, 1453 iwn_dma_map_addr, &paddr, BUS_DMA_NOWAIT); 1454 if (error != 0 && error != EFBIG) { 1455 device_printf(sc->sc_dev, 1456 "%s: bus_dmamap_load failed, error %d\n", __func__, error); 1457 m_freem(mnew); 1458 ic->ic_stats.is_rx_nobuf++; /* XXX need stat */ 1459 ifp->if_ierrors++; 1460 return; 1461 } 1462 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1463 1464 /* finalize mbuf and swap in new one */ 1465 m = data->m; 1466 m->m_pkthdr.rcvif = ifp; 1467 m->m_data = head; 1468 m->m_pkthdr.len = m->m_len = len; 1469 1470 data->m = mnew; 1471 /* update Rx descriptor */ 1472 ring->desc[ring->cur] = htole32(paddr >> 8); 1473 1474 rssi = iwn_get_rssi(sc, stat); 1475 1476 /* grab a reference to the source node */ 1477 wh = mtod(m, struct ieee80211_frame *); 1478 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 1479 1480 nf = (ni != NULL && ni->ni_vap->iv_state == IEEE80211_S_RUN && 1481 (ic->ic_flags & IEEE80211_F_SCAN) == 0) ? sc->noise : -95; 1482 1483 if (bpf_peers_present(ifp->if_bpf)) { 1484 struct iwn_rx_radiotap_header *tap = &sc->sc_rxtap; 1485 1486 tap->wr_flags = 0; 1487 tap->wr_dbm_antsignal = rssi; 1488 tap->wr_dbm_antnoise = nf; 1489 tap->wr_rate = maprate(stat->rate); 1490 tap->wr_tsft = htole64(stat->tstamp); 1491 1492 if (stat->flags & htole16(IWN_CONFIG_SHPREAMBLE)) 1493 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 1494 1495 bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m); 1496 } 1497 1498 IWN_UNLOCK(sc); 1499 1500 /* send the frame to the 802.11 layer */ 1501 if (ni != NULL) { 1502 (void) ieee80211_input(ni, m, rssi - nf, nf, 0); 1503 ieee80211_free_node(ni); 1504 } else 1505 (void) ieee80211_input_all(ic, m, rssi - nf, nf, 0); 1506 1507 IWN_LOCK(sc); 1508 } 1509 1510 void 1511 iwn_rx_statistics(struct iwn_softc *sc, struct iwn_rx_desc *desc) 1512 { 1513 struct ifnet *ifp = sc->sc_ifp; 1514 struct ieee80211com *ic = ifp->if_l2com; 1515 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1516 struct iwn_calib_state *calib = &sc->calib; 1517 struct iwn_stats *stats = (struct iwn_stats *)(desc + 1); 1518 1519 /* beacon stats are meaningful only when associated and not scanning */ 1520 if (vap->iv_state != IEEE80211_S_RUN || 1521 (ic->ic_flags & IEEE80211_F_SCAN)) 1522 return; 1523 1524 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: cmd %d\n", __func__, desc->type); 1525 iwn_calib_reset(sc); 1526 1527 /* test if temperature has changed */ 1528 if (stats->general.temp != sc->rawtemp) { 1529 int temp; 1530 1531 sc->rawtemp = stats->general.temp; 1532 temp = iwn_get_temperature(sc); 1533 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: temperature %d\n", 1534 __func__, temp); 1535 1536 /* update Tx power if need be */ 1537 iwn_power_calibration(sc, temp); 1538 } 1539 1540 if (desc->type != IWN_BEACON_STATISTICS) 1541 return; /* reply to a statistics request */ 1542 1543 sc->noise = iwn_get_noise(&stats->rx.general); 1544 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: noise %d\n", __func__, sc->noise); 1545 1546 /* test that RSSI and noise are present in stats report */ 1547 if (stats->rx.general.flags != htole32(1)) { 1548 DPRINTF(sc, IWN_DEBUG_ANY, "%s\n", 1549 "received statistics without RSSI"); 1550 return; 1551 } 1552 1553 if (calib->state == IWN_CALIB_STATE_ASSOC) 1554 iwn_compute_differential_gain(sc, &stats->rx.general); 1555 else if (calib->state == IWN_CALIB_STATE_RUN) 1556 iwn_tune_sensitivity(sc, &stats->rx); 1557 } 1558 1559 void 1560 iwn_tx_intr(struct iwn_softc *sc, struct iwn_rx_desc *desc) 1561 { 1562 struct ifnet *ifp = sc->sc_ifp; 1563 struct iwn_tx_ring *ring = &sc->txq[desc->qid & 0xf]; 1564 struct iwn_tx_data *data = &ring->data[desc->idx]; 1565 struct iwn_tx_stat *stat = (struct iwn_tx_stat *)(desc + 1); 1566 struct iwn_node *wn = IWN_NODE(data->ni); 1567 struct mbuf *m; 1568 struct ieee80211_node *ni; 1569 uint32_t status; 1570 1571 KASSERT(data->ni != NULL, ("no node")); 1572 1573 DPRINTF(sc, IWN_DEBUG_XMIT, "%s: " 1574 "qid %d idx %d retries %d nkill %d rate %x duration %d status %x\n", 1575 __func__, desc->qid, desc->idx, stat->ntries, 1576 stat->nkill, stat->rate, le16toh(stat->duration), 1577 le32toh(stat->status)); 1578 1579 /* 1580 * Update rate control statistics for the node. 1581 */ 1582 status = le32toh(stat->status) & 0xff; 1583 if (status & 0x80) { 1584 DPRINTF(sc, IWN_DEBUG_ANY, "%s: status 0x%x\n", 1585 __func__, le32toh(stat->status)); 1586 ifp->if_oerrors++; 1587 ieee80211_amrr_tx_complete(&wn->amn, 1588 IEEE80211_AMRR_FAILURE, stat->ntries); 1589 } else { 1590 ieee80211_amrr_tx_complete(&wn->amn, 1591 IEEE80211_AMRR_SUCCESS, stat->ntries); 1592 } 1593 1594 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_POSTWRITE); 1595 bus_dmamap_unload(ring->data_dmat, data->map); 1596 1597 m = data->m, data->m = NULL; 1598 ni = data->ni, data->ni = NULL; 1599 1600 if (m->m_flags & M_TXCB) { 1601 /* 1602 * Channels marked for "radar" require traffic to be received 1603 * to unlock before we can transmit. Until traffic is seen 1604 * any attempt to transmit is returned immediately with status 1605 * set to IWN_TX_FAIL_TX_LOCKED. Unfortunately this can easily 1606 * happen on first authenticate after scanning. To workaround 1607 * this we ignore a failure of this sort in AUTH state so the 1608 * 802.11 layer will fall back to using a timeout to wait for 1609 * the AUTH reply. This allows the firmware time to see 1610 * traffic so a subsequent retry of AUTH succeeds. It's 1611 * unclear why the firmware does not maintain state for 1612 * channels recently visited as this would allow immediate 1613 * use of the channel after a scan (where we see traffic). 1614 */ 1615 if (status == IWN_TX_FAIL_TX_LOCKED && 1616 ni->ni_vap->iv_state == IEEE80211_S_AUTH) 1617 ieee80211_process_callback(ni, m, 0); 1618 else 1619 ieee80211_process_callback(ni, m, 1620 (status & IWN_TX_FAIL) != 0); 1621 } 1622 m_freem(m); 1623 ieee80211_free_node(ni); 1624 1625 ring->queued--; 1626 1627 sc->sc_tx_timer = 0; 1628 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1629 iwn_start(ifp); 1630 } 1631 1632 void 1633 iwn_cmd_intr(struct iwn_softc *sc, struct iwn_rx_desc *desc) 1634 { 1635 struct iwn_tx_ring *ring = &sc->txq[4]; 1636 struct iwn_tx_data *data; 1637 1638 if ((desc->qid & 0xf) != 4) 1639 return; /* not a command ack */ 1640 1641 data = &ring->data[desc->idx]; 1642 1643 /* if the command was mapped in a mbuf, free it */ 1644 if (data->m != NULL) { 1645 bus_dmamap_unload(ring->data_dmat, data->map); 1646 m_freem(data->m); 1647 data->m = NULL; 1648 } 1649 1650 wakeup(&ring->cmd[desc->idx]); 1651 } 1652 1653 static void 1654 iwn_bmiss(void *arg, int npending) 1655 { 1656 struct iwn_softc *sc = arg; 1657 struct ieee80211com *ic = sc->sc_ifp->if_l2com; 1658 1659 ieee80211_beacon_miss(ic); 1660 } 1661 1662 void 1663 iwn_notif_intr(struct iwn_softc *sc) 1664 { 1665 struct ifnet *ifp = sc->sc_ifp; 1666 struct ieee80211com *ic = ifp->if_l2com; 1667 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1668 uint16_t hw; 1669 1670 hw = le16toh(sc->shared->closed_count) & 0xfff; 1671 while (sc->rxq.cur != hw) { 1672 struct iwn_rx_data *data = &sc->rxq.data[sc->rxq.cur]; 1673 struct iwn_rx_desc *desc = (void *)data->m->m_ext.ext_buf; 1674 1675 DPRINTF(sc, IWN_DEBUG_RECV, 1676 "%s: qid %x idx %d flags %x type %d(%s) len %d\n", 1677 __func__, desc->qid, desc->idx, desc->flags, 1678 desc->type, iwn_intr_str(desc->type), 1679 le16toh(desc->len)); 1680 1681 if (!(desc->qid & 0x80)) /* reply to a command */ 1682 iwn_cmd_intr(sc, desc); 1683 1684 switch (desc->type) { 1685 case IWN_RX_DONE: 1686 case IWN_AMPDU_RX_DONE: 1687 iwn_rx_intr(sc, desc, data); 1688 break; 1689 1690 case IWN_AMPDU_RX_START: 1691 iwn_ampdu_rx_start(sc, desc); 1692 break; 1693 1694 case IWN_TX_DONE: 1695 /* a 802.11 frame has been transmitted */ 1696 iwn_tx_intr(sc, desc); 1697 break; 1698 1699 case IWN_RX_STATISTICS: 1700 case IWN_BEACON_STATISTICS: 1701 iwn_rx_statistics(sc, desc); 1702 break; 1703 1704 case IWN_BEACON_MISSED: { 1705 struct iwn_beacon_missed *miss = 1706 (struct iwn_beacon_missed *)(desc + 1); 1707 int misses = le32toh(miss->consecutive); 1708 1709 /* XXX not sure why we're notified w/ zero */ 1710 if (misses == 0) 1711 break; 1712 DPRINTF(sc, IWN_DEBUG_STATE, 1713 "%s: beacons missed %d/%d\n", __func__, 1714 misses, le32toh(miss->total)); 1715 /* 1716 * If more than 5 consecutive beacons are missed, 1717 * reinitialize the sensitivity state machine. 1718 */ 1719 if (vap->iv_state == IEEE80211_S_RUN && misses > 5) 1720 (void) iwn_init_sensitivity(sc); 1721 if (misses >= vap->iv_bmissthreshold) 1722 taskqueue_enqueue(taskqueue_swi, 1723 &sc->sc_bmiss_task); 1724 break; 1725 } 1726 case IWN_UC_READY: { 1727 struct iwn_ucode_info *uc = 1728 (struct iwn_ucode_info *)(desc + 1); 1729 1730 /* the microcontroller is ready */ 1731 DPRINTF(sc, IWN_DEBUG_RESET, 1732 "microcode alive notification version=%d.%d " 1733 "subtype=%x alive=%x\n", uc->major, uc->minor, 1734 uc->subtype, le32toh(uc->valid)); 1735 1736 if (le32toh(uc->valid) != 1) { 1737 device_printf(sc->sc_dev, 1738 "microcontroller initialization failed"); 1739 break; 1740 } 1741 if (uc->subtype == IWN_UCODE_INIT) { 1742 /* save microcontroller's report */ 1743 memcpy(&sc->ucode_info, uc, sizeof (*uc)); 1744 } 1745 break; 1746 } 1747 case IWN_STATE_CHANGED: { 1748 uint32_t *status = (uint32_t *)(desc + 1); 1749 1750 /* 1751 * State change allows hardware switch change to be 1752 * noted. However, we handle this in iwn_intr as we 1753 * get both the enable/disble intr. 1754 */ 1755 DPRINTF(sc, IWN_DEBUG_INTR, "state changed to %x\n", 1756 le32toh(*status)); 1757 break; 1758 } 1759 case IWN_START_SCAN: { 1760 struct iwn_start_scan *scan = 1761 (struct iwn_start_scan *)(desc + 1); 1762 1763 DPRINTF(sc, IWN_DEBUG_ANY, 1764 "%s: scanning channel %d status %x\n", 1765 __func__, scan->chan, le32toh(scan->status)); 1766 break; 1767 } 1768 case IWN_STOP_SCAN: { 1769 struct iwn_stop_scan *scan = 1770 (struct iwn_stop_scan *)(desc + 1); 1771 1772 DPRINTF(sc, IWN_DEBUG_STATE, 1773 "scan finished nchan=%d status=%d chan=%d\n", 1774 scan->nchan, scan->status, scan->chan); 1775 1776 iwn_queue_cmd(sc, IWN_SCAN_NEXT, 0, IWN_QUEUE_NORMAL); 1777 break; 1778 } 1779 } 1780 sc->rxq.cur = (sc->rxq.cur + 1) % IWN_RX_RING_COUNT; 1781 } 1782 1783 /* tell the firmware what we have processed */ 1784 hw = (hw == 0) ? IWN_RX_RING_COUNT - 1 : hw - 1; 1785 IWN_WRITE(sc, IWN_RX_WIDX, hw & ~7); 1786 } 1787 1788 void 1789 iwn_intr(void *arg) 1790 { 1791 struct iwn_softc *sc = arg; 1792 uint32_t r1, r2; 1793 1794 IWN_LOCK(sc); 1795 1796 /* disable interrupts */ 1797 IWN_WRITE(sc, IWN_MASK, 0); 1798 1799 r1 = IWN_READ(sc, IWN_INTR); 1800 r2 = IWN_READ(sc, IWN_INTR_STATUS); 1801 1802 if (r1 == 0 && r2 == 0) { 1803 IWN_WRITE(sc, IWN_MASK, IWN_INTR_MASK); 1804 goto done; /* not for us */ 1805 } 1806 1807 if (r1 == 0xffffffff) 1808 goto done; /* hardware gone */ 1809 1810 /* ack interrupts */ 1811 IWN_WRITE(sc, IWN_INTR, r1); 1812 IWN_WRITE(sc, IWN_INTR_STATUS, r2); 1813 1814 DPRINTF(sc, IWN_DEBUG_INTR, "interrupt reg1=%x reg2=%x\n", r1, r2); 1815 1816 if (r1 & IWN_RF_TOGGLED) { 1817 uint32_t tmp = IWN_READ(sc, IWN_GPIO_CTL); 1818 device_printf(sc->sc_dev, "RF switch: radio %s\n", 1819 (tmp & IWN_GPIO_RF_ENABLED) ? "enabled" : "disabled"); 1820 if (tmp & IWN_GPIO_RF_ENABLED) 1821 iwn_queue_cmd(sc, IWN_RADIO_ENABLE, 0, IWN_QUEUE_CLEAR); 1822 else 1823 iwn_queue_cmd(sc, IWN_RADIO_DISABLE, 0, IWN_QUEUE_CLEAR); 1824 } 1825 if (r1 & IWN_CT_REACHED) 1826 device_printf(sc->sc_dev, "critical temperature reached!\n"); 1827 if (r1 & (IWN_SW_ERROR | IWN_HW_ERROR)) { 1828 device_printf(sc->sc_dev, "error, INTR=%b STATUS=0x%x\n", 1829 r1, IWN_INTR_BITS, r2); 1830 iwn_queue_cmd(sc, IWN_REINIT, 0, IWN_QUEUE_CLEAR); 1831 goto done; 1832 } 1833 if ((r1 & (IWN_RX_INTR | IWN_SW_RX_INTR)) || (r2 & IWN_RX_STATUS_INTR)) 1834 iwn_notif_intr(sc); 1835 if (r1 & IWN_ALIVE_INTR) 1836 wakeup(sc); 1837 1838 /* re-enable interrupts */ 1839 IWN_WRITE(sc, IWN_MASK, IWN_INTR_MASK); 1840 done: 1841 IWN_UNLOCK(sc); 1842 } 1843 1844 uint8_t 1845 iwn_plcp_signal(int rate) 1846 { 1847 switch (rate) { 1848 /* CCK rates (returned values are device-dependent) */ 1849 case 2: return 10; 1850 case 4: return 20; 1851 case 11: return 55; 1852 case 22: return 110; 1853 1854 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1855 /* R1-R4, (u)ral is R4-R1 */ 1856 case 12: return 0xd; 1857 case 18: return 0xf; 1858 case 24: return 0x5; 1859 case 36: return 0x7; 1860 case 48: return 0x9; 1861 case 72: return 0xb; 1862 case 96: return 0x1; 1863 case 108: return 0x3; 1864 case 120: return 0x3; 1865 } 1866 /* unknown rate (should not get there) */ 1867 return 0; 1868 } 1869 1870 /* determine if a given rate is CCK or OFDM */ 1871 #define IWN_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22) 1872 1873 int 1874 iwn_tx_data(struct iwn_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1875 struct iwn_tx_ring *ring) 1876 { 1877 struct ieee80211vap *vap = ni->ni_vap; 1878 struct ieee80211com *ic = ni->ni_ic; 1879 struct ifnet *ifp = sc->sc_ifp; 1880 const struct ieee80211_txparam *tp; 1881 struct iwn_tx_desc *desc; 1882 struct iwn_tx_data *data; 1883 struct iwn_tx_cmd *cmd; 1884 struct iwn_cmd_data *tx; 1885 struct ieee80211_frame *wh; 1886 struct ieee80211_key *k; 1887 bus_addr_t paddr; 1888 uint32_t flags; 1889 uint16_t timeout; 1890 uint8_t type; 1891 u_int hdrlen; 1892 struct mbuf *mnew; 1893 int rate, error, pad, nsegs, i, ismcast, id; 1894 bus_dma_segment_t segs[IWN_MAX_SCATTER]; 1895 1896 IWN_LOCK_ASSERT(sc); 1897 1898 wh = mtod(m0, struct ieee80211_frame *); 1899 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1900 ismcast = IEEE80211_IS_MULTICAST(wh->i_addr1); 1901 hdrlen = ieee80211_anyhdrsize(wh); 1902 1903 /* pick a tx rate */ 1904 /* XXX ni_chan */ 1905 tp = &vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)]; 1906 if (type == IEEE80211_FC0_TYPE_MGT) 1907 rate = tp->mgmtrate; 1908 else if (ismcast) 1909 rate = tp->mcastrate; 1910 else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) 1911 rate = tp->ucastrate; 1912 else { 1913 (void) ieee80211_amrr_choose(ni, &IWN_NODE(ni)->amn); 1914 rate = ni->ni_txrate; 1915 } 1916 1917 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1918 k = ieee80211_crypto_encap(ni, m0); 1919 if (k == NULL) { 1920 m_freem(m0); 1921 return ENOBUFS; 1922 } 1923 /* packet header may have moved, reset our local pointer */ 1924 wh = mtod(m0, struct ieee80211_frame *); 1925 } else 1926 k = NULL; 1927 1928 if (bpf_peers_present(ifp->if_bpf)) { 1929 struct iwn_tx_radiotap_header *tap = &sc->sc_txtap; 1930 1931 tap->wt_flags = 0; 1932 tap->wt_rate = rate; 1933 if (k != NULL) 1934 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP; 1935 1936 bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0); 1937 } 1938 1939 flags = IWN_TX_AUTO_SEQ; 1940 /* XXX honor ACM */ 1941 if (!ismcast) 1942 flags |= IWN_TX_NEED_ACK; 1943 1944 if (ismcast || type != IEEE80211_FC0_TYPE_DATA) 1945 id = IWN_ID_BROADCAST; 1946 else 1947 id = IWN_ID_BSS; 1948 1949 /* check if RTS/CTS or CTS-to-self protection must be used */ 1950 if (!ismcast) { 1951 /* multicast frames are not sent at OFDM rates in 802.11b/g */ 1952 if (m0->m_pkthdr.len+IEEE80211_CRC_LEN > vap->iv_rtsthreshold) { 1953 flags |= IWN_TX_NEED_RTS | IWN_TX_FULL_TXOP; 1954 } else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1955 IWN_RATE_IS_OFDM(rate)) { 1956 if (ic->ic_protmode == IEEE80211_PROT_CTSONLY) 1957 flags |= IWN_TX_NEED_CTS | IWN_TX_FULL_TXOP; 1958 else if (ic->ic_protmode == IEEE80211_PROT_RTSCTS) 1959 flags |= IWN_TX_NEED_RTS | IWN_TX_FULL_TXOP; 1960 } 1961 } 1962 1963 if (type == IEEE80211_FC0_TYPE_MGT) { 1964 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 1965 1966 /* tell h/w to set timestamp in probe responses */ 1967 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1968 flags |= IWN_TX_INSERT_TSTAMP; 1969 1970 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ || 1971 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) 1972 timeout = htole16(3); 1973 else 1974 timeout = htole16(2); 1975 } else 1976 timeout = htole16(0); 1977 1978 if (hdrlen & 3) { 1979 /* first segment's length must be a multiple of 4 */ 1980 flags |= IWN_TX_NEED_PADDING; 1981 pad = 4 - (hdrlen & 3); 1982 } else 1983 pad = 0; 1984 1985 desc = &ring->desc[ring->cur]; 1986 data = &ring->data[ring->cur]; 1987 1988 cmd = &ring->cmd[ring->cur]; 1989 cmd->code = IWN_CMD_TX_DATA; 1990 cmd->flags = 0; 1991 cmd->qid = ring->qid; 1992 cmd->idx = ring->cur; 1993 1994 tx = (struct iwn_cmd_data *)cmd->data; 1995 /* NB: no need to bzero tx, all fields are reinitialized here */ 1996 tx->id = id; 1997 tx->flags = htole32(flags); 1998 tx->len = htole16(m0->m_pkthdr.len); 1999 tx->rate = iwn_plcp_signal(rate); 2000 tx->rts_ntries = 60; /* XXX? */ 2001 tx->data_ntries = 15; /* XXX? */ 2002 tx->lifetime = htole32(IWN_LIFETIME_INFINITE); 2003 tx->timeout = timeout; 2004 2005 if (k != NULL) { 2006 /* XXX fill in */; 2007 } else 2008 tx->security = 0; 2009 2010 /* XXX alternate between Ant A and Ant B ? */ 2011 tx->rflags = IWN_RFLAG_ANT_B; 2012 if (tx->id == IWN_ID_BROADCAST) { 2013 tx->ridx = IWN_MAX_TX_RETRIES - 1; 2014 if (!IWN_RATE_IS_OFDM(rate)) 2015 tx->rflags |= IWN_RFLAG_CCK; 2016 } else { 2017 tx->ridx = 0; 2018 /* tell adapter to ignore rflags */ 2019 tx->flags |= htole32(IWN_TX_USE_NODE_RATE); 2020 } 2021 2022 /* copy and trim IEEE802.11 header */ 2023 memcpy((uint8_t *)(tx + 1), wh, hdrlen); 2024 m_adj(m0, hdrlen); 2025 2026 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m0, segs, 2027 &nsegs, BUS_DMA_NOWAIT); 2028 if (error != 0) { 2029 if (error == EFBIG) { 2030 /* too many fragments, linearize */ 2031 mnew = m_collapse(m0, M_DONTWAIT, IWN_MAX_SCATTER); 2032 if (mnew == NULL) { 2033 IWN_UNLOCK(sc); 2034 device_printf(sc->sc_dev, 2035 "%s: could not defrag mbuf\n", __func__); 2036 m_freem(m0); 2037 return ENOBUFS; 2038 } 2039 m0 = mnew; 2040 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, 2041 data->map, m0, segs, &nsegs, BUS_DMA_NOWAIT); 2042 } 2043 if (error != 0) { 2044 IWN_UNLOCK(sc); 2045 device_printf(sc->sc_dev, 2046 "%s: bus_dmamap_load_mbuf_sg failed, error %d\n", 2047 __func__, error); 2048 m_freem(m0); 2049 return error; 2050 } 2051 } 2052 2053 data->m = m0; 2054 data->ni = ni; 2055 2056 DPRINTF(sc, IWN_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n", 2057 __func__, ring->qid, ring->cur, m0->m_pkthdr.len, nsegs); 2058 2059 paddr = ring->cmd_dma.paddr + ring->cur * sizeof (struct iwn_tx_cmd); 2060 tx->loaddr = htole32(paddr + 4 + 2061 offsetof(struct iwn_cmd_data, ntries)); 2062 tx->hiaddr = 0; /* limit to 32-bit physical addresses */ 2063 2064 /* first scatter/gather segment is used by the tx data command */ 2065 IWN_SET_DESC_NSEGS(desc, 1 + nsegs); 2066 IWN_SET_DESC_SEG(desc, 0, paddr, 4 + sizeof (*tx) + hdrlen + pad); 2067 for (i = 1; i <= nsegs; i++) { 2068 IWN_SET_DESC_SEG(desc, i, segs[i - 1].ds_addr, 2069 segs[i - 1].ds_len); 2070 } 2071 sc->shared->len[ring->qid][ring->cur] = 2072 htole16(hdrlen + m0->m_pkthdr.len + 8); 2073 2074 if (ring->cur < IWN_TX_WINDOW) 2075 sc->shared->len[ring->qid][ring->cur + IWN_TX_RING_COUNT] = 2076 htole16(hdrlen + m0->m_pkthdr.len + 8); 2077 2078 ring->queued++; 2079 2080 /* kick Tx ring */ 2081 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT; 2082 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur); 2083 2084 ifp->if_opackets++; 2085 sc->sc_tx_timer = 5; 2086 2087 return 0; 2088 } 2089 2090 void 2091 iwn_start(struct ifnet *ifp) 2092 { 2093 struct iwn_softc *sc = ifp->if_softc; 2094 struct ieee80211_node *ni; 2095 struct iwn_tx_ring *txq; 2096 struct mbuf *m; 2097 int pri; 2098 2099 for (;;) { 2100 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 2101 if (m == NULL) 2102 break; 2103 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 2104 pri = M_WME_GETAC(m); 2105 txq = &sc->txq[pri]; 2106 m = ieee80211_encap(ni, m); 2107 if (m == NULL) { 2108 ifp->if_oerrors++; 2109 ieee80211_free_node(ni); 2110 continue; 2111 } 2112 IWN_LOCK(sc); 2113 if (txq->queued >= IWN_TX_RING_COUNT - 8) { 2114 /* XXX not right */ 2115 /* ring is nearly full, stop flow */ 2116 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2117 } 2118 if (iwn_tx_data(sc, m, ni, txq) != 0) { 2119 ifp->if_oerrors++; 2120 ieee80211_free_node(ni); 2121 IWN_UNLOCK(sc); 2122 break; 2123 } 2124 IWN_UNLOCK(sc); 2125 } 2126 } 2127 2128 static int 2129 iwn_tx_handoff(struct iwn_softc *sc, 2130 struct iwn_tx_ring *ring, 2131 struct iwn_tx_cmd *cmd, 2132 struct iwn_cmd_data *tx, 2133 struct ieee80211_node *ni, 2134 struct mbuf *m0, u_int hdrlen, int pad) 2135 { 2136 struct ifnet *ifp = sc->sc_ifp; 2137 struct iwn_tx_desc *desc; 2138 struct iwn_tx_data *data; 2139 bus_addr_t paddr; 2140 struct mbuf *mnew; 2141 int error, nsegs, i; 2142 bus_dma_segment_t segs[IWN_MAX_SCATTER]; 2143 2144 /* copy and trim IEEE802.11 header */ 2145 memcpy((uint8_t *)(tx + 1), mtod(m0, uint8_t *), hdrlen); 2146 m_adj(m0, hdrlen); 2147 2148 desc = &ring->desc[ring->cur]; 2149 data = &ring->data[ring->cur]; 2150 2151 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m0, segs, 2152 &nsegs, BUS_DMA_NOWAIT); 2153 if (error != 0) { 2154 if (error == EFBIG) { 2155 /* too many fragments, linearize */ 2156 mnew = m_collapse(m0, M_DONTWAIT, IWN_MAX_SCATTER); 2157 if (mnew == NULL) { 2158 IWN_UNLOCK(sc); 2159 device_printf(sc->sc_dev, 2160 "%s: could not defrag mbuf\n", __func__); 2161 m_freem(m0); 2162 return ENOBUFS; 2163 } 2164 m0 = mnew; 2165 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, 2166 data->map, m0, segs, &nsegs, BUS_DMA_NOWAIT); 2167 } 2168 if (error != 0) { 2169 IWN_UNLOCK(sc); 2170 device_printf(sc->sc_dev, 2171 "%s: bus_dmamap_load_mbuf_sg failed, error %d\n", 2172 __func__, error); 2173 m_freem(m0); 2174 return error; 2175 } 2176 } 2177 2178 data->m = m0; 2179 data->ni = ni; 2180 2181 DPRINTF(sc, IWN_DEBUG_XMIT, "%s: qid %d idx %d len %d nsegs %d\n", 2182 __func__, ring->qid, ring->cur, m0->m_pkthdr.len, nsegs); 2183 2184 paddr = ring->cmd_dma.paddr + ring->cur * sizeof (struct iwn_tx_cmd); 2185 tx->loaddr = htole32(paddr + 4 + 2186 offsetof(struct iwn_cmd_data, ntries)); 2187 tx->hiaddr = 0; /* limit to 32-bit physical addresses */ 2188 2189 /* first scatter/gather segment is used by the tx data command */ 2190 IWN_SET_DESC_NSEGS(desc, 1 + nsegs); 2191 IWN_SET_DESC_SEG(desc, 0, paddr, 4 + sizeof (*tx) + hdrlen + pad); 2192 for (i = 1; i <= nsegs; i++) { 2193 IWN_SET_DESC_SEG(desc, i, segs[i - 1].ds_addr, 2194 segs[i - 1].ds_len); 2195 } 2196 sc->shared->len[ring->qid][ring->cur] = 2197 htole16(hdrlen + m0->m_pkthdr.len + 8); 2198 2199 if (ring->cur < IWN_TX_WINDOW) 2200 sc->shared->len[ring->qid][ring->cur + IWN_TX_RING_COUNT] = 2201 htole16(hdrlen + m0->m_pkthdr.len + 8); 2202 2203 ring->queued++; 2204 2205 /* kick Tx ring */ 2206 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT; 2207 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur); 2208 2209 ifp->if_opackets++; 2210 sc->sc_tx_timer = 5; 2211 2212 return 0; 2213 } 2214 2215 static int 2216 iwn_tx_data_raw(struct iwn_softc *sc, struct mbuf *m0, 2217 struct ieee80211_node *ni, struct iwn_tx_ring *ring, 2218 const struct ieee80211_bpf_params *params) 2219 { 2220 struct ifnet *ifp = sc->sc_ifp; 2221 struct iwn_tx_cmd *cmd; 2222 struct iwn_cmd_data *tx; 2223 struct ieee80211_frame *wh; 2224 uint32_t flags; 2225 uint8_t type, subtype; 2226 u_int hdrlen; 2227 int rate, pad; 2228 2229 IWN_LOCK_ASSERT(sc); 2230 2231 wh = mtod(m0, struct ieee80211_frame *); 2232 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 2233 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 2234 hdrlen = ieee80211_anyhdrsize(wh); 2235 2236 flags = IWN_TX_AUTO_SEQ; 2237 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 2238 flags |= IWN_TX_NEED_ACK; 2239 if (params->ibp_flags & IEEE80211_BPF_RTS) 2240 flags |= IWN_TX_NEED_RTS | IWN_TX_FULL_TXOP; 2241 if (params->ibp_flags & IEEE80211_BPF_CTS) 2242 flags |= IWN_TX_NEED_CTS | IWN_TX_FULL_TXOP; 2243 if (type == IEEE80211_FC0_TYPE_MGT && 2244 subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) { 2245 /* tell h/w to set timestamp in probe responses */ 2246 flags |= IWN_TX_INSERT_TSTAMP; 2247 } 2248 if (hdrlen & 3) { 2249 /* first segment's length must be a multiple of 4 */ 2250 flags |= IWN_TX_NEED_PADDING; 2251 pad = 4 - (hdrlen & 3); 2252 } else 2253 pad = 0; 2254 2255 /* pick a tx rate */ 2256 rate = params->ibp_rate0; 2257 2258 if (bpf_peers_present(ifp->if_bpf)) { 2259 struct iwn_tx_radiotap_header *tap = &sc->sc_txtap; 2260 2261 tap->wt_flags = 0; 2262 tap->wt_rate = rate; 2263 2264 bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0); 2265 } 2266 2267 cmd = &ring->cmd[ring->cur]; 2268 cmd->code = IWN_CMD_TX_DATA; 2269 cmd->flags = 0; 2270 cmd->qid = ring->qid; 2271 cmd->idx = ring->cur; 2272 2273 tx = (struct iwn_cmd_data *)cmd->data; 2274 /* NB: no need to bzero tx, all fields are reinitialized here */ 2275 tx->id = IWN_ID_BROADCAST; 2276 tx->flags = htole32(flags); 2277 tx->len = htole16(m0->m_pkthdr.len); 2278 tx->rate = iwn_plcp_signal(rate); 2279 tx->rts_ntries = params->ibp_try1; /* XXX? */ 2280 tx->data_ntries = params->ibp_try0; 2281 tx->lifetime = htole32(IWN_LIFETIME_INFINITE); 2282 /* XXX use try count? */ 2283 if (type == IEEE80211_FC0_TYPE_MGT) { 2284 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ || 2285 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) 2286 tx->timeout = htole16(3); 2287 else 2288 tx->timeout = htole16(2); 2289 } else 2290 tx->timeout = htole16(0); 2291 tx->security = 0; 2292 /* XXX alternate between Ant A and Ant B ? */ 2293 tx->rflags = IWN_RFLAG_ANT_B; /* XXX params->ibp_pri >> 2 */ 2294 tx->ridx = IWN_MAX_TX_RETRIES - 1; 2295 if (!IWN_RATE_IS_OFDM(rate)) 2296 tx->rflags |= IWN_RFLAG_CCK; 2297 2298 return iwn_tx_handoff(sc, ring, cmd, tx, ni, m0, hdrlen, pad); 2299 } 2300 2301 static int 2302 iwn_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2303 const struct ieee80211_bpf_params *params) 2304 { 2305 struct ieee80211com *ic = ni->ni_ic; 2306 struct ifnet *ifp = ic->ic_ifp; 2307 struct iwn_softc *sc = ifp->if_softc; 2308 struct iwn_tx_ring *txq; 2309 int error; 2310 2311 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2312 ieee80211_free_node(ni); 2313 m_freem(m); 2314 return ENETDOWN; 2315 } 2316 2317 IWN_LOCK(sc); 2318 if (params == NULL) 2319 txq = &sc->txq[M_WME_GETAC(m)]; 2320 else 2321 txq = &sc->txq[params->ibp_pri & 3]; 2322 if (txq->queued >= IWN_TX_RING_COUNT - 8) { 2323 /* XXX not right */ 2324 /* ring is nearly full, stop flow */ 2325 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2326 } 2327 if (params == NULL) { 2328 /* 2329 * Legacy path; interpret frame contents to decide 2330 * precisely how to send the frame. 2331 */ 2332 error = iwn_tx_data(sc, m, ni, txq); 2333 } else { 2334 /* 2335 * Caller supplied explicit parameters to use in 2336 * sending the frame. 2337 */ 2338 error = iwn_tx_data_raw(sc, m, ni, txq, params); 2339 } 2340 if (error != 0) { 2341 /* NB: m is reclaimed on tx failure */ 2342 ieee80211_free_node(ni); 2343 ifp->if_oerrors++; 2344 } 2345 IWN_UNLOCK(sc); 2346 return error; 2347 } 2348 2349 static void 2350 iwn_watchdog(struct iwn_softc *sc) 2351 { 2352 if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) { 2353 struct ifnet *ifp = sc->sc_ifp; 2354 2355 if_printf(ifp, "device timeout\n"); 2356 iwn_queue_cmd(sc, IWN_REINIT, 0, IWN_QUEUE_CLEAR); 2357 } 2358 } 2359 2360 int 2361 iwn_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2362 { 2363 struct iwn_softc *sc = ifp->if_softc; 2364 struct ieee80211com *ic = ifp->if_l2com; 2365 struct ifreq *ifr = (struct ifreq *) data; 2366 int error = 0, startall = 0; 2367 2368 switch (cmd) { 2369 case SIOCSIFFLAGS: 2370 IWN_LOCK(sc); 2371 if (ifp->if_flags & IFF_UP) { 2372 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2373 iwn_init_locked(sc); 2374 startall = 1; 2375 } 2376 } else { 2377 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2378 iwn_stop_locked(sc); 2379 } 2380 IWN_UNLOCK(sc); 2381 if (startall) 2382 ieee80211_start_all(ic); 2383 break; 2384 case SIOCGIFMEDIA: 2385 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 2386 break; 2387 case SIOCGIFADDR: 2388 error = ether_ioctl(ifp, cmd, data); 2389 break; 2390 default: 2391 error = EINVAL; 2392 break; 2393 } 2394 return error; 2395 } 2396 2397 void 2398 iwn_read_eeprom(struct iwn_softc *sc) 2399 { 2400 struct ifnet *ifp = sc->sc_ifp; 2401 struct ieee80211com *ic = ifp->if_l2com; 2402 char domain[4]; 2403 uint16_t val; 2404 int i, error; 2405 2406 if ((error = iwn_eeprom_lock(sc)) != 0) { 2407 device_printf(sc->sc_dev, 2408 "%s: could not lock EEPROM, error %d\n", __func__, error); 2409 return; 2410 } 2411 /* read and print regulatory domain */ 2412 iwn_read_prom_data(sc, IWN_EEPROM_DOMAIN, domain, 4); 2413 device_printf(sc->sc_dev,"Reg Domain: %.4s", domain); 2414 2415 /* read and print MAC address */ 2416 iwn_read_prom_data(sc, IWN_EEPROM_MAC, ic->ic_myaddr, 6); 2417 printf(", address %s\n", ether_sprintf(ic->ic_myaddr)); 2418 2419 /* read the list of authorized channels */ 2420 iwn_read_eeprom_channels(sc); 2421 2422 /* read maximum allowed Tx power for 2GHz and 5GHz bands */ 2423 iwn_read_prom_data(sc, IWN_EEPROM_MAXPOW, &val, 2); 2424 sc->maxpwr2GHz = val & 0xff; 2425 sc->maxpwr5GHz = val >> 8; 2426 /* check that EEPROM values are correct */ 2427 if (sc->maxpwr5GHz < 20 || sc->maxpwr5GHz > 50) 2428 sc->maxpwr5GHz = 38; 2429 if (sc->maxpwr2GHz < 20 || sc->maxpwr2GHz > 50) 2430 sc->maxpwr2GHz = 38; 2431 DPRINTF(sc, IWN_DEBUG_RESET, "maxpwr 2GHz=%d 5GHz=%d\n", 2432 sc->maxpwr2GHz, sc->maxpwr5GHz); 2433 2434 /* read voltage at which samples were taken */ 2435 iwn_read_prom_data(sc, IWN_EEPROM_VOLTAGE, &val, 2); 2436 sc->eeprom_voltage = (int16_t)le16toh(val); 2437 DPRINTF(sc, IWN_DEBUG_RESET, "voltage=%d (in 0.3V)\n", 2438 sc->eeprom_voltage); 2439 2440 /* read power groups */ 2441 iwn_read_prom_data(sc, IWN_EEPROM_BANDS, sc->bands, sizeof sc->bands); 2442 #ifdef IWN_DEBUG 2443 if (sc->sc_debug & IWN_DEBUG_ANY) { 2444 for (i = 0; i < IWN_NBANDS; i++) 2445 iwn_print_power_group(sc, i); 2446 } 2447 #endif 2448 iwn_eeprom_unlock(sc); 2449 } 2450 2451 struct iwn_chan_band { 2452 uint32_t addr; /* offset in EEPROM */ 2453 uint32_t flags; /* net80211 flags */ 2454 uint8_t nchan; 2455 #define IWN_MAX_CHAN_PER_BAND 14 2456 uint8_t chan[IWN_MAX_CHAN_PER_BAND]; 2457 }; 2458 2459 static void 2460 iwn_read_eeprom_band(struct iwn_softc *sc, const struct iwn_chan_band *band) 2461 { 2462 struct ifnet *ifp = sc->sc_ifp; 2463 struct ieee80211com *ic = ifp->if_l2com; 2464 struct iwn_eeprom_chan channels[IWN_MAX_CHAN_PER_BAND]; 2465 struct ieee80211_channel *c; 2466 int i, chan, flags; 2467 2468 iwn_read_prom_data(sc, band->addr, channels, 2469 band->nchan * sizeof (struct iwn_eeprom_chan)); 2470 2471 for (i = 0; i < band->nchan; i++) { 2472 if (!(channels[i].flags & IWN_EEPROM_CHAN_VALID)) { 2473 DPRINTF(sc, IWN_DEBUG_RESET, 2474 "skip chan %d flags 0x%x maxpwr %d\n", 2475 band->chan[i], channels[i].flags, 2476 channels[i].maxpwr); 2477 continue; 2478 } 2479 chan = band->chan[i]; 2480 2481 /* translate EEPROM flags to net80211 */ 2482 flags = 0; 2483 if ((channels[i].flags & IWN_EEPROM_CHAN_ACTIVE) == 0) 2484 flags |= IEEE80211_CHAN_PASSIVE; 2485 if ((channels[i].flags & IWN_EEPROM_CHAN_IBSS) == 0) 2486 flags |= IEEE80211_CHAN_NOADHOC; 2487 if (channels[i].flags & IWN_EEPROM_CHAN_RADAR) { 2488 flags |= IEEE80211_CHAN_DFS; 2489 /* XXX apparently IBSS may still be marked */ 2490 flags |= IEEE80211_CHAN_NOADHOC; 2491 } 2492 2493 DPRINTF(sc, IWN_DEBUG_RESET, 2494 "add chan %d flags 0x%x maxpwr %d\n", 2495 chan, channels[i].flags, channels[i].maxpwr); 2496 2497 c = &ic->ic_channels[ic->ic_nchans++]; 2498 c->ic_ieee = chan; 2499 c->ic_freq = ieee80211_ieee2mhz(chan, band->flags); 2500 c->ic_maxregpower = channels[i].maxpwr; 2501 c->ic_maxpower = 2*c->ic_maxregpower; 2502 if (band->flags & IEEE80211_CHAN_2GHZ) { 2503 /* G =>'s B is supported */ 2504 c->ic_flags = IEEE80211_CHAN_B | flags; 2505 2506 c = &ic->ic_channels[ic->ic_nchans++]; 2507 c[0] = c[-1]; 2508 c->ic_flags = IEEE80211_CHAN_G | flags; 2509 } else { /* 5GHz band */ 2510 c->ic_flags = IEEE80211_CHAN_A | flags; 2511 } 2512 /* XXX no constraints on using HT20 */ 2513 /* add HT20, HT40 added separately */ 2514 c = &ic->ic_channels[ic->ic_nchans++]; 2515 c[0] = c[-1]; 2516 c->ic_flags |= IEEE80211_CHAN_HT20; 2517 /* XXX NARROW =>'s 1/2 and 1/4 width? */ 2518 } 2519 } 2520 2521 static void 2522 iwn_read_eeprom_ht40(struct iwn_softc *sc, const struct iwn_chan_band *band) 2523 { 2524 struct ifnet *ifp = sc->sc_ifp; 2525 struct ieee80211com *ic = ifp->if_l2com; 2526 struct iwn_eeprom_chan channels[IWN_MAX_CHAN_PER_BAND]; 2527 struct ieee80211_channel *c, *cent, *extc; 2528 int i; 2529 2530 iwn_read_prom_data(sc, band->addr, channels, 2531 band->nchan * sizeof (struct iwn_eeprom_chan)); 2532 2533 for (i = 0; i < band->nchan; i++) { 2534 if (!(channels[i].flags & IWN_EEPROM_CHAN_VALID) || 2535 !(channels[i].flags & IWN_EEPROM_CHAN_WIDE)) { 2536 DPRINTF(sc, IWN_DEBUG_RESET, 2537 "skip chan %d flags 0x%x maxpwr %d\n", 2538 band->chan[i], channels[i].flags, 2539 channels[i].maxpwr); 2540 continue; 2541 } 2542 /* 2543 * Each entry defines an HT40 channel pair; find the 2544 * center channel, then the extension channel above. 2545 */ 2546 cent = ieee80211_find_channel_byieee(ic, band->chan[i], 2547 band->flags & ~IEEE80211_CHAN_HT); 2548 if (cent == NULL) { /* XXX shouldn't happen */ 2549 device_printf(sc->sc_dev, 2550 "%s: no entry for channel %d\n", 2551 __func__, band->chan[i]); 2552 continue; 2553 } 2554 extc = ieee80211_find_channel(ic, cent->ic_freq+20, 2555 band->flags & ~IEEE80211_CHAN_HT); 2556 if (extc == NULL) { 2557 DPRINTF(sc, IWN_DEBUG_RESET, 2558 "skip chan %d, extension channel not found\n", 2559 band->chan[i]); 2560 continue; 2561 } 2562 2563 DPRINTF(sc, IWN_DEBUG_RESET, 2564 "add ht40 chan %d flags 0x%x maxpwr %d\n", 2565 band->chan[i], channels[i].flags, channels[i].maxpwr); 2566 2567 c = &ic->ic_channels[ic->ic_nchans++]; 2568 c[0] = cent[0]; 2569 c->ic_extieee = extc->ic_ieee; 2570 c->ic_flags &= ~IEEE80211_CHAN_HT; 2571 c->ic_flags |= IEEE80211_CHAN_HT40U; 2572 c = &ic->ic_channels[ic->ic_nchans++]; 2573 c[0] = extc[0]; 2574 c->ic_extieee = cent->ic_ieee; 2575 c->ic_flags &= ~IEEE80211_CHAN_HT; 2576 c->ic_flags |= IEEE80211_CHAN_HT40D; 2577 } 2578 } 2579 2580 static void 2581 iwn_read_eeprom_channels(struct iwn_softc *sc) 2582 { 2583 #define N(a) (sizeof(a)/sizeof(a[0])) 2584 static const struct iwn_chan_band iwn_bands[] = { 2585 { IWN_EEPROM_BAND1, IEEE80211_CHAN_G, 14, 2586 { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 } }, 2587 { IWN_EEPROM_BAND2, IEEE80211_CHAN_A, 13, 2588 { 183, 184, 185, 187, 188, 189, 192, 196, 7, 8, 11, 12, 16 } }, 2589 { IWN_EEPROM_BAND3, IEEE80211_CHAN_A, 12, 2590 { 34, 36, 38, 40, 42, 44, 46, 48, 52, 56, 60, 64 } }, 2591 { IWN_EEPROM_BAND4, IEEE80211_CHAN_A, 11, 2592 { 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140 } }, 2593 { IWN_EEPROM_BAND5, IEEE80211_CHAN_A, 6, 2594 { 145, 149, 153, 157, 161, 165 } }, 2595 { IWN_EEPROM_BAND6, IEEE80211_CHAN_G | IEEE80211_CHAN_HT40, 7, 2596 { 1, 2, 3, 4, 5, 6, 7 } }, 2597 { IWN_EEPROM_BAND7, IEEE80211_CHAN_A | IEEE80211_CHAN_HT40, 11, 2598 { 36, 44, 52, 60, 100, 108, 116, 124, 132, 149, 157 } } 2599 }; 2600 struct ifnet *ifp = sc->sc_ifp; 2601 struct ieee80211com *ic = ifp->if_l2com; 2602 int i; 2603 2604 /* read the list of authorized channels */ 2605 for (i = 0; i < N(iwn_bands)-2; i++) 2606 iwn_read_eeprom_band(sc, &iwn_bands[i]); 2607 for (; i < N(iwn_bands); i++) 2608 iwn_read_eeprom_ht40(sc, &iwn_bands[i]); 2609 ieee80211_sort_channels(ic->ic_channels, ic->ic_nchans); 2610 #undef N 2611 } 2612 2613 #ifdef IWN_DEBUG 2614 void 2615 iwn_print_power_group(struct iwn_softc *sc, int i) 2616 { 2617 struct iwn_eeprom_band *band = &sc->bands[i]; 2618 struct iwn_eeprom_chan_samples *chans = band->chans; 2619 int j, c; 2620 2621 printf("===band %d===\n", i); 2622 printf("chan lo=%d, chan hi=%d\n", band->lo, band->hi); 2623 printf("chan1 num=%d\n", chans[0].num); 2624 for (c = 0; c < IWN_NTXCHAINS; c++) { 2625 for (j = 0; j < IWN_NSAMPLES; j++) { 2626 printf("chain %d, sample %d: temp=%d gain=%d " 2627 "power=%d pa_det=%d\n", c, j, 2628 chans[0].samples[c][j].temp, 2629 chans[0].samples[c][j].gain, 2630 chans[0].samples[c][j].power, 2631 chans[0].samples[c][j].pa_det); 2632 } 2633 } 2634 printf("chan2 num=%d\n", chans[1].num); 2635 for (c = 0; c < IWN_NTXCHAINS; c++) { 2636 for (j = 0; j < IWN_NSAMPLES; j++) { 2637 printf("chain %d, sample %d: temp=%d gain=%d " 2638 "power=%d pa_det=%d\n", c, j, 2639 chans[1].samples[c][j].temp, 2640 chans[1].samples[c][j].gain, 2641 chans[1].samples[c][j].power, 2642 chans[1].samples[c][j].pa_det); 2643 } 2644 } 2645 } 2646 #endif 2647 2648 /* 2649 * Send a command to the firmware. 2650 */ 2651 int 2652 iwn_cmd(struct iwn_softc *sc, int code, const void *buf, int size, int async) 2653 { 2654 struct iwn_tx_ring *ring = &sc->txq[4]; 2655 struct iwn_tx_desc *desc; 2656 struct iwn_tx_cmd *cmd; 2657 bus_addr_t paddr; 2658 2659 IWN_LOCK_ASSERT(sc); 2660 2661 KASSERT(size <= sizeof cmd->data, ("Command too big")); 2662 2663 desc = &ring->desc[ring->cur]; 2664 cmd = &ring->cmd[ring->cur]; 2665 2666 cmd->code = code; 2667 cmd->flags = 0; 2668 cmd->qid = ring->qid; 2669 cmd->idx = ring->cur; 2670 memcpy(cmd->data, buf, size); 2671 2672 paddr = ring->cmd_dma.paddr + ring->cur * sizeof (struct iwn_tx_cmd); 2673 2674 IWN_SET_DESC_NSEGS(desc, 1); 2675 IWN_SET_DESC_SEG(desc, 0, paddr, 4 + size); 2676 sc->shared->len[ring->qid][ring->cur] = htole16(8); 2677 if (ring->cur < IWN_TX_WINDOW) { 2678 sc->shared->len[ring->qid][ring->cur + IWN_TX_RING_COUNT] = 2679 htole16(8); 2680 } 2681 2682 DPRINTF(sc, IWN_DEBUG_CMD, "%s: %s (0x%x) flags %d qid %d idx %d\n", 2683 __func__, iwn_intr_str(cmd->code), cmd->code, 2684 cmd->flags, cmd->qid, cmd->idx); 2685 2686 /* kick cmd ring */ 2687 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT; 2688 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur); 2689 2690 return async ? 0 : msleep(cmd, &sc->sc_mtx, PCATCH, "iwncmd", hz); 2691 } 2692 2693 static const uint8_t iwn_ridx_to_plcp[] = { 2694 10, 20, 55, 110, /* CCK */ 2695 0xd, 0xf, 0x5, 0x7, 0x9, 0xb, 0x1, 0x3, 0x3 /* OFDM R1-R4 */ 2696 }; 2697 static const uint8_t iwn_siso_mcs_to_plcp[] = { 2698 0, 0, 0, 0, /* CCK */ 2699 0, 0, 1, 2, 3, 4, 5, 6, 7 /* HT */ 2700 }; 2701 static const uint8_t iwn_mimo_mcs_to_plcp[] = { 2702 0, 0, 0, 0, /* CCK */ 2703 8, 8, 9, 10, 11, 12, 13, 14, 15 /* HT */ 2704 }; 2705 static const uint8_t iwn_prev_ridx[] = { 2706 /* NB: allow fallback from CCK11 to OFDM9 and from OFDM6 to CCK5 */ 2707 0, 0, 1, 5, /* CCK */ 2708 2, 4, 3, 6, 7, 8, 9, 10, 10 /* OFDM */ 2709 }; 2710 2711 /* 2712 * Configure hardware link parameters for the specified 2713 * node operating on the specified channel. 2714 */ 2715 int 2716 iwn_set_link_quality(struct iwn_softc *sc, uint8_t id, 2717 const struct ieee80211_channel *c, int async) 2718 { 2719 struct iwn_cmd_link_quality lq; 2720 int i, ridx; 2721 2722 memset(&lq, 0, sizeof(lq)); 2723 lq.id = id; 2724 if (IEEE80211_IS_CHAN_HT(c)) { 2725 lq.mimo = 1; 2726 lq.ssmask = 0x1; 2727 } else 2728 lq.ssmask = 0x2; 2729 2730 if (id == IWN_ID_BSS) 2731 ridx = IWN_RATE_OFDM54; 2732 else if (IEEE80211_IS_CHAN_A(c)) 2733 ridx = IWN_RATE_OFDM6; 2734 else 2735 ridx = IWN_RATE_CCK1; 2736 for (i = 0; i < IWN_MAX_TX_RETRIES; i++) { 2737 /* XXX toggle antenna for retry patterns */ 2738 if (IEEE80211_IS_CHAN_HT40(c)) { 2739 lq.table[i].rate = iwn_mimo_mcs_to_plcp[ridx] 2740 | IWN_RATE_MCS; 2741 lq.table[i].rflags = IWN_RFLAG_HT 2742 | IWN_RFLAG_HT40 2743 | IWN_RFLAG_ANT_A; 2744 /* XXX shortGI */ 2745 } else if (IEEE80211_IS_CHAN_HT(c)) { 2746 lq.table[i].rate = iwn_siso_mcs_to_plcp[ridx] 2747 | IWN_RATE_MCS; 2748 lq.table[i].rflags = IWN_RFLAG_HT 2749 | IWN_RFLAG_ANT_A; 2750 /* XXX shortGI */ 2751 } else { 2752 lq.table[i].rate = iwn_ridx_to_plcp[ridx]; 2753 if (ridx <= IWN_RATE_CCK11) 2754 lq.table[i].rflags = IWN_RFLAG_CCK; 2755 lq.table[i].rflags |= IWN_RFLAG_ANT_B; 2756 } 2757 ridx = iwn_prev_ridx[ridx]; 2758 } 2759 2760 lq.dsmask = 0x3; 2761 lq.ampdu_disable = 3; 2762 lq.ampdu_limit = htole16(4000); 2763 #ifdef IWN_DEBUG 2764 if (sc->sc_debug & IWN_DEBUG_STATE) { 2765 printf("%s: set link quality for node %d, mimo %d ssmask %d\n", 2766 __func__, id, lq.mimo, lq.ssmask); 2767 printf("%s:", __func__); 2768 for (i = 0; i < IWN_MAX_TX_RETRIES; i++) 2769 printf(" %d:%x", lq.table[i].rate, lq.table[i].rflags); 2770 printf("\n"); 2771 } 2772 #endif 2773 return iwn_cmd(sc, IWN_CMD_TX_LINK_QUALITY, &lq, sizeof(lq), async); 2774 } 2775 2776 #if 0 2777 2778 /* 2779 * Install a pairwise key into the hardware. 2780 */ 2781 int 2782 iwn_set_key(struct ieee80211com *ic, struct ieee80211_node *ni, 2783 const struct ieee80211_key *k) 2784 { 2785 struct iwn_softc *sc = ic->ic_softc; 2786 struct iwn_node_info node; 2787 2788 if (k->k_flags & IEEE80211_KEY_GROUP) 2789 return 0; 2790 2791 memset(&node, 0, sizeof node); 2792 2793 switch (k->k_cipher) { 2794 case IEEE80211_CIPHER_CCMP: 2795 node.security = htole16(IWN_CIPHER_CCMP); 2796 memcpy(node.key, k->k_key, k->k_len); 2797 break; 2798 default: 2799 return 0; 2800 } 2801 2802 node.id = IWN_ID_BSS; 2803 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr); 2804 node.control = IWN_NODE_UPDATE; 2805 node.flags = IWN_FLAG_SET_KEY; 2806 2807 return iwn_cmd(sc, IWN_CMD_ADD_NODE, &node, sizeof node, 1); 2808 } 2809 #endif 2810 2811 int 2812 iwn_wme_update(struct ieee80211com *ic) 2813 { 2814 #define IWN_EXP2(x) ((1 << (x)) - 1) /* CWmin = 2^ECWmin - 1 */ 2815 #define IWN_TXOP_TO_US(v) (v<<5) 2816 struct iwn_softc *sc = ic->ic_ifp->if_softc; 2817 struct iwn_edca_params cmd; 2818 int i; 2819 2820 memset(&cmd, 0, sizeof cmd); 2821 cmd.flags = htole32(IWN_EDCA_UPDATE); 2822 for (i = 0; i < WME_NUM_AC; i++) { 2823 const struct wmeParams *wmep = 2824 &ic->ic_wme.wme_chanParams.cap_wmeParams[i]; 2825 cmd.ac[i].aifsn = wmep->wmep_aifsn; 2826 cmd.ac[i].cwmin = htole16(IWN_EXP2(wmep->wmep_logcwmin)); 2827 cmd.ac[i].cwmax = htole16(IWN_EXP2(wmep->wmep_logcwmax)); 2828 cmd.ac[i].txoplimit = 2829 htole16(IWN_TXOP_TO_US(wmep->wmep_txopLimit)); 2830 } 2831 IWN_LOCK(sc); 2832 (void) iwn_cmd(sc, IWN_CMD_EDCA_PARAMS, &cmd, sizeof cmd, 1 /*async*/); 2833 IWN_UNLOCK(sc); 2834 return 0; 2835 #undef IWN_TXOP_TO_US 2836 #undef IWN_EXP2 2837 } 2838 2839 void 2840 iwn_set_led(struct iwn_softc *sc, uint8_t which, uint8_t off, uint8_t on) 2841 { 2842 struct iwn_cmd_led led; 2843 2844 led.which = which; 2845 led.unit = htole32(100000); /* on/off in unit of 100ms */ 2846 led.off = off; 2847 led.on = on; 2848 2849 (void) iwn_cmd(sc, IWN_CMD_SET_LED, &led, sizeof led, 1); 2850 } 2851 2852 /* 2853 * Set the critical temperature at which the firmware will automatically stop 2854 * the radio transmitter. 2855 */ 2856 int 2857 iwn_set_critical_temp(struct iwn_softc *sc) 2858 { 2859 struct iwn_ucode_info *uc = &sc->ucode_info; 2860 struct iwn_critical_temp crit; 2861 uint32_t r1, r2, r3, temp; 2862 2863 r1 = le32toh(uc->temp[0].chan20MHz); 2864 r2 = le32toh(uc->temp[1].chan20MHz); 2865 r3 = le32toh(uc->temp[2].chan20MHz); 2866 /* inverse function of iwn_get_temperature() */ 2867 temp = r2 + (IWN_CTOK(110) * (r3 - r1)) / 259; 2868 2869 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_CTEMP_STOP_RF); 2870 2871 memset(&crit, 0, sizeof crit); 2872 crit.tempR = htole32(temp); 2873 DPRINTF(sc, IWN_DEBUG_RESET, "setting critical temp to %u\n", temp); 2874 return iwn_cmd(sc, IWN_CMD_SET_CRITICAL_TEMP, &crit, sizeof crit, 0); 2875 } 2876 2877 void 2878 iwn_enable_tsf(struct iwn_softc *sc, struct ieee80211_node *ni) 2879 { 2880 struct iwn_cmd_tsf tsf; 2881 uint64_t val, mod; 2882 2883 memset(&tsf, 0, sizeof tsf); 2884 memcpy(&tsf.tstamp, ni->ni_tstamp.data, sizeof (uint64_t)); 2885 tsf.bintval = htole16(ni->ni_intval); 2886 tsf.lintval = htole16(10); 2887 2888 /* XXX all wrong */ 2889 /* compute remaining time until next beacon */ 2890 val = (uint64_t)ni->ni_intval * 1024; /* msecs -> usecs */ 2891 DPRINTF(sc, IWN_DEBUG_ANY, "%s: val = %ju %s\n", __func__, 2892 val, val == 0 ? "correcting" : ""); 2893 if (val == 0) 2894 val = 1; 2895 mod = le64toh(tsf.tstamp) % val; 2896 tsf.binitval = htole32((uint32_t)(val - mod)); 2897 2898 DPRINTF(sc, IWN_DEBUG_RESET, "TSF bintval=%u tstamp=%ju, init=%u\n", 2899 ni->ni_intval, le64toh(tsf.tstamp), (uint32_t)(val - mod)); 2900 2901 if (iwn_cmd(sc, IWN_CMD_TSF, &tsf, sizeof tsf, 1) != 0) 2902 device_printf(sc->sc_dev, 2903 "%s: could not enable TSF\n", __func__); 2904 } 2905 2906 void 2907 iwn_power_calibration(struct iwn_softc *sc, int temp) 2908 { 2909 struct ifnet *ifp = sc->sc_ifp; 2910 struct ieee80211com *ic = ifp->if_l2com; 2911 #if 0 2912 KASSERT(ic->ic_state == IEEE80211_S_RUN, ("not running")); 2913 #endif 2914 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: temperature %d->%d\n", 2915 __func__, sc->temp, temp); 2916 2917 /* adjust Tx power if need be (delta >= 3�C) */ 2918 if (abs(temp - sc->temp) < 3) 2919 return; 2920 2921 sc->temp = temp; 2922 2923 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: set Tx power for channel %d\n", 2924 __func__, ieee80211_chan2ieee(ic, ic->ic_bsschan)); 2925 if (iwn_set_txpower(sc, ic->ic_bsschan, 1) != 0) { 2926 /* just warn, too bad for the automatic calibration... */ 2927 device_printf(sc->sc_dev, 2928 "%s: could not adjust Tx power\n", __func__); 2929 } 2930 } 2931 2932 /* 2933 * Set Tx power for a given channel (each rate has its own power settings). 2934 * This function takes into account the regulatory information from EEPROM, 2935 * the current temperature and the current voltage. 2936 */ 2937 int 2938 iwn_set_txpower(struct iwn_softc *sc, struct ieee80211_channel *ch, int async) 2939 { 2940 /* fixed-point arithmetic division using a n-bit fractional part */ 2941 #define fdivround(a, b, n) \ 2942 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n)) 2943 /* linear interpolation */ 2944 #define interpolate(x, x1, y1, x2, y2, n) \ 2945 ((y1) + fdivround(((int)(x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n)) 2946 2947 static const int tdiv[IWN_NATTEN_GROUPS] = { 9, 8, 8, 8, 6 }; 2948 struct ifnet *ifp = sc->sc_ifp; 2949 struct ieee80211com *ic = ifp->if_l2com; 2950 struct iwn_ucode_info *uc = &sc->ucode_info; 2951 struct iwn_cmd_txpower cmd; 2952 struct iwn_eeprom_chan_samples *chans; 2953 const uint8_t *rf_gain, *dsp_gain; 2954 int32_t vdiff, tdiff; 2955 int i, c, grp, maxpwr; 2956 u_int chan; 2957 2958 /* get channel number */ 2959 chan = ieee80211_chan2ieee(ic, ch); 2960 2961 memset(&cmd, 0, sizeof cmd); 2962 cmd.band = IEEE80211_IS_CHAN_5GHZ(ch) ? 0 : 1; 2963 cmd.chan = chan; 2964 2965 if (IEEE80211_IS_CHAN_5GHZ(ch)) { 2966 maxpwr = sc->maxpwr5GHz; 2967 rf_gain = iwn_rf_gain_5ghz; 2968 dsp_gain = iwn_dsp_gain_5ghz; 2969 } else { 2970 maxpwr = sc->maxpwr2GHz; 2971 rf_gain = iwn_rf_gain_2ghz; 2972 dsp_gain = iwn_dsp_gain_2ghz; 2973 } 2974 2975 /* compute voltage compensation */ 2976 vdiff = ((int32_t)le32toh(uc->volt) - sc->eeprom_voltage) / 7; 2977 if (vdiff > 0) 2978 vdiff *= 2; 2979 if (abs(vdiff) > 2) 2980 vdiff = 0; 2981 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 2982 "%s: voltage compensation=%d (UCODE=%d, EEPROM=%d)\n", 2983 __func__, vdiff, le32toh(uc->volt), sc->eeprom_voltage); 2984 2985 /* get channel's attenuation group */ 2986 if (chan <= 20) /* 1-20 */ 2987 grp = 4; 2988 else if (chan <= 43) /* 34-43 */ 2989 grp = 0; 2990 else if (chan <= 70) /* 44-70 */ 2991 grp = 1; 2992 else if (chan <= 124) /* 71-124 */ 2993 grp = 2; 2994 else /* 125-200 */ 2995 grp = 3; 2996 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 2997 "%s: chan %d, attenuation group=%d\n", __func__, chan, grp); 2998 2999 /* get channel's sub-band */ 3000 for (i = 0; i < IWN_NBANDS; i++) 3001 if (sc->bands[i].lo != 0 && 3002 sc->bands[i].lo <= chan && chan <= sc->bands[i].hi) 3003 break; 3004 chans = sc->bands[i].chans; 3005 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 3006 "%s: chan %d sub-band=%d\n", __func__, chan, i); 3007 3008 for (c = 0; c < IWN_NTXCHAINS; c++) { 3009 uint8_t power, gain, temp; 3010 int maxchpwr, pwr, ridx, idx; 3011 3012 power = interpolate(chan, 3013 chans[0].num, chans[0].samples[c][1].power, 3014 chans[1].num, chans[1].samples[c][1].power, 1); 3015 gain = interpolate(chan, 3016 chans[0].num, chans[0].samples[c][1].gain, 3017 chans[1].num, chans[1].samples[c][1].gain, 1); 3018 temp = interpolate(chan, 3019 chans[0].num, chans[0].samples[c][1].temp, 3020 chans[1].num, chans[1].samples[c][1].temp, 1); 3021 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 3022 "%s: Tx chain %d: power=%d gain=%d temp=%d\n", 3023 __func__, c, power, gain, temp); 3024 3025 /* compute temperature compensation */ 3026 tdiff = ((sc->temp - temp) * 2) / tdiv[grp]; 3027 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 3028 "%s: temperature compensation=%d (current=%d, EEPROM=%d)\n", 3029 __func__, tdiff, sc->temp, temp); 3030 3031 for (ridx = 0; ridx <= IWN_RIDX_MAX; ridx++) { 3032 maxchpwr = ch->ic_maxpower; 3033 if ((ridx / 8) & 1) { 3034 /* MIMO: decrease Tx power (-3dB) */ 3035 maxchpwr -= 6; 3036 } 3037 3038 pwr = maxpwr - 10; 3039 3040 /* decrease power for highest OFDM rates */ 3041 if ((ridx % 8) == 5) /* 48Mbit/s */ 3042 pwr -= 5; 3043 else if ((ridx % 8) == 6) /* 54Mbit/s */ 3044 pwr -= 7; 3045 else if ((ridx % 8) == 7) /* 60Mbit/s */ 3046 pwr -= 10; 3047 3048 if (pwr > maxchpwr) 3049 pwr = maxchpwr; 3050 3051 idx = gain - (pwr - power) - tdiff - vdiff; 3052 if ((ridx / 8) & 1) /* MIMO */ 3053 idx += (int32_t)le32toh(uc->atten[grp][c]); 3054 3055 if (cmd.band == 0) 3056 idx += 9; /* 5GHz */ 3057 if (ridx == IWN_RIDX_MAX) 3058 idx += 5; /* CCK */ 3059 3060 /* make sure idx stays in a valid range */ 3061 if (idx < 0) 3062 idx = 0; 3063 else if (idx > IWN_MAX_PWR_INDEX) 3064 idx = IWN_MAX_PWR_INDEX; 3065 3066 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 3067 "%s: Tx chain %d, rate idx %d: power=%d\n", 3068 __func__, c, ridx, idx); 3069 cmd.power[ridx].rf_gain[c] = rf_gain[idx]; 3070 cmd.power[ridx].dsp_gain[c] = dsp_gain[idx]; 3071 } 3072 } 3073 3074 DPRINTF(sc, IWN_DEBUG_CALIBRATE | IWN_DEBUG_TXPOW, 3075 "%s: set tx power for chan %d\n", __func__, chan); 3076 return iwn_cmd(sc, IWN_CMD_TXPOWER, &cmd, sizeof cmd, async); 3077 3078 #undef interpolate 3079 #undef fdivround 3080 } 3081 3082 /* 3083 * Get the best (maximum) RSSI among the 3084 * connected antennas and convert to dBm. 3085 */ 3086 int8_t 3087 iwn_get_rssi(struct iwn_softc *sc, const struct iwn_rx_stat *stat) 3088 { 3089 int mask, agc, rssi; 3090 3091 mask = (le16toh(stat->antenna) >> 4) & 0x7; 3092 agc = (le16toh(stat->agc) >> 7) & 0x7f; 3093 3094 rssi = 0; 3095 #if 0 3096 if (mask & (1 << 0)) /* Ant A */ 3097 rssi = max(rssi, stat->rssi[0]); 3098 if (mask & (1 << 1)) /* Ant B */ 3099 rssi = max(rssi, stat->rssi[2]); 3100 if (mask & (1 << 2)) /* Ant C */ 3101 rssi = max(rssi, stat->rssi[4]); 3102 #else 3103 rssi = max(rssi, stat->rssi[0]); 3104 rssi = max(rssi, stat->rssi[2]); 3105 rssi = max(rssi, stat->rssi[4]); 3106 #endif 3107 DPRINTF(sc, IWN_DEBUG_RECV, "%s: agc %d mask 0x%x rssi %d %d %d " 3108 "result %d\n", __func__, agc, mask, 3109 stat->rssi[0], stat->rssi[2], stat->rssi[4], 3110 rssi - agc - IWN_RSSI_TO_DBM); 3111 return rssi - agc - IWN_RSSI_TO_DBM; 3112 } 3113 3114 /* 3115 * Get the average noise among Rx antennas (in dBm). 3116 */ 3117 int 3118 iwn_get_noise(const struct iwn_rx_general_stats *stats) 3119 { 3120 int i, total, nbant, noise; 3121 3122 total = nbant = 0; 3123 for (i = 0; i < 3; i++) { 3124 noise = le32toh(stats->noise[i]) & 0xff; 3125 if (noise != 0) { 3126 total += noise; 3127 nbant++; 3128 } 3129 } 3130 /* there should be at least one antenna but check anyway */ 3131 return (nbant == 0) ? -127 : (total / nbant) - 107; 3132 } 3133 3134 /* 3135 * Read temperature (in degC) from the on-board thermal sensor. 3136 */ 3137 int 3138 iwn_get_temperature(struct iwn_softc *sc) 3139 { 3140 struct iwn_ucode_info *uc = &sc->ucode_info; 3141 int32_t r1, r2, r3, r4, temp; 3142 3143 r1 = le32toh(uc->temp[0].chan20MHz); 3144 r2 = le32toh(uc->temp[1].chan20MHz); 3145 r3 = le32toh(uc->temp[2].chan20MHz); 3146 r4 = le32toh(sc->rawtemp); 3147 3148 if (r1 == r3) /* prevents division by 0 (should not happen) */ 3149 return 0; 3150 3151 /* sign-extend 23-bit R4 value to 32-bit */ 3152 r4 = (r4 << 8) >> 8; 3153 /* compute temperature */ 3154 temp = (259 * (r4 - r2)) / (r3 - r1); 3155 temp = (temp * 97) / 100 + 8; 3156 3157 return IWN_KTOC(temp); 3158 } 3159 3160 /* 3161 * Initialize sensitivity calibration state machine. 3162 */ 3163 int 3164 iwn_init_sensitivity(struct iwn_softc *sc) 3165 { 3166 struct iwn_calib_state *calib = &sc->calib; 3167 struct iwn_phy_calib_cmd cmd; 3168 int error; 3169 3170 /* reset calibration state */ 3171 memset(calib, 0, sizeof (*calib)); 3172 calib->state = IWN_CALIB_STATE_INIT; 3173 calib->cck_state = IWN_CCK_STATE_HIFA; 3174 /* initial values taken from the reference driver */ 3175 calib->corr_ofdm_x1 = 105; 3176 calib->corr_ofdm_mrc_x1 = 220; 3177 calib->corr_ofdm_x4 = 90; 3178 calib->corr_ofdm_mrc_x4 = 170; 3179 calib->corr_cck_x4 = 125; 3180 calib->corr_cck_mrc_x4 = 200; 3181 calib->energy_cck = 100; 3182 3183 /* write initial sensitivity values */ 3184 error = iwn_send_sensitivity(sc); 3185 if (error != 0) 3186 return error; 3187 3188 memset(&cmd, 0, sizeof cmd); 3189 cmd.code = IWN_SET_DIFF_GAIN; 3190 /* differential gains initially set to 0 for all 3 antennas */ 3191 DPRINTF(sc, IWN_DEBUG_CALIBRATE, "%s: calibrate phy\n", __func__); 3192 return iwn_cmd(sc, IWN_PHY_CALIB, &cmd, sizeof cmd, 1); 3193 } 3194 3195 /* 3196 * Collect noise and RSSI statistics for the first 20 beacons received 3197 * after association and use them to determine connected antennas and 3198 * set differential gains. 3199 */ 3200 void 3201 iwn_compute_differential_gain(struct iwn_softc *sc, 3202 const struct iwn_rx_general_stats *stats) 3203 { 3204 struct iwn_calib_state *calib = &sc->calib; 3205 struct iwn_phy_calib_cmd cmd; 3206 int i, val; 3207 3208 /* accumulate RSSI and noise for all 3 antennas */ 3209 for (i = 0; i < 3; i++) { 3210 calib->rssi[i] += le32toh(stats->rssi[i]) & 0xff; 3211 calib->noise[i] += le32toh(stats->noise[i]) & 0xff; 3212 } 3213 3214 /* we update differential gain only once after 20 beacons */ 3215 if (++calib->nbeacons < 20) 3216 return; 3217 3218 /* determine antenna with highest average RSSI */ 3219 val = max(calib->rssi[0], calib->rssi[1]); 3220 val = max(calib->rssi[2], val); 3221 3222 /* determine which antennas are connected */ 3223 sc->antmsk = 0; 3224 for (i = 0; i < 3; i++) 3225 if (val - calib->rssi[i] <= 15 * 20) 3226 sc->antmsk |= 1 << i; 3227 /* if neither Ant A and Ant B are connected.. */ 3228 if ((sc->antmsk & (1 << 0 | 1 << 1)) == 0) 3229 sc->antmsk |= 1 << 1; /* ..mark Ant B as connected! */ 3230 3231 /* get minimal noise among connected antennas */ 3232 val = INT_MAX; /* ok, there's at least one */ 3233 for (i = 0; i < 3; i++) 3234 if (sc->antmsk & (1 << i)) 3235 val = min(calib->noise[i], val); 3236 3237 memset(&cmd, 0, sizeof cmd); 3238 cmd.code = IWN_SET_DIFF_GAIN; 3239 /* set differential gains for connected antennas */ 3240 for (i = 0; i < 3; i++) { 3241 if (sc->antmsk & (1 << i)) { 3242 cmd.gain[i] = (calib->noise[i] - val) / 30; 3243 /* limit differential gain to 3 */ 3244 cmd.gain[i] = min(cmd.gain[i], 3); 3245 cmd.gain[i] |= IWN_GAIN_SET; 3246 } 3247 } 3248 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 3249 "%s: set differential gains Ant A/B/C: %x/%x/%x (%x)\n", 3250 __func__,cmd.gain[0], cmd.gain[1], cmd.gain[2], sc->antmsk); 3251 if (iwn_cmd(sc, IWN_PHY_CALIB, &cmd, sizeof cmd, 1) == 0) 3252 calib->state = IWN_CALIB_STATE_RUN; 3253 } 3254 3255 /* 3256 * Tune RF Rx sensitivity based on the number of false alarms detected 3257 * during the last beacon period. 3258 */ 3259 void 3260 iwn_tune_sensitivity(struct iwn_softc *sc, const struct iwn_rx_stats *stats) 3261 { 3262 #define inc_clip(val, inc, max) \ 3263 if ((val) < (max)) { \ 3264 if ((val) < (max) - (inc)) \ 3265 (val) += (inc); \ 3266 else \ 3267 (val) = (max); \ 3268 needs_update = 1; \ 3269 } 3270 #define dec_clip(val, dec, min) \ 3271 if ((val) > (min)) { \ 3272 if ((val) > (min) + (dec)) \ 3273 (val) -= (dec); \ 3274 else \ 3275 (val) = (min); \ 3276 needs_update = 1; \ 3277 } 3278 3279 struct iwn_calib_state *calib = &sc->calib; 3280 uint32_t val, rxena, fa; 3281 uint32_t energy[3], energy_min; 3282 uint8_t noise[3], noise_ref; 3283 int i, needs_update = 0; 3284 3285 /* check that we've been enabled long enough */ 3286 if ((rxena = le32toh(stats->general.load)) == 0) 3287 return; 3288 3289 /* compute number of false alarms since last call for OFDM */ 3290 fa = le32toh(stats->ofdm.bad_plcp) - calib->bad_plcp_ofdm; 3291 fa += le32toh(stats->ofdm.fa) - calib->fa_ofdm; 3292 fa *= 200 * 1024; /* 200TU */ 3293 3294 /* save counters values for next call */ 3295 calib->bad_plcp_ofdm = le32toh(stats->ofdm.bad_plcp); 3296 calib->fa_ofdm = le32toh(stats->ofdm.fa); 3297 3298 if (fa > 50 * rxena) { 3299 /* high false alarm count, decrease sensitivity */ 3300 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 3301 "%s: OFDM high false alarm count: %u\n", __func__, fa); 3302 inc_clip(calib->corr_ofdm_x1, 1, 140); 3303 inc_clip(calib->corr_ofdm_mrc_x1, 1, 270); 3304 inc_clip(calib->corr_ofdm_x4, 1, 120); 3305 inc_clip(calib->corr_ofdm_mrc_x4, 1, 210); 3306 3307 } else if (fa < 5 * rxena) { 3308 /* low false alarm count, increase sensitivity */ 3309 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 3310 "%s: OFDM low false alarm count: %u\n", __func__, fa); 3311 dec_clip(calib->corr_ofdm_x1, 1, 105); 3312 dec_clip(calib->corr_ofdm_mrc_x1, 1, 220); 3313 dec_clip(calib->corr_ofdm_x4, 1, 85); 3314 dec_clip(calib->corr_ofdm_mrc_x4, 1, 170); 3315 } 3316 3317 /* compute maximum noise among 3 antennas */ 3318 for (i = 0; i < 3; i++) 3319 noise[i] = (le32toh(stats->general.noise[i]) >> 8) & 0xff; 3320 val = max(noise[0], noise[1]); 3321 val = max(noise[2], val); 3322 /* insert it into our samples table */ 3323 calib->noise_samples[calib->cur_noise_sample] = val; 3324 calib->cur_noise_sample = (calib->cur_noise_sample + 1) % 20; 3325 3326 /* compute maximum noise among last 20 samples */ 3327 noise_ref = calib->noise_samples[0]; 3328 for (i = 1; i < 20; i++) 3329 noise_ref = max(noise_ref, calib->noise_samples[i]); 3330 3331 /* compute maximum energy among 3 antennas */ 3332 for (i = 0; i < 3; i++) 3333 energy[i] = le32toh(stats->general.energy[i]); 3334 val = min(energy[0], energy[1]); 3335 val = min(energy[2], val); 3336 /* insert it into our samples table */ 3337 calib->energy_samples[calib->cur_energy_sample] = val; 3338 calib->cur_energy_sample = (calib->cur_energy_sample + 1) % 10; 3339 3340 /* compute minimum energy among last 10 samples */ 3341 energy_min = calib->energy_samples[0]; 3342 for (i = 1; i < 10; i++) 3343 energy_min = max(energy_min, calib->energy_samples[i]); 3344 energy_min += 6; 3345 3346 /* compute number of false alarms since last call for CCK */ 3347 fa = le32toh(stats->cck.bad_plcp) - calib->bad_plcp_cck; 3348 fa += le32toh(stats->cck.fa) - calib->fa_cck; 3349 fa *= 200 * 1024; /* 200TU */ 3350 3351 /* save counters values for next call */ 3352 calib->bad_plcp_cck = le32toh(stats->cck.bad_plcp); 3353 calib->fa_cck = le32toh(stats->cck.fa); 3354 3355 if (fa > 50 * rxena) { 3356 /* high false alarm count, decrease sensitivity */ 3357 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 3358 "%s: CCK high false alarm count: %u\n", __func__, fa); 3359 calib->cck_state = IWN_CCK_STATE_HIFA; 3360 calib->low_fa = 0; 3361 3362 if (calib->corr_cck_x4 > 160) { 3363 calib->noise_ref = noise_ref; 3364 if (calib->energy_cck > 2) 3365 dec_clip(calib->energy_cck, 2, energy_min); 3366 } 3367 if (calib->corr_cck_x4 < 160) { 3368 calib->corr_cck_x4 = 161; 3369 needs_update = 1; 3370 } else 3371 inc_clip(calib->corr_cck_x4, 3, 200); 3372 3373 inc_clip(calib->corr_cck_mrc_x4, 3, 400); 3374 3375 } else if (fa < 5 * rxena) { 3376 /* low false alarm count, increase sensitivity */ 3377 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 3378 "%s: CCK low false alarm count: %u\n", __func__, fa); 3379 calib->cck_state = IWN_CCK_STATE_LOFA; 3380 calib->low_fa++; 3381 3382 if (calib->cck_state != 0 && 3383 ((calib->noise_ref - noise_ref) > 2 || 3384 calib->low_fa > 100)) { 3385 inc_clip(calib->energy_cck, 2, 97); 3386 dec_clip(calib->corr_cck_x4, 3, 125); 3387 dec_clip(calib->corr_cck_mrc_x4, 3, 200); 3388 } 3389 } else { 3390 /* not worth to increase or decrease sensitivity */ 3391 DPRINTF(sc, IWN_DEBUG_CALIBRATE, 3392 "%s: CCK normal false alarm count: %u\n", __func__, fa); 3393 calib->low_fa = 0; 3394 calib->noise_ref = noise_ref; 3395 3396 if (calib->cck_state == IWN_CCK_STATE_HIFA) { 3397 /* previous interval had many false alarms */ 3398 dec_clip(calib->energy_cck, 8, energy_min); 3399 } 3400 calib->cck_state = IWN_CCK_STATE_INIT; 3401 } 3402 3403 if (needs_update) 3404 (void)iwn_send_sensitivity(sc); 3405 #undef dec_clip 3406 #undef inc_clip 3407 } 3408 3409 int 3410 iwn_send_sensitivity(struct iwn_softc *sc) 3411 { 3412 struct iwn_calib_state *calib = &sc->calib; 3413 struct iwn_sensitivity_cmd cmd; 3414 3415 memset(&cmd, 0, sizeof cmd); 3416 cmd.which = IWN_SENSITIVITY_WORKTBL; 3417 /* OFDM modulation */ 3418 cmd.corr_ofdm_x1 = htole16(calib->corr_ofdm_x1); 3419 cmd.corr_ofdm_mrc_x1 = htole16(calib->corr_ofdm_mrc_x1); 3420 cmd.corr_ofdm_x4 = htole16(calib->corr_ofdm_x4); 3421 cmd.corr_ofdm_mrc_x4 = htole16(calib->corr_ofdm_mrc_x4); 3422 cmd.energy_ofdm = htole16(100); 3423 cmd.energy_ofdm_th = htole16(62); 3424 /* CCK modulation */ 3425 cmd.corr_cck_x4 = htole16(calib->corr_cck_x4); 3426 cmd.corr_cck_mrc_x4 = htole16(calib->corr_cck_mrc_x4); 3427 cmd.energy_cck = htole16(calib->energy_cck); 3428 /* Barker modulation: use default values */ 3429 cmd.corr_barker = htole16(190); 3430 cmd.corr_barker_mrc = htole16(390); 3431 3432 DPRINTF(sc, IWN_DEBUG_RESET, 3433 "%s: set sensitivity %d/%d/%d/%d/%d/%d/%d\n", __func__, 3434 calib->corr_ofdm_x1, calib->corr_ofdm_mrc_x1, calib->corr_ofdm_x4, 3435 calib->corr_ofdm_mrc_x4, calib->corr_cck_x4, 3436 calib->corr_cck_mrc_x4, calib->energy_cck); 3437 return iwn_cmd(sc, IWN_SENSITIVITY, &cmd, sizeof cmd, 1); 3438 } 3439 3440 int 3441 iwn_auth(struct iwn_softc *sc) 3442 { 3443 struct ifnet *ifp = sc->sc_ifp; 3444 struct ieee80211com *ic = ifp->if_l2com; 3445 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); /*XXX*/ 3446 struct ieee80211_node *ni = vap->iv_bss; 3447 struct iwn_node_info node; 3448 int error; 3449 3450 sc->calib.state = IWN_CALIB_STATE_INIT; 3451 3452 /* update adapter's configuration */ 3453 sc->config.associd = 0; 3454 IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid); 3455 sc->config.chan = htole16(ieee80211_chan2ieee(ic, ni->ni_chan)); 3456 sc->config.flags = htole32(IWN_CONFIG_TSF); 3457 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 3458 sc->config.flags |= htole32(IWN_CONFIG_AUTO | IWN_CONFIG_24GHZ); 3459 if (IEEE80211_IS_CHAN_A(ni->ni_chan)) { 3460 sc->config.cck_mask = 0; 3461 sc->config.ofdm_mask = 0x15; 3462 } else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) { 3463 sc->config.cck_mask = 0x03; 3464 sc->config.ofdm_mask = 0; 3465 } else { 3466 /* XXX assume 802.11b/g */ 3467 sc->config.cck_mask = 0x0f; 3468 sc->config.ofdm_mask = 0x15; 3469 } 3470 if (ic->ic_flags & IEEE80211_F_SHSLOT) 3471 sc->config.flags |= htole32(IWN_CONFIG_SHSLOT); 3472 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 3473 sc->config.flags |= htole32(IWN_CONFIG_SHPREAMBLE); 3474 sc->config.filter &= ~htole32(IWN_FILTER_BSS); 3475 3476 DPRINTF(sc, IWN_DEBUG_STATE, 3477 "%s: config chan %d mode %d flags 0x%x cck 0x%x ofdm 0x%x " 3478 "ht_single 0x%x ht_dual 0x%x rxchain 0x%x " 3479 "myaddr %6D wlap %6D bssid %6D associd %d filter 0x%x\n", 3480 __func__, 3481 le16toh(sc->config.chan), sc->config.mode, le32toh(sc->config.flags), 3482 sc->config.cck_mask, sc->config.ofdm_mask, 3483 sc->config.ht_single_mask, sc->config.ht_dual_mask, 3484 le16toh(sc->config.rxchain), 3485 sc->config.myaddr, ":", sc->config.wlap, ":", sc->config.bssid, ":", 3486 le16toh(sc->config.associd), le32toh(sc->config.filter)); 3487 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->config, 3488 sizeof (struct iwn_config), 1); 3489 if (error != 0) { 3490 device_printf(sc->sc_dev, 3491 "%s: could not configure, error %d\n", __func__, error); 3492 return error; 3493 } 3494 sc->sc_curchan = ic->ic_curchan; 3495 3496 /* configuration has changed, set Tx power accordingly */ 3497 error = iwn_set_txpower(sc, ni->ni_chan, 1); 3498 if (error != 0) { 3499 device_printf(sc->sc_dev, 3500 "%s: could not set Tx power, error %d\n", __func__, error); 3501 return error; 3502 } 3503 3504 /* 3505 * Reconfiguring clears the adapter's nodes table so we must 3506 * add the broadcast node again. 3507 */ 3508 memset(&node, 0, sizeof node); 3509 IEEE80211_ADDR_COPY(node.macaddr, ifp->if_broadcastaddr); 3510 node.id = IWN_ID_BROADCAST; 3511 DPRINTF(sc, IWN_DEBUG_STATE, "%s: add broadcast node\n", __func__); 3512 error = iwn_cmd(sc, IWN_CMD_ADD_NODE, &node, sizeof node, 1); 3513 if (error != 0) { 3514 device_printf(sc->sc_dev, 3515 "%s: could not add broadcast node, error %d\n", 3516 __func__, error); 3517 return error; 3518 } 3519 error = iwn_set_link_quality(sc, node.id, ic->ic_curchan, 1); 3520 if (error != 0) { 3521 device_printf(sc->sc_dev, 3522 "%s: could not setup MRR for broadcast node, error %d\n", 3523 __func__, error); 3524 return error; 3525 } 3526 3527 return 0; 3528 } 3529 3530 /* 3531 * Configure the adapter for associated state. 3532 */ 3533 int 3534 iwn_run(struct iwn_softc *sc) 3535 { 3536 #define MS(v,x) (((v) & x) >> x##_S) 3537 struct ifnet *ifp = sc->sc_ifp; 3538 struct ieee80211com *ic = ifp->if_l2com; 3539 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); /*XXX*/ 3540 struct ieee80211_node *ni = vap->iv_bss; 3541 struct iwn_node_info node; 3542 int error, maxrxampdu, ampdudensity; 3543 3544 sc->calib.state = IWN_CALIB_STATE_INIT; 3545 3546 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 3547 /* link LED blinks while monitoring */ 3548 iwn_set_led(sc, IWN_LED_LINK, 5, 5); 3549 return 0; 3550 } 3551 3552 iwn_enable_tsf(sc, ni); 3553 3554 /* update adapter's configuration */ 3555 sc->config.associd = htole16(IEEE80211_AID(ni->ni_associd)); 3556 /* short preamble/slot time are negotiated when associating */ 3557 sc->config.flags &= ~htole32(IWN_CONFIG_SHPREAMBLE | IWN_CONFIG_SHSLOT); 3558 if (ic->ic_flags & IEEE80211_F_SHSLOT) 3559 sc->config.flags |= htole32(IWN_CONFIG_SHSLOT); 3560 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 3561 sc->config.flags |= htole32(IWN_CONFIG_SHPREAMBLE); 3562 if (IEEE80211_IS_CHAN_HT(ni->ni_chan)) { 3563 sc->config.flags &= ~htole32(IWN_CONFIG_HT); 3564 if (IEEE80211_IS_CHAN_HT40U(ni->ni_chan)) 3565 sc->config.flags |= htole32(IWN_CONFIG_HT40U); 3566 else if (IEEE80211_IS_CHAN_HT40D(ni->ni_chan)) 3567 sc->config.flags |= htole32(IWN_CONFIG_HT40D); 3568 else 3569 sc->config.flags |= htole32(IWN_CONFIG_HT20); 3570 sc->config.rxchain = htole16( 3571 (3 << IWN_RXCHAIN_VALID_S) 3572 | (3 << IWN_RXCHAIN_MIMO_CNT_S) 3573 | (1 << IWN_RXCHAIN_CNT_S) 3574 | IWN_RXCHAIN_MIMO_FORCE); 3575 3576 maxrxampdu = MS(ni->ni_htparam, IEEE80211_HTCAP_MAXRXAMPDU); 3577 ampdudensity = MS(ni->ni_htparam, IEEE80211_HTCAP_MPDUDENSITY); 3578 } else 3579 maxrxampdu = ampdudensity = 0; 3580 sc->config.filter |= htole32(IWN_FILTER_BSS); 3581 3582 DPRINTF(sc, IWN_DEBUG_STATE, 3583 "%s: config chan %d mode %d flags 0x%x cck 0x%x ofdm 0x%x " 3584 "ht_single 0x%x ht_dual 0x%x rxchain 0x%x " 3585 "myaddr %6D wlap %6D bssid %6D associd %d filter 0x%x\n", 3586 __func__, 3587 le16toh(sc->config.chan), sc->config.mode, le32toh(sc->config.flags), 3588 sc->config.cck_mask, sc->config.ofdm_mask, 3589 sc->config.ht_single_mask, sc->config.ht_dual_mask, 3590 le16toh(sc->config.rxchain), 3591 sc->config.myaddr, ":", sc->config.wlap, ":", sc->config.bssid, ":", 3592 le16toh(sc->config.associd), le32toh(sc->config.filter)); 3593 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->config, 3594 sizeof (struct iwn_config), 1); 3595 if (error != 0) { 3596 device_printf(sc->sc_dev, 3597 "%s: could not update configuration, error %d\n", 3598 __func__, error); 3599 return error; 3600 } 3601 sc->sc_curchan = ni->ni_chan; 3602 3603 /* configuration has changed, set Tx power accordingly */ 3604 error = iwn_set_txpower(sc, ni->ni_chan, 1); 3605 if (error != 0) { 3606 device_printf(sc->sc_dev, 3607 "%s: could not set Tx power, error %d\n", __func__, error); 3608 return error; 3609 } 3610 3611 /* add BSS node */ 3612 memset(&node, 0, sizeof node); 3613 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr); 3614 node.id = IWN_ID_BSS; 3615 node.htflags = htole32( 3616 (maxrxampdu << IWN_MAXRXAMPDU_S) | 3617 (ampdudensity << IWN_MPDUDENSITY_S)); 3618 DPRINTF(sc, IWN_DEBUG_STATE, "%s: add BSS node, id %d htflags 0x%x\n", 3619 __func__, node.id, le32toh(node.htflags)); 3620 error = iwn_cmd(sc, IWN_CMD_ADD_NODE, &node, sizeof node, 1); 3621 if (error != 0) { 3622 device_printf(sc->sc_dev,"could not add BSS node\n"); 3623 return error; 3624 } 3625 error = iwn_set_link_quality(sc, node.id, ni->ni_chan, 1); 3626 if (error != 0) { 3627 device_printf(sc->sc_dev, 3628 "%s: could not setup MRR for node %d, error %d\n", 3629 __func__, node.id, error); 3630 return error; 3631 } 3632 3633 if (ic->ic_opmode == IEEE80211_M_STA) { 3634 /* fake a join to init the tx rate */ 3635 iwn_newassoc(ni, 1); 3636 } 3637 3638 error = iwn_init_sensitivity(sc); 3639 if (error != 0) { 3640 device_printf(sc->sc_dev, 3641 "%s: could not set sensitivity, error %d\n", 3642 __func__, error); 3643 return error; 3644 } 3645 3646 /* start/restart periodic calibration timer */ 3647 sc->calib.state = IWN_CALIB_STATE_ASSOC; 3648 iwn_calib_reset(sc); 3649 3650 /* link LED always on while associated */ 3651 iwn_set_led(sc, IWN_LED_LINK, 0, 1); 3652 3653 return 0; 3654 #undef MS 3655 } 3656 3657 /* 3658 * Send a scan request to the firmware. Since this command is huge, we map it 3659 * into a mbuf instead of using the pre-allocated set of commands. 3660 */ 3661 int 3662 iwn_scan(struct iwn_softc *sc) 3663 { 3664 struct ifnet *ifp = sc->sc_ifp; 3665 struct ieee80211com *ic = ifp->if_l2com; 3666 struct ieee80211_scan_state *ss = ic->ic_scan; /*XXX*/ 3667 struct iwn_tx_ring *ring = &sc->txq[4]; 3668 struct iwn_tx_desc *desc; 3669 struct iwn_tx_data *data; 3670 struct iwn_tx_cmd *cmd; 3671 struct iwn_cmd_data *tx; 3672 struct iwn_scan_hdr *hdr; 3673 struct iwn_scan_essid *essid; 3674 struct iwn_scan_chan *chan; 3675 struct ieee80211_frame *wh; 3676 struct ieee80211_rateset *rs; 3677 struct ieee80211_channel *c; 3678 enum ieee80211_phymode mode; 3679 uint8_t *frm; 3680 int pktlen, error, nrates; 3681 bus_addr_t physaddr; 3682 3683 desc = &ring->desc[ring->cur]; 3684 data = &ring->data[ring->cur]; 3685 3686 /* XXX malloc */ 3687 data->m = m_getcl(M_DONTWAIT, MT_DATA, 0); 3688 if (data->m == NULL) { 3689 device_printf(sc->sc_dev, 3690 "%s: could not allocate mbuf for scan command\n", __func__); 3691 return ENOMEM; 3692 } 3693 3694 cmd = mtod(data->m, struct iwn_tx_cmd *); 3695 cmd->code = IWN_CMD_SCAN; 3696 cmd->flags = 0; 3697 cmd->qid = ring->qid; 3698 cmd->idx = ring->cur; 3699 3700 hdr = (struct iwn_scan_hdr *)cmd->data; 3701 memset(hdr, 0, sizeof (struct iwn_scan_hdr)); 3702 3703 /* XXX use scan state */ 3704 /* 3705 * Move to the next channel if no packets are received within 5 msecs 3706 * after sending the probe request (this helps to reduce the duration 3707 * of active scans). 3708 */ 3709 hdr->quiet = htole16(5); /* timeout in milliseconds */ 3710 hdr->plcp_threshold = htole16(1); /* min # of packets */ 3711 3712 /* select Ant B and Ant C for scanning */ 3713 hdr->rxchain = htole16(0x3e1 | (7 << IWN_RXCHAIN_VALID_S)); 3714 3715 tx = (struct iwn_cmd_data *)(hdr + 1); 3716 memset(tx, 0, sizeof (struct iwn_cmd_data)); 3717 tx->flags = htole32(IWN_TX_AUTO_SEQ | 0x200); /* XXX */ 3718 tx->id = IWN_ID_BROADCAST; 3719 tx->lifetime = htole32(IWN_LIFETIME_INFINITE); 3720 tx->rflags = IWN_RFLAG_ANT_B; 3721 3722 if (IEEE80211_IS_CHAN_A(ic->ic_curchan)) { 3723 hdr->crc_threshold = htole16(1); 3724 /* send probe requests at 6Mbps */ 3725 tx->rate = iwn_ridx_to_plcp[IWN_RATE_OFDM6]; 3726 } else { 3727 hdr->flags = htole32(IWN_CONFIG_24GHZ | IWN_CONFIG_AUTO); 3728 /* send probe requests at 1Mbps */ 3729 tx->rate = iwn_ridx_to_plcp[IWN_RATE_CCK1]; 3730 tx->rflags |= IWN_RFLAG_CCK; 3731 } 3732 3733 essid = (struct iwn_scan_essid *)(tx + 1); 3734 memset(essid, 0, 4 * sizeof (struct iwn_scan_essid)); 3735 essid[0].id = IEEE80211_ELEMID_SSID; 3736 essid[0].len = ss->ss_ssid[0].len; 3737 memcpy(essid[0].data, ss->ss_ssid[0].ssid, ss->ss_ssid[0].len); 3738 3739 /* 3740 * Build a probe request frame. Most of the following code is a 3741 * copy & paste of what is done in net80211. 3742 */ 3743 wh = (struct ieee80211_frame *)&essid[4]; 3744 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 3745 IEEE80211_FC0_SUBTYPE_PROBE_REQ; 3746 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 3747 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 3748 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 3749 IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr); 3750 *(u_int16_t *)&wh->i_dur[0] = 0; /* filled by h/w */ 3751 *(u_int16_t *)&wh->i_seq[0] = 0; /* filled by h/w */ 3752 3753 frm = (uint8_t *)(wh + 1); 3754 3755 /* add SSID IE */ 3756 *frm++ = IEEE80211_ELEMID_SSID; 3757 *frm++ = ss->ss_ssid[0].len; 3758 memcpy(frm, ss->ss_ssid[0].ssid, ss->ss_ssid[0].len); 3759 frm += ss->ss_ssid[0].len; 3760 3761 mode = ieee80211_chan2mode(ic->ic_curchan); 3762 rs = &ic->ic_sup_rates[mode]; 3763 3764 /* add supported rates IE */ 3765 *frm++ = IEEE80211_ELEMID_RATES; 3766 nrates = rs->rs_nrates; 3767 if (nrates > IEEE80211_RATE_SIZE) 3768 nrates = IEEE80211_RATE_SIZE; 3769 *frm++ = nrates; 3770 memcpy(frm, rs->rs_rates, nrates); 3771 frm += nrates; 3772 3773 /* add supported xrates IE */ 3774 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 3775 nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 3776 *frm++ = IEEE80211_ELEMID_XRATES; 3777 *frm++ = (uint8_t)nrates; 3778 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 3779 frm += nrates; 3780 } 3781 3782 /* setup length of probe request */ 3783 tx->len = htole16(frm - (uint8_t *)wh); 3784 3785 c = ic->ic_curchan; 3786 chan = (struct iwn_scan_chan *)frm; 3787 chan->chan = ieee80211_chan2ieee(ic, c); 3788 chan->flags = 0; 3789 if ((c->ic_flags & IEEE80211_CHAN_PASSIVE) == 0) { 3790 chan->flags |= IWN_CHAN_ACTIVE; 3791 if (ss->ss_nssid > 0) 3792 chan->flags |= IWN_CHAN_DIRECT; 3793 } 3794 chan->dsp_gain = 0x6e; 3795 if (IEEE80211_IS_CHAN_5GHZ(c)) { 3796 chan->rf_gain = 0x3b; 3797 chan->active = htole16(10); 3798 chan->passive = htole16(110); 3799 } else { 3800 chan->rf_gain = 0x28; 3801 chan->active = htole16(20); 3802 chan->passive = htole16(120); 3803 } 3804 3805 DPRINTF(sc, IWN_DEBUG_STATE, "%s: chan %u flags 0x%x rf_gain 0x%x " 3806 "dsp_gain 0x%x active 0x%x passive 0x%x\n", __func__, 3807 chan->chan, chan->flags, chan->rf_gain, chan->dsp_gain, 3808 chan->active, chan->passive); 3809 hdr->nchan++; 3810 chan++; 3811 3812 frm += sizeof (struct iwn_scan_chan); 3813 3814 hdr->len = htole16(frm - (uint8_t *)hdr); 3815 pktlen = frm - (uint8_t *)cmd; 3816 3817 error = bus_dmamap_load(ring->data_dmat, data->map, cmd, pktlen, 3818 iwn_dma_map_addr, &physaddr, BUS_DMA_NOWAIT); 3819 if (error != 0) { 3820 device_printf(sc->sc_dev, 3821 "%s: could not map scan command, error %d\n", 3822 __func__, error); 3823 m_freem(data->m); 3824 data->m = NULL; 3825 return error; 3826 } 3827 3828 IWN_SET_DESC_NSEGS(desc, 1); 3829 IWN_SET_DESC_SEG(desc, 0, physaddr, pktlen); 3830 sc->shared->len[ring->qid][ring->cur] = htole16(8); 3831 if (ring->cur < IWN_TX_WINDOW) 3832 sc->shared->len[ring->qid][ring->cur + IWN_TX_RING_COUNT] = 3833 htole16(8); 3834 3835 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 3836 BUS_DMASYNC_PREWRITE); 3837 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 3838 3839 /* kick cmd ring */ 3840 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT; 3841 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur); 3842 3843 return 0; /* will be notified async. of failure/success */ 3844 } 3845 3846 int 3847 iwn_config(struct iwn_softc *sc) 3848 { 3849 struct ifnet *ifp = sc->sc_ifp; 3850 struct ieee80211com *ic = ifp->if_l2com; 3851 struct iwn_power power; 3852 struct iwn_bluetooth bluetooth; 3853 struct iwn_node_info node; 3854 int error; 3855 3856 /* set power mode */ 3857 memset(&power, 0, sizeof power); 3858 power.flags = htole16(IWN_POWER_CAM | 0x8); 3859 DPRINTF(sc, IWN_DEBUG_RESET, "%s: set power mode\n", __func__); 3860 error = iwn_cmd(sc, IWN_CMD_SET_POWER_MODE, &power, sizeof power, 0); 3861 if (error != 0) { 3862 device_printf(sc->sc_dev, 3863 "%s: could not set power mode, error %d\n", 3864 __func__, error); 3865 return error; 3866 } 3867 3868 /* configure bluetooth coexistence */ 3869 memset(&bluetooth, 0, sizeof bluetooth); 3870 bluetooth.flags = 3; 3871 bluetooth.lead = 0xaa; 3872 bluetooth.kill = 1; 3873 DPRINTF(sc, IWN_DEBUG_RESET, "%s: config bluetooth coexistence\n", 3874 __func__); 3875 error = iwn_cmd(sc, IWN_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth, 3876 0); 3877 if (error != 0) { 3878 device_printf(sc->sc_dev, 3879 "%s: could not configure bluetooth coexistence, error %d\n", 3880 __func__, error); 3881 return error; 3882 } 3883 3884 /* configure adapter */ 3885 memset(&sc->config, 0, sizeof (struct iwn_config)); 3886 IEEE80211_ADDR_COPY(sc->config.myaddr, ic->ic_myaddr); 3887 IEEE80211_ADDR_COPY(sc->config.wlap, ic->ic_myaddr); 3888 /* set default channel */ 3889 sc->config.chan = htole16(ieee80211_chan2ieee(ic, ic->ic_curchan)); 3890 sc->config.flags = htole32(IWN_CONFIG_TSF); 3891 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) 3892 sc->config.flags |= htole32(IWN_CONFIG_AUTO | IWN_CONFIG_24GHZ); 3893 sc->config.filter = 0; 3894 switch (ic->ic_opmode) { 3895 case IEEE80211_M_STA: 3896 sc->config.mode = IWN_MODE_STA; 3897 sc->config.filter |= htole32(IWN_FILTER_MULTICAST); 3898 break; 3899 case IEEE80211_M_IBSS: 3900 case IEEE80211_M_AHDEMO: 3901 sc->config.mode = IWN_MODE_IBSS; 3902 break; 3903 case IEEE80211_M_HOSTAP: 3904 sc->config.mode = IWN_MODE_HOSTAP; 3905 break; 3906 case IEEE80211_M_MONITOR: 3907 sc->config.mode = IWN_MODE_MONITOR; 3908 sc->config.filter |= htole32(IWN_FILTER_MULTICAST | 3909 IWN_FILTER_CTL | IWN_FILTER_PROMISC); 3910 break; 3911 default: 3912 break; 3913 } 3914 sc->config.cck_mask = 0x0f; /* not yet negotiated */ 3915 sc->config.ofdm_mask = 0xff; /* not yet negotiated */ 3916 sc->config.ht_single_mask = 0xff; 3917 sc->config.ht_dual_mask = 0xff; 3918 sc->config.rxchain = htole16(0x2800 | (7 << IWN_RXCHAIN_VALID_S)); 3919 3920 DPRINTF(sc, IWN_DEBUG_STATE, 3921 "%s: config chan %d mode %d flags 0x%x cck 0x%x ofdm 0x%x " 3922 "ht_single 0x%x ht_dual 0x%x rxchain 0x%x " 3923 "myaddr %6D wlap %6D bssid %6D associd %d filter 0x%x\n", 3924 __func__, 3925 le16toh(sc->config.chan), sc->config.mode, le32toh(sc->config.flags), 3926 sc->config.cck_mask, sc->config.ofdm_mask, 3927 sc->config.ht_single_mask, sc->config.ht_dual_mask, 3928 le16toh(sc->config.rxchain), 3929 sc->config.myaddr, ":", sc->config.wlap, ":", sc->config.bssid, ":", 3930 le16toh(sc->config.associd), le32toh(sc->config.filter)); 3931 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->config, 3932 sizeof (struct iwn_config), 0); 3933 if (error != 0) { 3934 device_printf(sc->sc_dev, 3935 "%s: configure command failed, error %d\n", 3936 __func__, error); 3937 return error; 3938 } 3939 sc->sc_curchan = ic->ic_curchan; 3940 3941 /* configuration has changed, set Tx power accordingly */ 3942 error = iwn_set_txpower(sc, ic->ic_curchan, 0); 3943 if (error != 0) { 3944 device_printf(sc->sc_dev, 3945 "%s: could not set Tx power, error %d\n", __func__, error); 3946 return error; 3947 } 3948 3949 /* add broadcast node */ 3950 memset(&node, 0, sizeof node); 3951 IEEE80211_ADDR_COPY(node.macaddr, ic->ic_ifp->if_broadcastaddr); 3952 node.id = IWN_ID_BROADCAST; 3953 node.rate = iwn_plcp_signal(2); 3954 DPRINTF(sc, IWN_DEBUG_RESET, "%s: add broadcast node\n", __func__); 3955 error = iwn_cmd(sc, IWN_CMD_ADD_NODE, &node, sizeof node, 0); 3956 if (error != 0) { 3957 device_printf(sc->sc_dev, 3958 "%s: could not add broadcast node, error %d\n", 3959 __func__, error); 3960 return error; 3961 } 3962 error = iwn_set_link_quality(sc, node.id, ic->ic_curchan, 0); 3963 if (error != 0) { 3964 device_printf(sc->sc_dev, 3965 "%s: could not setup MRR for node %d, error %d\n", 3966 __func__, node.id, error); 3967 return error; 3968 } 3969 3970 error = iwn_set_critical_temp(sc); 3971 if (error != 0) { 3972 device_printf(sc->sc_dev, 3973 "%s: could not set critical temperature, error %d\n", 3974 __func__, error); 3975 return error; 3976 } 3977 return 0; 3978 } 3979 3980 /* 3981 * Do post-alive initialization of the NIC (after firmware upload). 3982 */ 3983 void 3984 iwn_post_alive(struct iwn_softc *sc) 3985 { 3986 uint32_t base; 3987 uint16_t offset; 3988 int qid; 3989 3990 iwn_mem_lock(sc); 3991 3992 /* clear SRAM */ 3993 base = iwn_mem_read(sc, IWN_SRAM_BASE); 3994 for (offset = 0x380; offset < 0x520; offset += 4) { 3995 IWN_WRITE(sc, IWN_MEM_WADDR, base + offset); 3996 IWN_WRITE(sc, IWN_MEM_WDATA, 0); 3997 } 3998 3999 /* shared area is aligned on a 1K boundary */ 4000 iwn_mem_write(sc, IWN_SRAM_BASE, sc->shared_dma.paddr >> 10); 4001 iwn_mem_write(sc, IWN_SELECT_QCHAIN, 0); 4002 4003 for (qid = 0; qid < IWN_NTXQUEUES; qid++) { 4004 iwn_mem_write(sc, IWN_QUEUE_RIDX(qid), 0); 4005 IWN_WRITE(sc, IWN_TX_WIDX, qid << 8 | 0); 4006 4007 /* set sched. window size */ 4008 IWN_WRITE(sc, IWN_MEM_WADDR, base + IWN_QUEUE_OFFSET(qid)); 4009 IWN_WRITE(sc, IWN_MEM_WDATA, 64); 4010 /* set sched. frame limit */ 4011 IWN_WRITE(sc, IWN_MEM_WADDR, base + IWN_QUEUE_OFFSET(qid) + 4); 4012 IWN_WRITE(sc, IWN_MEM_WDATA, 10 << 16); 4013 } 4014 4015 /* enable interrupts for all 16 queues */ 4016 iwn_mem_write(sc, IWN_QUEUE_INTR_MASK, 0xffff); 4017 4018 /* identify active Tx rings (0-7) */ 4019 iwn_mem_write(sc, IWN_TX_ACTIVE, 0xff); 4020 4021 /* mark Tx rings (4 EDCA + cmd + 2 HCCA) as active */ 4022 for (qid = 0; qid < 7; qid++) { 4023 iwn_mem_write(sc, IWN_TXQ_STATUS(qid), 4024 IWN_TXQ_STATUS_ACTIVE | qid << 1); 4025 } 4026 4027 iwn_mem_unlock(sc); 4028 } 4029 4030 void 4031 iwn_stop_master(struct iwn_softc *sc) 4032 { 4033 uint32_t tmp; 4034 int ntries; 4035 4036 tmp = IWN_READ(sc, IWN_RESET); 4037 IWN_WRITE(sc, IWN_RESET, tmp | IWN_STOP_MASTER); 4038 4039 tmp = IWN_READ(sc, IWN_GPIO_CTL); 4040 if ((tmp & IWN_GPIO_PWR_STATUS) == IWN_GPIO_PWR_SLEEP) 4041 return; /* already asleep */ 4042 4043 for (ntries = 0; ntries < 100; ntries++) { 4044 if (IWN_READ(sc, IWN_RESET) & IWN_MASTER_DISABLED) 4045 break; 4046 DELAY(10); 4047 } 4048 if (ntries == 100) 4049 device_printf(sc->sc_dev, 4050 "%s: timeout waiting for master\n", __func__); 4051 } 4052 4053 int 4054 iwn_reset(struct iwn_softc *sc) 4055 { 4056 uint32_t tmp; 4057 int ntries; 4058 4059 /* clear any pending interrupts */ 4060 IWN_WRITE(sc, IWN_INTR, 0xffffffff); 4061 4062 tmp = IWN_READ(sc, IWN_CHICKEN); 4063 IWN_WRITE(sc, IWN_CHICKEN, tmp | IWN_CHICKEN_DISLOS); 4064 4065 tmp = IWN_READ(sc, IWN_GPIO_CTL); 4066 IWN_WRITE(sc, IWN_GPIO_CTL, tmp | IWN_GPIO_INIT); 4067 4068 /* wait for clock stabilization */ 4069 for (ntries = 0; ntries < 1000; ntries++) { 4070 if (IWN_READ(sc, IWN_GPIO_CTL) & IWN_GPIO_CLOCK) 4071 break; 4072 DELAY(10); 4073 } 4074 if (ntries == 1000) { 4075 device_printf(sc->sc_dev, 4076 "%s: timeout waiting for clock stabilization\n", __func__); 4077 return ETIMEDOUT; 4078 } 4079 return 0; 4080 } 4081 4082 void 4083 iwn_hw_config(struct iwn_softc *sc) 4084 { 4085 uint32_t tmp, hw; 4086 4087 /* enable interrupts mitigation */ 4088 IWN_WRITE(sc, IWN_INTR_MIT, 512 / 32); 4089 4090 /* voodoo from the reference driver */ 4091 tmp = pci_read_config(sc->sc_dev, PCIR_REVID,1); 4092 if ((tmp & 0x80) && (tmp & 0x7f) < 8) { 4093 /* enable "no snoop" field */ 4094 tmp = pci_read_config(sc->sc_dev, 0xe8, 1); 4095 tmp &= ~IWN_DIS_NOSNOOP; 4096 /* clear device specific PCI configuration register 0x41 */ 4097 pci_write_config(sc->sc_dev, 0xe8, tmp, 1); 4098 } 4099 4100 /* disable L1 entry to work around a hardware bug */ 4101 tmp = pci_read_config(sc->sc_dev, 0xf0, 1); 4102 tmp &= ~IWN_ENA_L1; 4103 pci_write_config(sc->sc_dev, 0xf0, tmp, 1 ); 4104 4105 hw = IWN_READ(sc, IWN_HWCONFIG); 4106 IWN_WRITE(sc, IWN_HWCONFIG, hw | 0x310); 4107 4108 iwn_mem_lock(sc); 4109 tmp = iwn_mem_read(sc, IWN_MEM_POWER); 4110 iwn_mem_write(sc, IWN_MEM_POWER, tmp | IWN_POWER_RESET); 4111 DELAY(5); 4112 tmp = iwn_mem_read(sc, IWN_MEM_POWER); 4113 iwn_mem_write(sc, IWN_MEM_POWER, tmp & ~IWN_POWER_RESET); 4114 iwn_mem_unlock(sc); 4115 } 4116 4117 void 4118 iwn_init_locked(struct iwn_softc *sc) 4119 { 4120 struct ifnet *ifp = sc->sc_ifp; 4121 uint32_t tmp; 4122 int error, qid; 4123 4124 IWN_LOCK_ASSERT(sc); 4125 4126 /* load the firmware */ 4127 if (sc->fw_fp == NULL && (error = iwn_load_firmware(sc)) != 0) { 4128 device_printf(sc->sc_dev, 4129 "%s: could not load firmware, error %d\n", __func__, error); 4130 return; 4131 } 4132 4133 error = iwn_reset(sc); 4134 if (error != 0) { 4135 device_printf(sc->sc_dev, 4136 "%s: could not reset adapter, error %d\n", __func__, error); 4137 return; 4138 } 4139 4140 iwn_mem_lock(sc); 4141 iwn_mem_read(sc, IWN_CLOCK_CTL); 4142 iwn_mem_write(sc, IWN_CLOCK_CTL, 0xa00); 4143 iwn_mem_read(sc, IWN_CLOCK_CTL); 4144 iwn_mem_unlock(sc); 4145 4146 DELAY(20); 4147 4148 iwn_mem_lock(sc); 4149 tmp = iwn_mem_read(sc, IWN_MEM_PCIDEV); 4150 iwn_mem_write(sc, IWN_MEM_PCIDEV, tmp | 0x800); 4151 iwn_mem_unlock(sc); 4152 4153 iwn_mem_lock(sc); 4154 tmp = iwn_mem_read(sc, IWN_MEM_POWER); 4155 iwn_mem_write(sc, IWN_MEM_POWER, tmp & ~0x03000000); 4156 iwn_mem_unlock(sc); 4157 4158 iwn_hw_config(sc); 4159 4160 /* init Rx ring */ 4161 iwn_mem_lock(sc); 4162 IWN_WRITE(sc, IWN_RX_CONFIG, 0); 4163 IWN_WRITE(sc, IWN_RX_WIDX, 0); 4164 /* Rx ring is aligned on a 256-byte boundary */ 4165 IWN_WRITE(sc, IWN_RX_BASE, sc->rxq.desc_dma.paddr >> 8); 4166 /* shared area is aligned on a 16-byte boundary */ 4167 IWN_WRITE(sc, IWN_RW_WIDX_PTR, (sc->shared_dma.paddr + 4168 offsetof(struct iwn_shared, closed_count)) >> 4); 4169 IWN_WRITE(sc, IWN_RX_CONFIG, 0x80601000); 4170 iwn_mem_unlock(sc); 4171 4172 IWN_WRITE(sc, IWN_RX_WIDX, (IWN_RX_RING_COUNT - 1) & ~7); 4173 4174 iwn_mem_lock(sc); 4175 iwn_mem_write(sc, IWN_TX_ACTIVE, 0); 4176 4177 /* set physical address of "keep warm" page */ 4178 IWN_WRITE(sc, IWN_KW_BASE, sc->kw_dma.paddr >> 4); 4179 4180 /* init Tx rings */ 4181 for (qid = 0; qid < IWN_NTXQUEUES; qid++) { 4182 struct iwn_tx_ring *txq = &sc->txq[qid]; 4183 IWN_WRITE(sc, IWN_TX_BASE(qid), txq->desc_dma.paddr >> 8); 4184 IWN_WRITE(sc, IWN_TX_CONFIG(qid), 0x80000008); 4185 } 4186 iwn_mem_unlock(sc); 4187 4188 /* clear "radio off" and "disable command" bits (reversed logic) */ 4189 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_RADIO_OFF); 4190 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_DISABLE_CMD); 4191 4192 /* clear any pending interrupts */ 4193 IWN_WRITE(sc, IWN_INTR, 0xffffffff); 4194 /* enable interrupts */ 4195 IWN_WRITE(sc, IWN_MASK, IWN_INTR_MASK); 4196 4197 /* not sure why/if this is necessary... */ 4198 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_RADIO_OFF); 4199 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_RADIO_OFF); 4200 4201 /* check that the radio is not disabled by RF switch */ 4202 if (!(IWN_READ(sc, IWN_GPIO_CTL) & IWN_GPIO_RF_ENABLED)) { 4203 device_printf(sc->sc_dev, 4204 "radio is disabled by hardware switch\n"); 4205 return; 4206 } 4207 4208 error = iwn_transfer_firmware(sc); 4209 if (error != 0) { 4210 device_printf(sc->sc_dev, 4211 "%s: could not load firmware, error %d\n", __func__, error); 4212 return; 4213 } 4214 4215 /* firmware has notified us that it is alive.. */ 4216 iwn_post_alive(sc); /* ..do post alive initialization */ 4217 4218 sc->rawtemp = sc->ucode_info.temp[3].chan20MHz; 4219 sc->temp = iwn_get_temperature(sc); 4220 DPRINTF(sc, IWN_DEBUG_RESET, "%s: temperature=%d\n", 4221 __func__, sc->temp); 4222 4223 error = iwn_config(sc); 4224 if (error != 0) { 4225 device_printf(sc->sc_dev, 4226 "%s: could not configure device, error %d\n", 4227 __func__, error); 4228 return; 4229 } 4230 4231 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 4232 ifp->if_drv_flags |= IFF_DRV_RUNNING; 4233 } 4234 4235 void 4236 iwn_init(void *arg) 4237 { 4238 struct iwn_softc *sc = arg; 4239 struct ifnet *ifp = sc->sc_ifp; 4240 struct ieee80211com *ic = ifp->if_l2com; 4241 4242 IWN_LOCK(sc); 4243 iwn_init_locked(sc); 4244 IWN_UNLOCK(sc); 4245 4246 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 4247 ieee80211_start_all(ic); 4248 } 4249 4250 void 4251 iwn_stop_locked(struct iwn_softc *sc) 4252 { 4253 struct ifnet *ifp = sc->sc_ifp; 4254 uint32_t tmp; 4255 int i; 4256 4257 IWN_LOCK_ASSERT(sc); 4258 4259 IWN_WRITE(sc, IWN_RESET, IWN_NEVO_RESET); 4260 4261 sc->sc_tx_timer = 0; 4262 callout_stop(&sc->sc_timer_to); 4263 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 4264 4265 /* disable interrupts */ 4266 IWN_WRITE(sc, IWN_MASK, 0); 4267 IWN_WRITE(sc, IWN_INTR, 0xffffffff); 4268 IWN_WRITE(sc, IWN_INTR_STATUS, 0xffffffff); 4269 4270 /* Clear any commands left in the taskq command buffer */ 4271 memset(sc->sc_cmd, 0, sizeof(sc->sc_cmd)); 4272 4273 /* reset all Tx rings */ 4274 for (i = 0; i < IWN_NTXQUEUES; i++) 4275 iwn_reset_tx_ring(sc, &sc->txq[i]); 4276 4277 /* reset Rx ring */ 4278 iwn_reset_rx_ring(sc, &sc->rxq); 4279 4280 iwn_mem_lock(sc); 4281 iwn_mem_write(sc, IWN_MEM_CLOCK2, 0x200); 4282 iwn_mem_unlock(sc); 4283 4284 DELAY(5); 4285 iwn_stop_master(sc); 4286 4287 tmp = IWN_READ(sc, IWN_RESET); 4288 IWN_WRITE(sc, IWN_RESET, tmp | IWN_SW_RESET); 4289 } 4290 4291 void 4292 iwn_stop(struct iwn_softc *sc) 4293 { 4294 IWN_LOCK(sc); 4295 iwn_stop_locked(sc); 4296 IWN_UNLOCK(sc); 4297 } 4298 4299 /* 4300 * Callback from net80211 to start a scan. 4301 */ 4302 static void 4303 iwn_scan_start(struct ieee80211com *ic) 4304 { 4305 struct ifnet *ifp = ic->ic_ifp; 4306 struct iwn_softc *sc = ifp->if_softc; 4307 4308 iwn_queue_cmd(sc, IWN_SCAN_START, 0, IWN_QUEUE_NORMAL); 4309 } 4310 4311 /* 4312 * Callback from net80211 to terminate a scan. 4313 */ 4314 static void 4315 iwn_scan_end(struct ieee80211com *ic) 4316 { 4317 struct ifnet *ifp = ic->ic_ifp; 4318 struct iwn_softc *sc = ifp->if_softc; 4319 4320 iwn_queue_cmd(sc, IWN_SCAN_STOP, 0, IWN_QUEUE_NORMAL); 4321 } 4322 4323 /* 4324 * Callback from net80211 to force a channel change. 4325 */ 4326 static void 4327 iwn_set_channel(struct ieee80211com *ic) 4328 { 4329 struct ifnet *ifp = ic->ic_ifp; 4330 struct iwn_softc *sc = ifp->if_softc; 4331 const struct ieee80211_channel *c = ic->ic_curchan; 4332 4333 if (c != sc->sc_curchan) { 4334 sc->sc_rxtap.wr_chan_freq = htole16(c->ic_freq); 4335 sc->sc_rxtap.wr_chan_flags = htole16(c->ic_flags); 4336 sc->sc_txtap.wt_chan_freq = htole16(c->ic_freq); 4337 sc->sc_txtap.wt_chan_flags = htole16(c->ic_flags); 4338 iwn_queue_cmd(sc, IWN_SET_CHAN, 0, IWN_QUEUE_NORMAL); 4339 } 4340 } 4341 4342 /* 4343 * Callback from net80211 to start scanning of the current channel. 4344 */ 4345 static void 4346 iwn_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell) 4347 { 4348 struct ieee80211vap *vap = ss->ss_vap; 4349 struct iwn_softc *sc = vap->iv_ic->ic_ifp->if_softc; 4350 4351 iwn_queue_cmd(sc, IWN_SCAN_CURCHAN, 0, IWN_QUEUE_NORMAL); 4352 } 4353 4354 /* 4355 * Callback from net80211 to handle the minimum dwell time being met. 4356 * The intent is to terminate the scan but we just let the firmware 4357 * notify us when it's finished as we have no safe way to abort it. 4358 */ 4359 static void 4360 iwn_scan_mindwell(struct ieee80211_scan_state *ss) 4361 { 4362 /* NB: don't try to abort scan; wait for firmware to finish */ 4363 } 4364 4365 /* 4366 * Carry out work in the taskq context. 4367 */ 4368 static void 4369 iwn_ops(void *arg0, int pending) 4370 { 4371 struct iwn_softc *sc = arg0; 4372 struct ifnet *ifp = sc->sc_ifp; 4373 struct ieee80211com *ic = ifp->if_l2com; 4374 struct ieee80211vap *vap; 4375 int cmd, arg, error; 4376 enum ieee80211_state nstate; 4377 4378 for (;;) { 4379 IWN_CMD_LOCK(sc); 4380 cmd = sc->sc_cmd[sc->sc_cmd_cur]; 4381 if (cmd == 0) { 4382 /* No more commands to process */ 4383 IWN_CMD_UNLOCK(sc); 4384 return; 4385 } 4386 if ((sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING) == 0 && 4387 cmd != IWN_RADIO_ENABLE ) { 4388 IWN_CMD_UNLOCK(sc); 4389 return; 4390 } 4391 arg = sc->sc_cmd_arg[sc->sc_cmd_cur]; 4392 sc->sc_cmd[sc->sc_cmd_cur] = 0; /* free the slot */ 4393 sc->sc_cmd_cur = (sc->sc_cmd_cur + 1) % IWN_CMD_MAXOPS; 4394 IWN_CMD_UNLOCK(sc); 4395 4396 IWN_LOCK(sc); /* NB: sync debug printfs on smp */ 4397 DPRINTF(sc, IWN_DEBUG_OPS, "%s: %s (cmd 0x%x)\n", 4398 __func__, iwn_ops_str(cmd), cmd); 4399 4400 vap = TAILQ_FIRST(&ic->ic_vaps); /* XXX */ 4401 switch (cmd) { 4402 case IWN_SCAN_START: 4403 /* make the link LED blink while we're scanning */ 4404 iwn_set_led(sc, IWN_LED_LINK, 20, 2); 4405 break; 4406 case IWN_SCAN_STOP: 4407 break; 4408 case IWN_SCAN_NEXT: 4409 ieee80211_scan_next(vap); 4410 break; 4411 case IWN_SCAN_CURCHAN: 4412 error = iwn_scan(sc); 4413 if (error != 0) { 4414 IWN_UNLOCK(sc); 4415 ieee80211_cancel_scan(vap); 4416 IWN_LOCK(sc); 4417 return; 4418 } 4419 break; 4420 case IWN_SET_CHAN: 4421 error = iwn_config(sc); 4422 if (error != 0) { 4423 DPRINTF(sc, IWN_DEBUG_STATE, 4424 "%s: set chan failed, cancel scan\n", 4425 __func__); 4426 IWN_UNLOCK(sc); 4427 //XXX Handle failed scan correctly 4428 ieee80211_cancel_scan(vap); 4429 return; 4430 } 4431 break; 4432 case IWN_AUTH: 4433 case IWN_RUN: 4434 if (cmd == IWN_AUTH) { 4435 error = iwn_auth(sc); 4436 nstate = IEEE80211_S_AUTH; 4437 } else { 4438 error = iwn_run(sc); 4439 nstate = IEEE80211_S_RUN; 4440 } 4441 if (error == 0) { 4442 IWN_UNLOCK(sc); 4443 IEEE80211_LOCK(ic); 4444 IWN_VAP(vap)->iv_newstate(vap, nstate, arg); 4445 if (vap->iv_newstate_cb != NULL) 4446 vap->iv_newstate_cb(vap, nstate, arg); 4447 IEEE80211_UNLOCK(ic); 4448 IWN_LOCK(sc); 4449 } else { 4450 device_printf(sc->sc_dev, 4451 "%s: %s state change failed, error %d\n", 4452 __func__, ieee80211_state_name[nstate], 4453 error); 4454 } 4455 break; 4456 case IWN_REINIT: 4457 IWN_UNLOCK(sc); 4458 iwn_init(sc); 4459 IWN_LOCK(sc); 4460 ieee80211_notify_radio(ic, 1); 4461 break; 4462 case IWN_RADIO_ENABLE: 4463 KASSERT(sc->fw_fp != NULL, 4464 ("Fware Not Loaded, can't load from tq")); 4465 IWN_UNLOCK(sc); 4466 iwn_init(sc); 4467 IWN_LOCK(sc); 4468 break; 4469 case IWN_RADIO_DISABLE: 4470 ieee80211_notify_radio(ic, 0); 4471 iwn_stop_locked(sc); 4472 break; 4473 } 4474 IWN_UNLOCK(sc); 4475 } 4476 } 4477 4478 /* 4479 * Queue a command for execution in the taskq thread. 4480 * This is needed as the net80211 callbacks do not allow 4481 * sleeping, since we need to sleep to confirm commands have 4482 * been processed by the firmware, we must defer execution to 4483 * a sleep enabled thread. 4484 */ 4485 static int 4486 iwn_queue_cmd(struct iwn_softc *sc, int cmd, int arg, int clear) 4487 { 4488 IWN_CMD_LOCK(sc); 4489 if (clear) { 4490 sc->sc_cmd[0] = cmd; 4491 sc->sc_cmd_arg[0] = arg; 4492 sc->sc_cmd_cur = 0; 4493 sc->sc_cmd_next = 1; 4494 } else { 4495 if (sc->sc_cmd[sc->sc_cmd_next] != 0) { 4496 IWN_CMD_UNLOCK(sc); 4497 DPRINTF(sc, IWN_DEBUG_ANY, "%s: command %d dropped\n", 4498 __func__, cmd); 4499 return EBUSY; 4500 } 4501 sc->sc_cmd[sc->sc_cmd_next] = cmd; 4502 sc->sc_cmd_arg[sc->sc_cmd_next] = arg; 4503 sc->sc_cmd_next = (sc->sc_cmd_next + 1) % IWN_CMD_MAXOPS; 4504 } 4505 taskqueue_enqueue(sc->sc_tq, &sc->sc_ops_task); 4506 IWN_CMD_UNLOCK(sc); 4507 return 0; 4508 } 4509 4510 static void 4511 iwn_bpfattach(struct iwn_softc *sc) 4512 { 4513 struct ifnet *ifp = sc->sc_ifp; 4514 4515 bpfattach(ifp, DLT_IEEE802_11_RADIO, 4516 sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap)); 4517 4518 sc->sc_rxtap_len = sizeof sc->sc_rxtap; 4519 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 4520 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWN_RX_RADIOTAP_PRESENT); 4521 4522 sc->sc_txtap_len = sizeof sc->sc_txtap; 4523 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 4524 sc->sc_txtap.wt_ihdr.it_present = htole32(IWN_TX_RADIOTAP_PRESENT); 4525 } 4526 4527 static void 4528 iwn_sysctlattach(struct iwn_softc *sc) 4529 { 4530 struct sysctl_ctx_list *ctx = device_get_sysctl_ctx(sc->sc_dev); 4531 struct sysctl_oid *tree = device_get_sysctl_tree(sc->sc_dev); 4532 4533 #ifdef IWN_DEBUG 4534 sc->sc_debug = 0; 4535 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 4536 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "control debugging printfs"); 4537 #endif 4538 } 4539 4540 #ifdef IWN_DEBUG 4541 static const char * 4542 iwn_ops_str(int cmd) 4543 { 4544 switch (cmd) { 4545 case IWN_SCAN_START: return "SCAN_START"; 4546 case IWN_SCAN_CURCHAN: return "SCAN_CURCHAN"; 4547 case IWN_SCAN_STOP: return "SCAN_STOP"; 4548 case IWN_SET_CHAN: return "SET_CHAN"; 4549 case IWN_AUTH: return "AUTH"; 4550 case IWN_SCAN_NEXT: return "SCAN_NEXT"; 4551 case IWN_RUN: return "RUN"; 4552 case IWN_RADIO_ENABLE: return "RADIO_ENABLE"; 4553 case IWN_RADIO_DISABLE: return "RADIO_DISABLE"; 4554 case IWN_REINIT: return "REINIT"; 4555 } 4556 return "UNKNOWN COMMAND"; 4557 } 4558 4559 static const char * 4560 iwn_intr_str(uint8_t cmd) 4561 { 4562 switch (cmd) { 4563 /* Notifications */ 4564 case IWN_UC_READY: return "UC_READY"; 4565 case IWN_ADD_NODE_DONE: return "ADD_NODE_DONE"; 4566 case IWN_TX_DONE: return "TX_DONE"; 4567 case IWN_START_SCAN: return "START_SCAN"; 4568 case IWN_STOP_SCAN: return "STOP_SCAN"; 4569 case IWN_RX_STATISTICS: return "RX_STATS"; 4570 case IWN_BEACON_STATISTICS: return "BEACON_STATS"; 4571 case IWN_STATE_CHANGED: return "STATE_CHANGED"; 4572 case IWN_BEACON_MISSED: return "BEACON_MISSED"; 4573 case IWN_AMPDU_RX_START: return "AMPDU_RX_START"; 4574 case IWN_AMPDU_RX_DONE: return "AMPDU_RX_DONE"; 4575 case IWN_RX_DONE: return "RX_DONE"; 4576 4577 /* Command Notifications */ 4578 case IWN_CMD_CONFIGURE: return "IWN_CMD_CONFIGURE"; 4579 case IWN_CMD_ASSOCIATE: return "IWN_CMD_ASSOCIATE"; 4580 case IWN_CMD_EDCA_PARAMS: return "IWN_CMD_EDCA_PARAMS"; 4581 case IWN_CMD_TSF: return "IWN_CMD_TSF"; 4582 case IWN_CMD_TX_LINK_QUALITY: return "IWN_CMD_TX_LINK_QUALITY"; 4583 case IWN_CMD_SET_LED: return "IWN_CMD_SET_LED"; 4584 case IWN_CMD_SET_POWER_MODE: return "IWN_CMD_SET_POWER_MODE"; 4585 case IWN_CMD_SCAN: return "IWN_CMD_SCAN"; 4586 case IWN_CMD_TXPOWER: return "IWN_CMD_TXPOWER"; 4587 case IWN_CMD_BLUETOOTH: return "IWN_CMD_BLUETOOTH"; 4588 case IWN_CMD_SET_CRITICAL_TEMP: return "IWN_CMD_SET_CRITICAL_TEMP"; 4589 case IWN_SENSITIVITY: return "IWN_SENSITIVITY"; 4590 case IWN_PHY_CALIB: return "IWN_PHY_CALIB"; 4591 } 4592 return "UNKNOWN INTR NOTIF/CMD"; 4593 } 4594 #endif /* IWN_DEBUG */ 4595 4596 static device_method_t iwn_methods[] = { 4597 /* Device interface */ 4598 DEVMETHOD(device_probe, iwn_probe), 4599 DEVMETHOD(device_attach, iwn_attach), 4600 DEVMETHOD(device_detach, iwn_detach), 4601 DEVMETHOD(device_shutdown, iwn_shutdown), 4602 DEVMETHOD(device_suspend, iwn_suspend), 4603 DEVMETHOD(device_resume, iwn_resume), 4604 4605 { 0, 0 } 4606 }; 4607 4608 static driver_t iwn_driver = { 4609 "iwn", 4610 iwn_methods, 4611 sizeof (struct iwn_softc) 4612 }; 4613 static devclass_t iwn_devclass; 4614 DRIVER_MODULE(iwn, pci, iwn_driver, iwn_devclass, 0, 0); 4615 MODULE_DEPEND(iwn, pci, 1, 1, 1); 4616 MODULE_DEPEND(iwn, firmware, 1, 1, 1); 4617 MODULE_DEPEND(iwn, wlan, 1, 1, 1); 4618 MODULE_DEPEND(iwn, wlan_amrr, 1, 1, 1); 4619