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