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