1 /* $NetBSD: if_iwn.c,v 1.10 2008/05/10 12:56:28 degroote Exp $ */ 2 3 /*- 4 * Copyright (c) 2007 5 * Damien Bergamini <damien.bergamini@free.fr> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20 #include <sys/cdefs.h> 21 __KERNEL_RCSID(0, "$NetBSD: if_iwn.c,v 1.10 2008/05/10 12:56:28 degroote Exp $"); 22 23 24 /* 25 * Driver for Intel Wireless WiFi Link 4965AGN 802.11 network adapters. 26 */ 27 28 #include "bpfilter.h" 29 30 #include <sys/param.h> 31 #include <sys/sockio.h> 32 #include <sys/sysctl.h> 33 #include <sys/mbuf.h> 34 #include <sys/kernel.h> 35 #include <sys/socket.h> 36 #include <sys/systm.h> 37 #include <sys/malloc.h> 38 #include <sys/conf.h> 39 #include <sys/kauth.h> 40 #include <sys/callout.h> 41 42 #include <machine/bus.h> 43 #include <machine/endian.h> 44 #include <machine/intr.h> 45 46 #include <dev/pci/pcireg.h> 47 #include <dev/pci/pcivar.h> 48 #include <dev/pci/pcidevs.h> 49 50 #if NBPFILTER > 0 51 #include <net/bpf.h> 52 #endif 53 #include <net/if.h> 54 #include <net/if_arp.h> 55 #include <net/if_dl.h> 56 #include <net/if_media.h> 57 #include <net/if_types.h> 58 59 #include <netinet/in.h> 60 #include <netinet/in_systm.h> 61 #include <netinet/in_var.h> 62 #include <net/if_ether.h> 63 #include <netinet/ip.h> 64 65 #include <net80211/ieee80211_var.h> 66 #include <net80211/ieee80211_amrr.h> 67 #include <net80211/ieee80211_radiotap.h> 68 69 #include <dev/firmload.h> 70 71 #include <dev/pci/if_iwnreg.h> 72 #include <dev/pci/if_iwnvar.h> 73 74 #if 0 75 static const struct pci_matchid iwn_devices[] = { 76 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_4965AGN_1 }, 77 { PCI_VENDOR_INTEL, PCI_PRODUCT_INTEL_PRO_WL_4965AGN_2 } 78 }; 79 #endif 80 81 /* 82 * Supported rates for 802.11a/b/g modes (in 500Kbps unit). 83 */ 84 static const struct ieee80211_rateset iwn_rateset_11a = 85 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } }; 86 87 static const struct ieee80211_rateset iwn_rateset_11b = 88 { 4, { 2, 4, 11, 22 } }; 89 90 static const struct ieee80211_rateset iwn_rateset_11g = 91 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 92 93 94 #define EDCA_NUM_AC 4 95 static int iwn_match(device_t , struct cfdata *, void *); 96 static void iwn_attach(device_t , device_t, void *); 97 static int iwn_detach(device_t, int); 98 99 static void iwn_radiotap_attach(struct iwn_softc *); 100 static int iwn_dma_contig_alloc(bus_dma_tag_t, struct iwn_dma_info *, 101 void **, bus_size_t, bus_size_t, int); 102 static void iwn_dma_contig_free(struct iwn_dma_info *); 103 static int iwn_alloc_shared(struct iwn_softc *); 104 static void iwn_free_shared(struct iwn_softc *); 105 static int iwn_alloc_kw(struct iwn_softc *); 106 static void iwn_free_kw(struct iwn_softc *); 107 static int iwn_alloc_fwmem(struct iwn_softc *); 108 static void iwn_free_fwmem(struct iwn_softc *); 109 static struct iwn_rbuf *iwn_alloc_rbuf(struct iwn_softc *); 110 static void iwn_free_rbuf(struct mbuf *, void *, size_t, void *); 111 static int iwn_alloc_rpool(struct iwn_softc *); 112 static void iwn_free_rpool(struct iwn_softc *); 113 static int iwn_alloc_rx_ring(struct iwn_softc *, struct iwn_rx_ring *); 114 static void iwn_reset_rx_ring(struct iwn_softc *, struct iwn_rx_ring *); 115 static void iwn_free_rx_ring(struct iwn_softc *, struct iwn_rx_ring *); 116 static int iwn_alloc_tx_ring(struct iwn_softc *, struct iwn_tx_ring *, 117 int, int); 118 static void iwn_reset_tx_ring(struct iwn_softc *, struct iwn_tx_ring *); 119 static void iwn_free_tx_ring(struct iwn_softc *, struct iwn_tx_ring *); 120 static struct ieee80211_node *iwn_node_alloc(struct ieee80211_node_table *); 121 static void iwn_newassoc(struct ieee80211_node *, int); 122 static int iwn_media_change(struct ifnet *); 123 static int iwn_newstate(struct ieee80211com *, enum ieee80211_state, int); 124 static void iwn_mem_lock(struct iwn_softc *); 125 static void iwn_mem_unlock(struct iwn_softc *); 126 static uint32_t iwn_mem_read(struct iwn_softc *, uint32_t); 127 static void iwn_mem_write(struct iwn_softc *, uint32_t, uint32_t); 128 static void iwn_mem_write_region_4(struct iwn_softc *, uint32_t, 129 const uint32_t *, int); 130 static int iwn_eeprom_lock(struct iwn_softc *); 131 static void iwn_eeprom_unlock(struct iwn_softc *); 132 static int iwn_read_prom_data(struct iwn_softc *, uint32_t, void *, int); 133 static int iwn_load_microcode(struct iwn_softc *, const uint8_t *, int); 134 static int iwn_load_firmware(struct iwn_softc *); 135 static void iwn_calib_timeout(void *); 136 static void iwn_iter_func(void *, struct ieee80211_node *); 137 static void iwn_ampdu_rx_start(struct iwn_softc *, struct iwn_rx_desc *); 138 static void iwn_rx_intr(struct iwn_softc *, struct iwn_rx_desc *, 139 struct iwn_rx_data *); 140 static void iwn_rx_statistics(struct iwn_softc *, struct iwn_rx_desc *); 141 static void iwn_tx_intr(struct iwn_softc *, struct iwn_rx_desc *); 142 static void iwn_cmd_intr(struct iwn_softc *, struct iwn_rx_desc *); 143 static void iwn_notif_intr(struct iwn_softc *); 144 static int iwn_intr(void *); 145 static void iwn_read_eeprom(struct iwn_softc *); 146 static void iwn_read_eeprom_channels(struct iwn_softc *, int); 147 static void iwn_print_power_group(struct iwn_softc *, int); 148 static uint8_t iwn_plcp_signal(int); 149 static int iwn_tx_data(struct iwn_softc *, struct mbuf *, 150 struct ieee80211_node *, int); 151 static void iwn_start(struct ifnet *); 152 static void iwn_watchdog(struct ifnet *); 153 static int iwn_ioctl(struct ifnet *, u_long, void *); 154 static int iwn_cmd(struct iwn_softc *, int, const void *, int, int); 155 static int iwn_wme_update(struct ieee80211com *); 156 static int iwn_setup_node_mrr(struct iwn_softc *, uint8_t, int); 157 static void iwn_set_led(struct iwn_softc *, uint8_t, uint8_t, uint8_t); 158 static int iwn_set_critical_temp(struct iwn_softc *); 159 static void iwn_enable_tsf(struct iwn_softc *, struct ieee80211_node *); 160 static void iwn_power_calibration(struct iwn_softc *, int); 161 static int iwn_set_txpower(struct iwn_softc *, 162 struct ieee80211_channel *, int); 163 static int iwn_get_rssi(const struct iwn_rx_stat *); 164 static int iwn_get_noise(const struct iwn_rx_general_stats *); 165 static int iwn_get_temperature(struct iwn_softc *); 166 static int iwn_init_sensitivity(struct iwn_softc *); 167 static void iwn_compute_differential_gain(struct iwn_softc *, 168 const struct iwn_rx_general_stats *); 169 static void iwn_tune_sensitivity(struct iwn_softc *, 170 const struct iwn_rx_stats *); 171 static int iwn_send_sensitivity(struct iwn_softc *); 172 /*static int iwn_setup_beacon(struct iwn_softc *, struct ieee80211_node *);*/ 173 static int iwn_auth(struct iwn_softc *); 174 static int iwn_run(struct iwn_softc *); 175 static int iwn_scan(struct iwn_softc *, uint16_t); 176 static int iwn_config(struct iwn_softc *); 177 static void iwn_post_alive(struct iwn_softc *); 178 static void iwn_stop_master(struct iwn_softc *); 179 static int iwn_reset(struct iwn_softc *); 180 static void iwn_hw_config(struct iwn_softc *); 181 static int iwn_init(struct ifnet *); 182 static void iwn_stop(struct ifnet *, int); 183 static void iwn_fix_channel(struct ieee80211com *, struct mbuf *); 184 static bool iwn_resume(device_t PMF_FN_PROTO); 185 186 187 188 #define IWN_DEBUG 189 190 #ifdef IWN_DEBUG 191 #define DPRINTF(x) do { if (iwn_debug > 0) printf x; } while (0) 192 #define DPRINTFN(n, x) do { if (iwn_debug >= (n)) printf x; } while (0) 193 int iwn_debug = 2; 194 #else 195 #define DPRINTF(x) 196 #define DPRINTFN(n, x) 197 #endif 198 199 CFATTACH_DECL_NEW(iwn, sizeof(struct iwn_softc), iwn_match, iwn_attach, 200 iwn_detach, NULL); 201 202 static int 203 iwn_match(device_t parent, struct cfdata *match __unused, void *aux) 204 { 205 struct pci_attach_args *pa = aux; 206 207 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL) 208 return 0; 209 210 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_4965AGN_1 || 211 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_4965AGN_2) 212 return 1; 213 214 return 0; 215 } 216 217 /* Base Address Register */ 218 #define IWN_PCI_BAR0 0x10 219 220 static void 221 iwn_attach(device_t parent __unused, device_t self, void *aux) 222 { 223 struct iwn_softc *sc = device_private(self); 224 struct ieee80211com *ic = &sc->sc_ic; 225 struct ifnet *ifp = &sc->sc_ec.ec_if; 226 struct pci_attach_args *pa = aux; 227 const char *intrstr; 228 char devinfo[256]; 229 pci_intr_handle_t ih; 230 pcireg_t memtype, data; 231 int i, error, revision; 232 233 sc->sc_dev = self; 234 sc->sc_pct = pa->pa_pc; 235 sc->sc_pcitag = pa->pa_tag; 236 237 callout_init(&sc->calib_to, 0); 238 callout_setfunc(&sc->calib_to, iwn_calib_timeout, sc); 239 240 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo); 241 revision = PCI_REVISION(pa->pa_class); 242 aprint_normal(": %s (rev. 0x%2x)\n", devinfo, revision); 243 244 245 /* clear device specific PCI configuration register 0x41 */ 246 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40); 247 data &= ~0x0000ff00; 248 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data); 249 250 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG); 251 data |= PCI_COMMAND_MASTER_ENABLE; 252 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data); 253 254 /* enable bus-mastering */ 255 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG); 256 data |= PCI_COMMAND_MASTER_ENABLE; 257 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data); 258 259 /* map the register window */ 260 memtype = pci_mapreg_type(pa->pa_pc, pa->pa_tag, IWN_PCI_BAR0); 261 error = pci_mapreg_map(pa, IWN_PCI_BAR0, memtype, 0, &sc->sc_st, 262 &sc->sc_sh, NULL, &sc->sc_sz); 263 if (error != 0) { 264 aprint_error_dev(self, "could not map memory space\n"); 265 return; 266 } 267 268 sc->sc_dmat = pa->pa_dmat; 269 270 if (pci_intr_map(pa, &ih) != 0) { 271 aprint_error_dev(self, "could not map interrupt\n"); 272 return; 273 } 274 275 intrstr = pci_intr_string(sc->sc_pct, ih); 276 sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwn_intr, sc); 277 278 if (sc->sc_ih == NULL) { 279 aprint_error_dev(self, "could not establish interrupt"); 280 if (intrstr != NULL) 281 aprint_error(" at %s", intrstr); 282 aprint_error("\n"); 283 return; 284 } 285 aprint_normal_dev(self, "interrupting at %s\n", intrstr); 286 287 if (iwn_reset(sc) != 0) { 288 aprint_error_dev(self, "could not reset adapter\n"); 289 return; 290 } 291 292 /* 293 * Allocate DMA memory for firmware transfers. 294 */ 295 if ((error = iwn_alloc_fwmem(sc)) != 0) { 296 aprint_error_dev(self, "could not allocate firmware memory\n"); 297 return; 298 } 299 300 /* 301 * Allocate a "keep warm" page. 302 */ 303 if ((error = iwn_alloc_kw(sc)) != 0) { 304 aprint_error_dev(self, "could not allocate keep warm page\n"); 305 goto fail1; 306 } 307 308 /* 309 * Allocate shared area (communication area). 310 */ 311 if ((error = iwn_alloc_shared(sc)) != 0) { 312 aprint_error_dev(self, "could not allocate shared area\n"); 313 goto fail2; 314 } 315 316 /* 317 * Allocate Rx buffers and Tx/Rx rings. 318 */ 319 if ((error = iwn_alloc_rpool(sc)) != 0) { 320 aprint_error_dev(self, "could not allocate Rx buffers\n"); 321 goto fail3; 322 } 323 324 for (i = 0; i < IWN_NTXQUEUES; i++) { 325 struct iwn_tx_ring *txq = &sc->txq[i]; 326 error = iwn_alloc_tx_ring(sc, txq, IWN_TX_RING_COUNT, i); 327 if (error != 0) { 328 aprint_error_dev(self, "could not allocate Tx ring %d\n", i); 329 goto fail4; 330 } 331 } 332 333 if (iwn_alloc_rx_ring(sc, &sc->rxq) != 0) { 334 aprint_error_dev(self, "could not allocate Rx ring\n"); 335 goto fail4; 336 } 337 338 339 ic->ic_ifp = ifp; 340 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 341 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 342 ic->ic_state = IEEE80211_S_INIT; 343 344 /* set device capabilities */ 345 ic->ic_caps = 346 IEEE80211_C_IBSS | /* IBSS mode support */ 347 IEEE80211_C_WPA | /* 802.11i */ 348 IEEE80211_C_MONITOR | /* monitor mode supported */ 349 IEEE80211_C_TXPMGT | /* tx power management */ 350 IEEE80211_C_SHSLOT | /* short slot time supported */ 351 IEEE80211_C_SHPREAMBLE| /* short preamble supported */ 352 IEEE80211_C_WME; /* 802.11e */ 353 354 /* read supported channels and MAC address from EEPROM */ 355 iwn_read_eeprom(sc); 356 357 /* set supported .11a, .11b and .11g rates */ 358 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwn_rateset_11a; 359 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwn_rateset_11b; 360 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwn_rateset_11g; 361 362 /* IBSS channel undefined for now */ 363 ic->ic_ibss_chan = &ic->ic_channels[0]; 364 365 ifp->if_softc = sc; 366 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 367 ifp->if_init = iwn_init; 368 ifp->if_stop = iwn_stop; 369 ifp->if_ioctl = iwn_ioctl; 370 ifp->if_start = iwn_start; 371 ifp->if_watchdog = iwn_watchdog; 372 IFQ_SET_READY(&ifp->if_snd); 373 memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ); 374 375 if_attach(ifp); 376 ieee80211_ifattach(ic); 377 ic->ic_node_alloc = iwn_node_alloc; 378 ic->ic_newassoc = iwn_newassoc; 379 ic->ic_wme.wme_update = iwn_wme_update; 380 381 /* override state transition machine */ 382 sc->sc_newstate = ic->ic_newstate; 383 ic->ic_newstate = iwn_newstate; 384 ieee80211_media_init(ic, iwn_media_change, ieee80211_media_status); 385 386 sc->amrr.amrr_min_success_threshold = 1; 387 sc->amrr.amrr_max_success_threshold = 15; 388 389 if (!pmf_device_register(self, NULL, iwn_resume)) 390 aprint_error_dev(self, "couldn't establish power handler\n"); 391 else 392 pmf_class_network_register(self, ifp); 393 394 iwn_radiotap_attach(sc); 395 396 ieee80211_announce(ic); 397 398 return; 399 400 /* free allocated memory if something failed during attachment */ 401 fail4: while (--i >= 0) 402 iwn_free_tx_ring(sc, &sc->txq[i]); 403 iwn_free_rpool(sc); 404 fail3: iwn_free_shared(sc); 405 fail2: iwn_free_kw(sc); 406 fail1: iwn_free_fwmem(sc); 407 } 408 409 static int 410 iwn_detach(struct device* self, int flags __unused) 411 { 412 struct iwn_softc *sc = (struct iwn_softc *)self; 413 struct ifnet *ifp = sc->sc_ic.ic_ifp; 414 int ac; 415 416 iwn_stop(ifp, 1); 417 418 #if NBPFILTER > 0 419 if (ifp != NULL) 420 bpfdetach(ifp); 421 #endif 422 ieee80211_ifdetach(&sc->sc_ic); 423 if (ifp != NULL) 424 if_detach(ifp); 425 426 for (ac = 0; ac < IWN_NTXQUEUES; ac++) 427 iwn_free_tx_ring(sc, &sc->txq[ac]); 428 iwn_free_rx_ring(sc, &sc->rxq); 429 iwn_free_rpool(sc); 430 iwn_free_shared(sc); 431 432 if (sc->sc_ih != NULL) { 433 pci_intr_disestablish(sc->sc_pct, sc->sc_ih); 434 sc->sc_ih = NULL; 435 } 436 437 bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz); 438 439 return 0; 440 } 441 442 /* 443 * Attach the interface to 802.11 radiotap. 444 */ 445 static void 446 iwn_radiotap_attach(struct iwn_softc *sc) 447 { 448 struct ifnet *ifp = sc->sc_ic.ic_ifp; 449 450 #if NBPFILTER > 0 451 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 452 sizeof (struct ieee80211_frame) + IEEE80211_RADIOTAP_HDRLEN, 453 &sc->sc_drvbpf); 454 455 sc->sc_rxtap_len = sizeof sc->sc_rxtapu; 456 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 457 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWN_RX_RADIOTAP_PRESENT); 458 459 sc->sc_txtap_len = sizeof sc->sc_txtapu; 460 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 461 sc->sc_txtap.wt_ihdr.it_present = htole32(IWN_TX_RADIOTAP_PRESENT); 462 #endif 463 } 464 465 466 /* 467 * Build a beacon frame that the firmware will broadcast periodically in 468 * IBSS or HostAP modes. 469 */ 470 #if 0 471 static int 472 iwn_setup_beacon(struct iwn_softc *sc, struct ieee80211_node *ni) 473 { 474 struct ieee80211com *ic = &sc->sc_ic; 475 struct iwn_tx_ring *ring = &sc->txq[4]; 476 struct iwn_tx_desc *desc; 477 struct iwn_tx_data *data; 478 struct iwn_tx_cmd *cmd; 479 struct iwn_cmd_beacon *bcn; 480 struct ieee80211_beacon_offsets bo; 481 struct mbuf *m0; 482 bus_addr_t paddr; 483 int error; 484 485 desc = &ring->desc[ring->cur]; 486 data = &ring->data[ring->cur]; 487 488 m0 = ieee80211_beacon_alloc(ic, ni, &bo); 489 if (m0 == NULL) { 490 aprint_error_dev(sc->sc_dev, "could not allocate beacon frame\n"); 491 return ENOMEM; 492 } 493 494 cmd = &ring->cmd[ring->cur]; 495 cmd->code = IWN_CMD_SET_BEACON; 496 cmd->flags = 0; 497 cmd->qid = ring->qid; 498 cmd->idx = ring->cur; 499 500 bcn = (struct iwn_cmd_beacon *)cmd->data; 501 memset(bcn, 0, sizeof (struct iwn_cmd_beacon)); 502 bcn->id = IWN_ID_BROADCAST; 503 bcn->ofdm_mask = 0xff; 504 bcn->cck_mask = 0x0f; 505 bcn->lifetime = htole32(IWN_LIFETIME_INFINITE); 506 bcn->len = htole16(m0->m_pkthdr.len); 507 bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ? 508 iwn_plcp_signal(12) : iwn_plcp_signal(2); 509 bcn->flags = htole32(IWN_TX_AUTO_SEQ | IWN_TX_INSERT_TSTAMP); 510 511 /* save and trim IEEE802.11 header */ 512 m_copydata(m0, 0, sizeof (struct ieee80211_frame), (void *)&bcn->wh); 513 m_adj(m0, sizeof (struct ieee80211_frame)); 514 515 /* assume beacon frame is contiguous */ 516 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, 517 BUS_DMA_READ | BUS_DMA_NOWAIT); 518 if (error) { 519 aprint_error_dev(sc->sc_dev, "could not map beacon\n"); 520 m_freem(m0); 521 return error; 522 } 523 524 data->m = m0; 525 526 /* first scatter/gather segment is used by the beacon command */ 527 paddr = ring->cmd_dma.paddr + ring->cur * sizeof (struct iwn_tx_cmd); 528 529 IWN_SET_DESC_NSEGS(desc, 2); 530 IWN_SET_DESC_SEG(desc, 0, paddr , 4 + sizeof(struct iwn_cmd_beacon)); 531 IWN_SET_DESC_SEG(desc, 1, data->map->dm_segs[0].ds_addr, 532 data->map->dm_segs[1].ds_len); 533 534 535 /* kick cmd ring */ 536 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT; 537 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur); 538 539 return 0; 540 } 541 #endif 542 543 static int 544 iwn_dma_contig_alloc(bus_dma_tag_t tag, struct iwn_dma_info *dma, void **kvap, 545 bus_size_t size, bus_size_t alignment, int flags) 546 { 547 int nsegs, error; 548 549 dma->tag = tag; 550 dma->size = size; 551 552 error = bus_dmamap_create(tag, size, 1, size, 0, flags, &dma->map); 553 if (error != 0) 554 goto fail; 555 556 error = bus_dmamem_alloc(tag, size, alignment, 0, &dma->seg, 1, &nsegs, 557 flags); 558 if (error != 0) 559 goto fail; 560 561 error = bus_dmamem_map(tag, &dma->seg, 1, size, &dma->vaddr, flags); 562 if (error != 0) 563 goto fail; 564 565 error = bus_dmamap_load(tag, dma->map, dma->vaddr, size, NULL, flags); 566 if (error != 0) 567 goto fail; 568 569 memset(dma->vaddr, 0, size); 570 571 dma->paddr = dma->map->dm_segs[0].ds_addr; 572 if (kvap != NULL) 573 *kvap = dma->vaddr; 574 575 return 0; 576 577 fail: iwn_dma_contig_free(dma); 578 return error; 579 } 580 581 static void 582 iwn_dma_contig_free(struct iwn_dma_info *dma) 583 { 584 if (dma->map != NULL) { 585 if (dma->vaddr != NULL) { 586 bus_dmamap_unload(dma->tag, dma->map); 587 bus_dmamem_unmap(dma->tag, dma->vaddr, dma->size); 588 bus_dmamem_free(dma->tag, &dma->seg, 1); 589 dma->vaddr = NULL; 590 } 591 bus_dmamap_destroy(dma->tag, dma->map); 592 dma->map = NULL; 593 } 594 } 595 596 static int 597 iwn_alloc_shared(struct iwn_softc *sc) 598 { 599 int error; 600 /* must be aligned on a 1KB boundary */ 601 error = iwn_dma_contig_alloc(sc->sc_dmat, &sc->shared_dma, 602 (void **)&sc->shared, sizeof (struct iwn_shared), 603 1024,BUS_DMA_NOWAIT); 604 if (error != 0) 605 aprint_error_dev(sc->sc_dev, 606 "could not allocate shared area DMA memory\n"); 607 608 return error; 609 610 } 611 612 static void 613 iwn_free_shared(struct iwn_softc *sc) 614 { 615 iwn_dma_contig_free(&sc->shared_dma); 616 } 617 618 static int 619 iwn_alloc_kw(struct iwn_softc *sc) 620 { 621 /* must be aligned on a 16-byte boundary */ 622 return iwn_dma_contig_alloc(sc->sc_dmat, &sc->kw_dma, NULL, 623 PAGE_SIZE, PAGE_SIZE, BUS_DMA_NOWAIT); 624 } 625 626 static void 627 iwn_free_kw(struct iwn_softc *sc) 628 { 629 iwn_dma_contig_free(&sc->kw_dma); 630 } 631 632 static int 633 iwn_alloc_fwmem(struct iwn_softc *sc) 634 { 635 int error; 636 /* allocate enough contiguous space to store text and data */ 637 error = iwn_dma_contig_alloc(sc->sc_dmat, &sc->fw_dma, NULL, 638 IWN_FW_MAIN_TEXT_MAXSZ + IWN_FW_MAIN_DATA_MAXSZ, 16, 639 BUS_DMA_NOWAIT); 640 641 if (error != 0){ 642 aprint_error_dev(sc->sc_dev, 643 "could not allocate firmware transfer area DMA memory\n" ); 644 645 } 646 return error; 647 } 648 649 static void 650 iwn_free_fwmem(struct iwn_softc *sc) 651 { 652 iwn_dma_contig_free(&sc->fw_dma); 653 } 654 655 static struct iwn_rbuf * 656 iwn_alloc_rbuf(struct iwn_softc *sc) 657 { 658 struct iwn_rbuf *rbuf; 659 660 rbuf = SLIST_FIRST(&sc->rxq.freelist); 661 if (rbuf == NULL) 662 return NULL; 663 SLIST_REMOVE_HEAD(&sc->rxq.freelist, next); 664 sc->rxq.nb_free_entries --; 665 return rbuf; 666 } 667 668 /* 669 * This is called automatically by the network stack when the mbuf to which 670 * our Rx buffer is attached is freed. 671 */ 672 static void 673 iwn_free_rbuf(struct mbuf* m, void *buf, size_t size, void *arg) 674 { 675 struct iwn_rbuf *rbuf = arg; 676 struct iwn_softc *sc = rbuf->sc; 677 678 /* put the buffer back in the free list */ 679 SLIST_INSERT_HEAD(&sc->rxq.freelist, rbuf, next); 680 681 sc->rxq.nb_free_entries ++; 682 683 if (__predict_true(m != NULL)) 684 pool_cache_put(mb_cache, m); 685 } 686 687 688 static int 689 iwn_alloc_rpool(struct iwn_softc *sc) 690 { 691 struct iwn_rx_ring *ring = &sc->rxq; 692 struct iwn_rbuf *rbuf; 693 int i, error; 694 695 /* allocate a big chunk of DMA'able memory.. */ 696 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->buf_dma, NULL, 697 IWN_RBUF_COUNT * IWN_RBUF_SIZE, IWN_BUF_ALIGN, BUS_DMA_NOWAIT); 698 if (error != 0) { 699 aprint_error_dev(sc->sc_dev, 700 "could not allocate Rx buffers DMA memory\n"); 701 return error; 702 } 703 704 /* ..and split it into chunks of "rbufsz" bytes */ 705 SLIST_INIT(&ring->freelist); 706 for (i = 0; i < IWN_RBUF_COUNT; i++) { 707 rbuf = &ring->rbuf[i]; 708 709 rbuf->sc = sc; /* backpointer for callbacks */ 710 rbuf->vaddr = (char *)ring->buf_dma.vaddr + i * IWN_RBUF_SIZE; 711 rbuf->paddr = ring->buf_dma.paddr + i * IWN_RBUF_SIZE; 712 713 SLIST_INSERT_HEAD(&ring->freelist, rbuf, next); 714 } 715 ring->nb_free_entries = IWN_RBUF_COUNT; 716 return 0; 717 } 718 719 static void 720 iwn_free_rpool(struct iwn_softc *sc) 721 { 722 iwn_dma_contig_free(&sc->rxq.buf_dma); 723 } 724 725 static int 726 iwn_alloc_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring) 727 { 728 struct iwn_rx_data *data; 729 struct iwn_rbuf *rbuf; 730 int i, error; 731 732 ring->cur = 0; 733 734 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma, 735 (void **)&ring->desc, IWN_RX_RING_COUNT * sizeof (struct iwn_rx_desc), 736 IWN_RING_DMA_ALIGN, BUS_DMA_NOWAIT); 737 if (error != 0) { 738 aprint_error_dev(sc->sc_dev, 739 "could not allocate rx ring DMA memory\n"); 740 goto fail; 741 } 742 743 /* 744 * Setup Rx buffers. 745 */ 746 for (i = 0; i < IWN_RX_RING_COUNT; i++) { 747 data = &ring->data[i]; 748 749 MGETHDR(data->m, M_DONTWAIT, MT_DATA); 750 if (data->m == NULL) { 751 aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf\n"); 752 error = ENOMEM; 753 goto fail; 754 } 755 if ((rbuf = iwn_alloc_rbuf(sc)) == NULL) { 756 m_freem(data->m); 757 data->m = NULL; 758 aprint_error_dev(sc->sc_dev, "could not allocate rx buffer\n"); 759 error = ENOMEM; 760 goto fail; 761 } 762 /* attach Rx buffer to mbuf */ 763 MEXTADD(data->m, rbuf->vaddr, IWN_RBUF_SIZE, 0, iwn_free_rbuf, 764 rbuf); 765 766 data->m->m_flags |= M_EXT_RW; 767 /* Rx buffers are aligned on a 256-byte boundary */ 768 ring->desc[i] = htole32(rbuf->paddr >> 8); 769 } 770 771 return 0; 772 773 fail: iwn_free_rx_ring(sc, ring); 774 return error; 775 } 776 777 static void 778 iwn_reset_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring) 779 { 780 int ntries; 781 782 iwn_mem_lock(sc); 783 784 IWN_WRITE(sc, IWN_RX_CONFIG, 0); 785 for (ntries = 0; ntries < 100; ntries++) { 786 if (IWN_READ(sc, IWN_RX_STATUS) & IWN_RX_IDLE) 787 break; 788 DELAY(10); 789 } 790 #ifdef IWN_DEBUG 791 if (ntries == 100 && iwn_debug > 0) 792 aprint_error_dev(sc->sc_dev, "timeout resetting Rx ring\n"); 793 #endif 794 iwn_mem_unlock(sc); 795 796 ring->cur = 0; 797 } 798 799 static void 800 iwn_free_rx_ring(struct iwn_softc *sc, struct iwn_rx_ring *ring) 801 { 802 int i; 803 804 iwn_dma_contig_free(&ring->desc_dma); 805 806 for (i = 0; i < IWN_RX_RING_COUNT; i++) { 807 if (ring->data[i].m != NULL) 808 m_freem(ring->data[i].m); 809 } 810 } 811 812 static int 813 iwn_alloc_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring, int count, 814 int qid) 815 { 816 struct iwn_tx_data *data; 817 int i, error; 818 819 ring->qid = qid; 820 ring->count = count; 821 ring->queued = 0; 822 ring->cur = 0; 823 824 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->desc_dma, 825 (void **)&ring->desc, count * sizeof (struct iwn_tx_desc), 826 IWN_RING_DMA_ALIGN, BUS_DMA_NOWAIT); 827 if (error != 0) { 828 aprint_error_dev(sc->sc_dev, "could not allocate tx ring DMA memory\n"); 829 goto fail; 830 } 831 832 error = iwn_dma_contig_alloc(sc->sc_dmat, &ring->cmd_dma, 833 (void **)&ring->cmd, count * sizeof (struct iwn_tx_cmd), 4, 834 BUS_DMA_NOWAIT); 835 if (error != 0) { 836 aprint_error_dev(sc->sc_dev, "could not allocate tx cmd DMA memory\n"); 837 goto fail; 838 } 839 840 ring->data = malloc(count * sizeof (struct iwn_tx_data), M_DEVBUF, M_NOWAIT); 841 842 if (ring->data == NULL) { 843 aprint_error_dev(sc->sc_dev,"could not allocate tx data slots\n"); 844 goto fail; 845 } 846 847 memset(ring->data, 0, count * sizeof (struct iwn_tx_data)); 848 849 for (i = 0; i < count; i++) { 850 data = &ring->data[i]; 851 852 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 853 IWN_MAX_SCATTER - 1, MCLBYTES, 0, BUS_DMA_NOWAIT, 854 &data->map); 855 if (error != 0) { 856 aprint_error_dev(sc->sc_dev, "could not create tx buf DMA map\n"); 857 goto fail; 858 } 859 } 860 861 return 0; 862 863 fail: iwn_free_tx_ring(sc, ring); 864 return error; 865 } 866 867 static void 868 iwn_reset_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring) 869 { 870 struct iwn_tx_data *data; 871 uint32_t tmp; 872 int i, ntries; 873 874 iwn_mem_lock(sc); 875 876 IWN_WRITE(sc, IWN_TX_CONFIG(ring->qid), 0); 877 for (ntries = 0; ntries < 100; ntries++) { 878 tmp = IWN_READ(sc, IWN_TX_STATUS); 879 if ((tmp & IWN_TX_IDLE(ring->qid)) == IWN_TX_IDLE(ring->qid)) 880 break; 881 DELAY(10); 882 } 883 #ifdef IWN_DEBUG 884 if (ntries == 100 && iwn_debug > 1) { 885 aprint_error_dev(sc->sc_dev, "timeout resetting Tx ring %d\n", ring->qid); 886 } 887 #endif 888 iwn_mem_unlock(sc); 889 890 for (i = 0; i < ring->count; i++) { 891 data = &ring->data[i]; 892 893 if (data->m != NULL) { 894 bus_dmamap_unload(sc->sc_dmat, data->map); 895 m_freem(data->m); 896 data->m = NULL; 897 } 898 } 899 900 ring->queued = 0; 901 ring->cur = 0; 902 } 903 904 static void 905 iwn_free_tx_ring(struct iwn_softc *sc, struct iwn_tx_ring *ring) 906 { 907 struct iwn_tx_data *data; 908 int i; 909 910 iwn_dma_contig_free(&ring->desc_dma); 911 iwn_dma_contig_free(&ring->cmd_dma); 912 913 if (ring->data != NULL) { 914 for (i = 0; i < ring->count; i++) { 915 data = &ring->data[i]; 916 917 if (data->m != NULL) { 918 bus_dmamap_unload(sc->sc_dmat, data->map); 919 m_freem(data->m); 920 } 921 } 922 free(ring->data, M_DEVBUF); 923 } 924 } 925 926 /*ARGUSED*/ 927 struct ieee80211_node * 928 iwn_node_alloc(struct ieee80211_node_table *nt __unused) 929 { 930 struct iwn_node *wn; 931 932 wn = malloc(sizeof (struct iwn_node), M_DEVBUF, M_NOWAIT); 933 934 if (wn != NULL) 935 memset(wn, 0, sizeof (struct iwn_node)); 936 return (struct ieee80211_node *)wn; 937 938 } 939 940 static void 941 iwn_newassoc(struct ieee80211_node *ni, int isnew) 942 { 943 struct iwn_softc *sc = ni->ni_ic->ic_ifp->if_softc; 944 int i; 945 946 ieee80211_amrr_node_init(&sc->amrr, &((struct iwn_node *)ni)->amn); 947 948 /* set rate to some reasonable initial value */ 949 for (i = ni->ni_rates.rs_nrates - 1; 950 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72; 951 i--); 952 ni->ni_txrate = i; 953 } 954 955 static int 956 iwn_media_change(struct ifnet *ifp) 957 { 958 int error; 959 960 error = ieee80211_media_change(ifp); 961 if (error != ENETRESET) 962 return error; 963 964 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) 965 iwn_init(ifp); 966 967 return 0; 968 } 969 970 static int 971 iwn_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 972 { 973 struct ifnet *ifp = ic->ic_ifp; 974 struct iwn_softc *sc = ifp->if_softc; 975 int error; 976 977 callout_stop(&sc->calib_to); 978 979 switch (nstate) { 980 981 case IEEE80211_S_SCAN: 982 983 if (sc->is_scanning) 984 break; 985 986 sc->is_scanning = true; 987 ieee80211_node_table_reset(&ic->ic_scan); 988 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN; 989 990 /* make the link LED blink while we're scanning */ 991 iwn_set_led(sc, IWN_LED_LINK, 20, 2); 992 993 if ((error = iwn_scan(sc, IEEE80211_CHAN_G)) != 0) { 994 aprint_error_dev(sc->sc_dev, "could not initiate scan\n"); 995 ic->ic_flags &= ~(IEEE80211_F_SCAN | IEEE80211_F_ASCAN); 996 return error; 997 } 998 ic->ic_state = nstate; 999 return 0; 1000 1001 case IEEE80211_S_ASSOC: 1002 if (ic->ic_state != IEEE80211_S_RUN) 1003 break; 1004 /* FALLTHROUGH */ 1005 case IEEE80211_S_AUTH: 1006 /* reset state to handle reassociations correctly */ 1007 sc->config.associd = 0; 1008 sc->config.filter &= ~htole32(IWN_FILTER_BSS); 1009 /*sc->calib.state = IWN_CALIB_STATE_INIT;*/ 1010 1011 if ((error = iwn_auth(sc)) != 0) { 1012 aprint_error_dev(sc->sc_dev, "could not move to auth state\n"); 1013 return error; 1014 } 1015 break; 1016 1017 case IEEE80211_S_RUN: 1018 if ((error = iwn_run(sc)) != 0) { 1019 aprint_error_dev(sc->sc_dev, "could not move to run state\n"); 1020 return error; 1021 } 1022 break; 1023 1024 case IEEE80211_S_INIT: 1025 sc->is_scanning = false; 1026 break; 1027 } 1028 1029 return sc->sc_newstate(ic, nstate, arg); 1030 } 1031 1032 /* 1033 * Grab exclusive access to NIC memory. 1034 */ 1035 static void 1036 iwn_mem_lock(struct iwn_softc *sc) 1037 { 1038 uint32_t tmp; 1039 int ntries; 1040 1041 tmp = IWN_READ(sc, IWN_GPIO_CTL); 1042 IWN_WRITE(sc, IWN_GPIO_CTL, tmp | IWN_GPIO_MAC); 1043 1044 /* spin until we actually get the lock */ 1045 for (ntries = 0; ntries < 1000; ntries++) { 1046 if ((IWN_READ(sc, IWN_GPIO_CTL) & 1047 (IWN_GPIO_CLOCK | IWN_GPIO_SLEEP)) == IWN_GPIO_CLOCK) 1048 break; 1049 DELAY(10); 1050 } 1051 if (ntries == 1000) 1052 aprint_error_dev(sc->sc_dev, "could not lock memory\n"); 1053 } 1054 1055 /* 1056 * Release lock on NIC memory. 1057 */ 1058 static void 1059 iwn_mem_unlock(struct iwn_softc *sc) 1060 { 1061 uint32_t tmp = IWN_READ(sc, IWN_GPIO_CTL); 1062 IWN_WRITE(sc, IWN_GPIO_CTL, tmp & ~IWN_GPIO_MAC); 1063 } 1064 1065 static uint32_t 1066 iwn_mem_read(struct iwn_softc *sc, uint32_t addr) 1067 { 1068 IWN_WRITE(sc, IWN_READ_MEM_ADDR, IWN_MEM_4 | addr); 1069 return IWN_READ(sc, IWN_READ_MEM_DATA); 1070 } 1071 1072 static void 1073 iwn_mem_write(struct iwn_softc *sc, uint32_t addr, uint32_t data) 1074 { 1075 IWN_WRITE(sc, IWN_WRITE_MEM_ADDR, IWN_MEM_4 | addr); 1076 IWN_WRITE(sc, IWN_WRITE_MEM_DATA, data); 1077 } 1078 1079 static void 1080 iwn_mem_write_region_4(struct iwn_softc *sc, uint32_t addr, 1081 const uint32_t *data, int wlen) 1082 { 1083 for (; wlen > 0; wlen--, data++, addr += 4) 1084 iwn_mem_write(sc, addr, *data); 1085 } 1086 1087 static int 1088 iwn_eeprom_lock(struct iwn_softc *sc) 1089 { 1090 uint32_t tmp; 1091 int ntries; 1092 1093 tmp = IWN_READ(sc, IWN_HWCONFIG); 1094 IWN_WRITE(sc, IWN_HWCONFIG, tmp | IWN_HW_EEPROM_LOCKED); 1095 1096 /* spin until we actually get the lock */ 1097 for (ntries = 0; ntries < 100; ntries++) { 1098 if (IWN_READ(sc, IWN_HWCONFIG) & IWN_HW_EEPROM_LOCKED) 1099 return 0; 1100 DELAY(10); 1101 } 1102 return ETIMEDOUT; 1103 } 1104 1105 static void 1106 iwn_eeprom_unlock(struct iwn_softc *sc) 1107 { 1108 uint32_t tmp = IWN_READ(sc, IWN_HWCONFIG); 1109 IWN_WRITE(sc, IWN_HWCONFIG, tmp & ~IWN_HW_EEPROM_LOCKED); 1110 } 1111 1112 /* 1113 * Read `len' bytes from the EEPROM. We access the EEPROM through the MAC 1114 * instead of using the traditional bit-bang method. 1115 */ 1116 static int 1117 iwn_read_prom_data(struct iwn_softc *sc, uint32_t addr, void *data, int len) 1118 { 1119 uint8_t *out = data; 1120 uint32_t val; 1121 int ntries; 1122 1123 iwn_mem_lock(sc); 1124 for (; len > 0; len -= 2, addr++) { 1125 IWN_WRITE(sc, IWN_EEPROM_CTL, addr << 2); 1126 IWN_WRITE(sc, IWN_EEPROM_CTL, 1127 IWN_READ(sc, IWN_EEPROM_CTL) & ~IWN_EEPROM_CMD); 1128 1129 for (ntries = 0; ntries < 10; ntries++) { 1130 if ((val = IWN_READ(sc, IWN_EEPROM_CTL)) & 1131 IWN_EEPROM_READY) 1132 break; 1133 DELAY(5); 1134 } 1135 if (ntries == 10) { 1136 aprint_error_dev(sc->sc_dev, "could not read EEPROM\n"); 1137 return ETIMEDOUT; 1138 } 1139 *out++ = val >> 16; 1140 if (len > 1) 1141 *out++ = val >> 24; 1142 } 1143 iwn_mem_unlock(sc); 1144 1145 return 0; 1146 } 1147 1148 /* 1149 * The firmware boot code is small and is intended to be copied directly into 1150 * the NIC internal memory. 1151 */ 1152 static int 1153 iwn_load_microcode(struct iwn_softc *sc, const uint8_t *ucode, int size) 1154 { 1155 int ntries; 1156 1157 size /= sizeof (uint32_t); 1158 1159 iwn_mem_lock(sc); 1160 1161 /* copy microcode image into NIC memory */ 1162 iwn_mem_write_region_4(sc, IWN_MEM_UCODE_BASE, 1163 (const uint32_t *)ucode, size); 1164 1165 iwn_mem_write(sc, IWN_MEM_UCODE_SRC, 0); 1166 iwn_mem_write(sc, IWN_MEM_UCODE_DST, IWN_FW_TEXT); 1167 iwn_mem_write(sc, IWN_MEM_UCODE_SIZE, size); 1168 1169 /* run microcode */ 1170 iwn_mem_write(sc, IWN_MEM_UCODE_CTL, IWN_UC_RUN); 1171 1172 /* wait for transfer to complete */ 1173 for (ntries = 0; ntries < 1000; ntries++) { 1174 if (!(iwn_mem_read(sc, IWN_MEM_UCODE_CTL) & IWN_UC_RUN)) 1175 break; 1176 DELAY(10); 1177 } 1178 if (ntries == 1000) { 1179 iwn_mem_unlock(sc); 1180 aprint_error_dev(sc->sc_dev, "could not load boot firmware\n"); 1181 return ETIMEDOUT; 1182 } 1183 iwn_mem_write(sc, IWN_MEM_UCODE_CTL, IWN_UC_ENABLE); 1184 1185 iwn_mem_unlock(sc); 1186 1187 return 0; 1188 } 1189 1190 static int 1191 iwn_load_firmware(struct iwn_softc *sc) 1192 { 1193 struct iwn_dma_info *dma = &sc->fw_dma; 1194 struct iwn_firmware_hdr hdr; 1195 const uint8_t *init_text, *init_data, *main_text, *main_data; 1196 const uint8_t *boot_text; 1197 uint32_t init_textsz, init_datasz, main_textsz, main_datasz; 1198 uint32_t boot_textsz; 1199 firmware_handle_t fw; 1200 u_char *dfw; 1201 size_t size; 1202 int error; 1203 1204 /* load firmware image from disk */ 1205 if ((error = firmware_open("if_iwn","iwlwifi-4965.ucode", &fw) != 0)) { 1206 aprint_error_dev(sc->sc_dev, "could not read firmware file\n"); 1207 goto fail1; 1208 } 1209 1210 size = firmware_get_size(fw); 1211 1212 /* extract firmware header information */ 1213 if (size < sizeof (struct iwn_firmware_hdr)) { 1214 aprint_error_dev(sc->sc_dev, "truncated firmware header: %zu bytes\n", size); 1215 1216 error = EINVAL; 1217 goto fail2; 1218 } 1219 1220 1221 if ((error = firmware_read(fw, 0, &hdr, 1222 sizeof (struct iwn_firmware_hdr))) != 0) { 1223 aprint_error_dev(sc->sc_dev, "can't get firmware header\n"); 1224 goto fail2; 1225 } 1226 1227 main_textsz = le32toh(hdr.main_textsz); 1228 main_datasz = le32toh(hdr.main_datasz); 1229 init_textsz = le32toh(hdr.init_textsz); 1230 init_datasz = le32toh(hdr.init_datasz); 1231 boot_textsz = le32toh(hdr.boot_textsz); 1232 1233 /* sanity-check firmware segments sizes */ 1234 if (main_textsz > IWN_FW_MAIN_TEXT_MAXSZ || 1235 main_datasz > IWN_FW_MAIN_DATA_MAXSZ || 1236 init_textsz > IWN_FW_INIT_TEXT_MAXSZ || 1237 init_datasz > IWN_FW_INIT_DATA_MAXSZ || 1238 boot_textsz > IWN_FW_BOOT_TEXT_MAXSZ || 1239 (boot_textsz & 3) != 0) { 1240 aprint_error_dev(sc->sc_dev, "invalid firmware header\n"); 1241 error = EINVAL; 1242 goto fail2; 1243 } 1244 1245 /* check that all firmware segments are present */ 1246 if (size < sizeof (struct iwn_firmware_hdr) + main_textsz + 1247 main_datasz + init_textsz + init_datasz + boot_textsz) { 1248 aprint_error_dev(sc->sc_dev, "firmware file too short: %zu bytes\n", size); 1249 error = EINVAL; 1250 goto fail2; 1251 } 1252 1253 dfw = firmware_malloc(size); 1254 if (dfw == NULL) { 1255 aprint_error_dev(sc->sc_dev, "not enough memory to stock firmware\n"); 1256 error = ENOMEM; 1257 goto fail2; 1258 } 1259 1260 if ((error = firmware_read(fw, 0, dfw, size)) != 0) { 1261 aprint_error_dev(sc->sc_dev, "can't get firmware\n"); 1262 goto fail2; 1263 } 1264 1265 /* get pointers to firmware segments */ 1266 main_text = dfw + sizeof (struct iwn_firmware_hdr); 1267 main_data = main_text + main_textsz; 1268 init_text = main_data + main_datasz; 1269 init_data = init_text + init_textsz; 1270 boot_text = init_data + init_datasz; 1271 1272 /* copy initialization images into pre-allocated DMA-safe memory */ 1273 memcpy(dma->vaddr, init_data, init_datasz); 1274 memcpy((char *)dma->vaddr + IWN_FW_INIT_DATA_MAXSZ, init_text, init_textsz); 1275 1276 /* tell adapter where to find initialization images */ 1277 iwn_mem_lock(sc); 1278 iwn_mem_write(sc, IWN_MEM_DATA_BASE, dma->paddr >> 4); 1279 iwn_mem_write(sc, IWN_MEM_DATA_SIZE, init_datasz); 1280 iwn_mem_write(sc, IWN_MEM_TEXT_BASE, 1281 (dma->paddr + IWN_FW_INIT_DATA_MAXSZ) >> 4); 1282 iwn_mem_write(sc, IWN_MEM_TEXT_SIZE, init_textsz); 1283 iwn_mem_unlock(sc); 1284 1285 /* load firmware boot code */ 1286 if ((error = iwn_load_microcode(sc, boot_text, boot_textsz)) != 0) { 1287 aprint_error_dev(sc->sc_dev, "could not load boot firmware\n"); 1288 goto fail3; 1289 } 1290 1291 /* now press "execute" ;-) */ 1292 IWN_WRITE(sc, IWN_RESET, 0); 1293 1294 /* ..and wait at most one second for adapter to initialize */ 1295 if ((error = tsleep(sc, PCATCH, "iwninit", hz)) != 0) { 1296 /* this isn't what was supposed to happen.. */ 1297 aprint_error_dev(sc->sc_dev, "timeout waiting for adapter to initialize\n"); 1298 } 1299 1300 /* copy runtime images into pre-allocated DMA-safe memory */ 1301 memcpy((char *)dma->vaddr, main_data, main_datasz); 1302 memcpy((char *)dma->vaddr + IWN_FW_MAIN_DATA_MAXSZ, main_text, main_textsz); 1303 1304 /* tell adapter where to find runtime images */ 1305 iwn_mem_lock(sc); 1306 iwn_mem_write(sc, IWN_MEM_DATA_BASE, dma->paddr >> 4); 1307 iwn_mem_write(sc, IWN_MEM_DATA_SIZE, main_datasz); 1308 iwn_mem_write(sc, IWN_MEM_TEXT_BASE, 1309 (dma->paddr + IWN_FW_MAIN_DATA_MAXSZ) >> 4); 1310 iwn_mem_write(sc, IWN_MEM_TEXT_SIZE, IWN_FW_UPDATED | main_textsz); 1311 iwn_mem_unlock(sc); 1312 1313 /* wait at most one second for second alive notification */ 1314 if ((error = tsleep(sc, PCATCH, "iwninit", hz)) != 0) { 1315 /* this isn't what was supposed to happen.. */ 1316 aprint_error_dev(sc->sc_dev, "timeout waiting for adapter to initialize\n"); 1317 } 1318 1319 fail3: firmware_free(dfw,size); 1320 fail2: firmware_close(fw); 1321 fail1: return error; 1322 } 1323 1324 static void 1325 iwn_calib_timeout(void *arg) 1326 { 1327 struct iwn_softc *sc = arg; 1328 struct ieee80211com *ic = &sc->sc_ic; 1329 int s; 1330 1331 /* automatic rate control triggered every 500ms */ 1332 if (ic->ic_fixed_rate == -1) { 1333 s = splnet(); 1334 if (ic->ic_opmode == IEEE80211_M_STA) 1335 iwn_iter_func(sc, ic->ic_bss); 1336 else 1337 ieee80211_iterate_nodes(&ic->ic_sta, iwn_iter_func, sc); 1338 splx(s); 1339 } 1340 1341 /* automatic calibration every 60s */ 1342 if (++sc->calib_cnt >= 120) { 1343 DPRINTF(("sending request for statistics\n")); 1344 (void)iwn_cmd(sc, IWN_CMD_GET_STATISTICS, NULL, 0, 1); 1345 sc->calib_cnt = 0; 1346 } 1347 1348 callout_schedule(&sc->calib_to, hz/2); 1349 1350 } 1351 1352 static void 1353 iwn_iter_func(void *arg, struct ieee80211_node *ni) 1354 { 1355 struct iwn_softc *sc = arg; 1356 struct iwn_node *wn = (struct iwn_node *)ni; 1357 1358 ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn); 1359 } 1360 1361 static void 1362 iwn_ampdu_rx_start(struct iwn_softc *sc, struct iwn_rx_desc *desc) 1363 { 1364 struct iwn_rx_stat *stat; 1365 1366 DPRINTFN(2, ("received AMPDU stats\n")); 1367 /* save Rx statistics, they will be used on IWN_AMPDU_RX_DONE */ 1368 stat = (struct iwn_rx_stat *)(desc + 1); 1369 memcpy(&sc->last_rx_stat, stat, sizeof (*stat)); 1370 sc->last_rx_valid = 1; 1371 } 1372 1373 void 1374 iwn_rx_intr(struct iwn_softc *sc, struct iwn_rx_desc *desc, 1375 struct iwn_rx_data *data) 1376 { 1377 struct ieee80211com *ic = &sc->sc_ic; 1378 struct ifnet *ifp = ic->ic_ifp; 1379 struct iwn_rx_ring *ring = &sc->rxq; 1380 struct iwn_rbuf *rbuf; 1381 struct ieee80211_frame *wh; 1382 struct ieee80211_node *ni; 1383 struct mbuf *m, *mnew; 1384 struct iwn_rx_stat *stat; 1385 char *head; 1386 uint32_t *tail; 1387 int len, rssi; 1388 1389 if (desc->type == IWN_AMPDU_RX_DONE) { 1390 /* check for prior AMPDU_RX_START */ 1391 if (!sc->last_rx_valid) { 1392 DPRINTF(("missing AMPDU_RX_START\n")); 1393 ifp->if_ierrors++; 1394 return; 1395 } 1396 sc->last_rx_valid = 0; 1397 stat = &sc->last_rx_stat; 1398 } else 1399 stat = (struct iwn_rx_stat *)(desc + 1); 1400 1401 if (stat->cfg_phy_len > IWN_STAT_MAXLEN) { 1402 aprint_error_dev(sc->sc_dev, "invalid rx statistic header\n"); 1403 ifp->if_ierrors++; 1404 return; 1405 } 1406 1407 if (desc->type == IWN_AMPDU_RX_DONE) { 1408 struct iwn_rx_ampdu *ampdu = 1409 (struct iwn_rx_ampdu *)(desc + 1); 1410 head = (char *)(ampdu + 1); 1411 len = le16toh(ampdu->len); 1412 } else { 1413 head = (char *)(stat + 1) + stat->cfg_phy_len; 1414 len = le16toh(stat->len); 1415 } 1416 1417 /* discard Rx frames with bad CRC early */ 1418 tail = (uint32_t *)(head + len); 1419 if ((le32toh(*tail) & IWN_RX_NOERROR) != IWN_RX_NOERROR) { 1420 DPRINTFN(2, ("rx flags error %x\n", le32toh(*tail))); 1421 ifp->if_ierrors++; 1422 return; 1423 } 1424 /* XXX for ieee80211_find_rxnode() */ 1425 if (len < sizeof (struct ieee80211_frame)) { 1426 DPRINTF(("frame too short: %d\n", len)); 1427 ic->ic_stats.is_rx_tooshort++; 1428 ifp->if_ierrors++; 1429 return; 1430 } 1431 1432 m = data->m; 1433 1434 /* finalize mbuf */ 1435 m->m_pkthdr.rcvif = ifp; 1436 m->m_data = head; 1437 m->m_pkthdr.len = m->m_len = len; 1438 1439 if ((rbuf = SLIST_FIRST(&sc->rxq.freelist)) != NULL) { 1440 MGETHDR(mnew, M_DONTWAIT, MT_DATA); 1441 if (mnew == NULL) { 1442 ic->ic_stats.is_rx_nobuf++; 1443 ifp->if_ierrors++; 1444 return; 1445 } 1446 1447 /* attach Rx buffer to mbuf */ 1448 MEXTADD(mnew, rbuf->vaddr, IWN_RBUF_SIZE, 0, iwn_free_rbuf, 1449 rbuf); 1450 mnew->m_flags |= M_EXT_RW; 1451 SLIST_REMOVE_HEAD(&sc->rxq.freelist, next); 1452 1453 data->m = mnew; 1454 1455 /* update Rx descriptor */ 1456 ring->desc[ring->cur] = htole32(rbuf->paddr >> 8); 1457 } else { 1458 /* no free rbufs, copy frame */ 1459 m = m_dup(m, 0, M_COPYALL, M_DONTWAIT); 1460 if (m == NULL) { 1461 /* no free mbufs either, drop frame */ 1462 ic->ic_stats.is_rx_nobuf++; 1463 ifp->if_ierrors++; 1464 return; 1465 } 1466 } 1467 1468 rssi = iwn_get_rssi(stat); 1469 1470 if (ic->ic_state == IEEE80211_S_SCAN) 1471 iwn_fix_channel(ic, m); 1472 1473 #if NBPFILTER > 0 1474 if (sc->sc_drvbpf != NULL) { 1475 struct iwn_rx_radiotap_header *tap = &sc->sc_rxtap; 1476 1477 tap->wr_flags = 0; 1478 tap->wr_chan_freq = 1479 htole16(ic->ic_channels[stat->chan].ic_freq); 1480 tap->wr_chan_flags = 1481 htole16(ic->ic_channels[stat->chan].ic_flags); 1482 tap->wr_dbm_antsignal = (int8_t)rssi; 1483 tap->wr_dbm_antnoise = (int8_t)sc->noise; 1484 tap->wr_tsft = stat->tstamp; 1485 switch (stat->rate) { 1486 /* CCK rates */ 1487 case 10: tap->wr_rate = 2; break; 1488 case 20: tap->wr_rate = 4; break; 1489 case 55: tap->wr_rate = 11; break; 1490 case 110: tap->wr_rate = 22; break; 1491 /* OFDM rates */ 1492 case 0xd: tap->wr_rate = 12; break; 1493 case 0xf: tap->wr_rate = 18; break; 1494 case 0x5: tap->wr_rate = 24; break; 1495 case 0x7: tap->wr_rate = 36; break; 1496 case 0x9: tap->wr_rate = 48; break; 1497 case 0xb: tap->wr_rate = 72; break; 1498 case 0x1: tap->wr_rate = 96; break; 1499 case 0x3: tap->wr_rate = 108; break; 1500 /* unknown rate: should not happen */ 1501 default: tap->wr_rate = 0; 1502 } 1503 1504 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 1505 } 1506 #endif 1507 1508 /* grab a reference to the source node */ 1509 wh = mtod(m, struct ieee80211_frame *); 1510 ni = ieee80211_find_rxnode(ic,(struct ieee80211_frame_min *)wh); 1511 1512 /* send the frame to the 802.11 layer */ 1513 ieee80211_input(ic, m, ni, rssi, 0); 1514 1515 /* node is no longer needed */ 1516 ieee80211_free_node(ni); 1517 } 1518 1519 1520 /* 1521 * XXX: Hack to set the current channel to the value advertised in beacons or 1522 * probe responses. Only used during AP detection. 1523 * XXX: Duplicated from if_iwi.c 1524 */ 1525 static void 1526 iwn_fix_channel(struct ieee80211com *ic, struct mbuf *m) 1527 { 1528 struct ieee80211_frame *wh; 1529 uint8_t subtype; 1530 uint8_t *frm, *efrm; 1531 1532 wh = mtod(m, struct ieee80211_frame *); 1533 1534 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT) 1535 return; 1536 1537 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 1538 1539 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON && 1540 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1541 return; 1542 1543 frm = (uint8_t *)(wh + 1); 1544 efrm = mtod(m, uint8_t *) + m->m_len; 1545 1546 frm += 12; /* skip tstamp, bintval and capinfo fields */ 1547 while (frm < efrm) { 1548 if (*frm == IEEE80211_ELEMID_DSPARMS) 1549 #if IEEE80211_CHAN_MAX < 255 1550 if (frm[2] <= IEEE80211_CHAN_MAX) 1551 #endif 1552 ic->ic_curchan = &ic->ic_channels[frm[2]]; 1553 1554 frm += frm[1] + 2; 1555 } 1556 } 1557 1558 static void 1559 iwn_rx_statistics(struct iwn_softc *sc, struct iwn_rx_desc *desc) 1560 { 1561 struct ieee80211com *ic = &sc->sc_ic; 1562 struct iwn_calib_state *calib = &sc->calib; 1563 struct iwn_stats *stats = (struct iwn_stats *)(desc + 1); 1564 1565 /* ignore beacon statistics received during a scan */ 1566 if (ic->ic_state != IEEE80211_S_RUN) 1567 return; 1568 1569 DPRINTFN(3, ("received statistics (cmd=%d)\n", desc->type)); 1570 sc->calib_cnt = 0; /* reset timeout */ 1571 1572 /* test if temperature has changed */ 1573 if (stats->general.temp != sc->rawtemp) { 1574 int temp; 1575 1576 sc->rawtemp = stats->general.temp; 1577 temp = iwn_get_temperature(sc); 1578 DPRINTFN(2, ("temperature=%d\n", temp)); 1579 1580 /* update Tx power if need be */ 1581 iwn_power_calibration(sc, temp); 1582 } 1583 1584 if (desc->type != IWN_BEACON_STATISTICS) 1585 return; /* reply to a statistics request */ 1586 1587 sc->noise = iwn_get_noise(&stats->rx.general); 1588 DPRINTFN(3, ("noise=%d\n", sc->noise)); 1589 1590 /* test that RSSI and noise are present in stats report */ 1591 if (le32toh(stats->rx.general.flags) != 1) { 1592 DPRINTF(("received statistics without RSSI\n")); 1593 return; 1594 } 1595 1596 if (calib->state == IWN_CALIB_STATE_ASSOC) 1597 iwn_compute_differential_gain(sc, &stats->rx.general); 1598 else if (calib->state == IWN_CALIB_STATE_RUN) 1599 iwn_tune_sensitivity(sc, &stats->rx); 1600 } 1601 1602 static void 1603 iwn_tx_intr(struct iwn_softc *sc, struct iwn_rx_desc *desc) 1604 { 1605 struct ifnet *ifp = sc->sc_ic.ic_ifp; 1606 struct iwn_tx_ring *ring = &sc->txq[desc->qid & 0xf]; 1607 struct iwn_tx_data *txdata = &ring->data[desc->idx]; 1608 struct iwn_tx_stat *stat = (struct iwn_tx_stat *)(desc + 1); 1609 struct iwn_node *wn = (struct iwn_node *)txdata->ni; 1610 uint32_t status; 1611 1612 DPRINTFN(4, ("tx done: qid=%d idx=%d retries=%d nkill=%d rate=%x " 1613 "duration=%d status=%x\n", desc->qid, desc->idx, stat->ntries, 1614 stat->nkill, stat->rate, le16toh(stat->duration), 1615 le32toh(stat->status))); 1616 1617 /* 1618 * Update rate control statistics for the node. 1619 */ 1620 wn->amn.amn_txcnt++; 1621 if (stat->ntries > 0) { 1622 DPRINTFN(3, ("tx intr ntries %d\n", stat->ntries)); 1623 wn->amn.amn_retrycnt++; 1624 } 1625 1626 status = le32toh(stat->status) & 0xff; 1627 if (status != 1 && status != 2) 1628 ifp->if_oerrors++; 1629 else 1630 ifp->if_opackets++; 1631 1632 bus_dmamap_unload(sc->sc_dmat, txdata->map); 1633 m_freem(txdata->m); 1634 txdata->m = NULL; 1635 ieee80211_free_node(txdata->ni); 1636 txdata->ni = NULL; 1637 1638 ring->queued--; 1639 1640 sc->sc_tx_timer = 0; 1641 ifp->if_flags &= ~IFF_OACTIVE; 1642 iwn_start(ifp); 1643 } 1644 1645 static void 1646 iwn_cmd_intr(struct iwn_softc *sc, struct iwn_rx_desc *desc) 1647 { 1648 struct iwn_tx_ring *ring = &sc->txq[4]; 1649 struct iwn_tx_data *data; 1650 1651 if ((desc->qid & 0xf) != 4) 1652 return; /* not a command ack */ 1653 1654 data = &ring->data[desc->idx]; 1655 1656 /* if the command was mapped in a mbuf, free it */ 1657 if (data->m != NULL) { 1658 bus_dmamap_unload(sc->sc_dmat, data->map); 1659 m_freem(data->m); 1660 data->m = NULL; 1661 } 1662 1663 wakeup(&ring->cmd[desc->idx]); 1664 } 1665 1666 static void 1667 iwn_notif_intr(struct iwn_softc *sc) 1668 { 1669 struct ieee80211com *ic = &sc->sc_ic; 1670 struct ifnet *ifp = ic->ic_ifp; 1671 uint16_t hw; 1672 1673 hw = le16toh(sc->shared->closed_count); 1674 while (sc->rxq.cur != hw) { 1675 struct iwn_rx_data *data = &sc->rxq.data[sc->rxq.cur]; 1676 struct iwn_rx_desc *desc = (void *)data->m->m_ext.ext_buf; 1677 1678 DPRINTFN(4,("rx notification qid=%x idx=%d flags=%x type=%d " 1679 "len=%d\n", desc->qid, desc->idx, desc->flags, desc->type, 1680 le32toh(desc->len))); 1681 1682 if (!(desc->qid & 0x80)) /* reply to a command */ 1683 iwn_cmd_intr(sc, desc); 1684 1685 switch (desc->type) { 1686 case IWN_RX_DONE: 1687 case IWN_AMPDU_RX_DONE: 1688 iwn_rx_intr(sc, desc, data); 1689 break; 1690 1691 case IWN_AMPDU_RX_START: 1692 iwn_ampdu_rx_start(sc, desc); 1693 break; 1694 1695 case IWN_TX_DONE: 1696 /* a 802.11 frame has been transmitted */ 1697 iwn_tx_intr(sc, desc); 1698 break; 1699 1700 case IWN_RX_STATISTICS: 1701 case IWN_BEACON_STATISTICS: 1702 iwn_rx_statistics(sc, desc); 1703 break; 1704 1705 case IWN_BEACON_MISSED: 1706 { 1707 struct iwn_beacon_missed *miss = 1708 (struct iwn_beacon_missed *)(desc + 1); 1709 /* 1710 * If more than 5 consecutive beacons are missed, 1711 * reinitialize the sensitivity state machine. 1712 */ 1713 DPRINTFN(2, ("beacons missed %d/%d\n", 1714 le32toh(miss->consecutive), le32toh(miss->total))); 1715 if (ic->ic_state == IEEE80211_S_RUN && 1716 le32toh(miss->consecutive) > 5) 1717 (void)iwn_init_sensitivity(sc); 1718 break; 1719 } 1720 1721 case IWN_UC_READY: 1722 { 1723 struct iwn_ucode_info *uc = 1724 (struct iwn_ucode_info *)(desc + 1); 1725 1726 /* the microcontroller is ready */ 1727 DPRINTF(("microcode alive notification version=%d.%d " 1728 "subtype=%x alive=%x\n", uc->major, uc->minor, 1729 uc->subtype, le32toh(uc->valid))); 1730 1731 if (le32toh(uc->valid) != 1) { 1732 aprint_error_dev(sc->sc_dev, "microcontroller initialization " 1733 "failed\n"); 1734 break; 1735 } 1736 if (uc->subtype == IWN_UCODE_INIT) { 1737 /* save microcontroller's report */ 1738 memcpy(&sc->ucode_info, uc, sizeof (*uc)); 1739 } 1740 break; 1741 } 1742 case IWN_STATE_CHANGED: 1743 { 1744 uint32_t *status = (uint32_t *)(desc + 1); 1745 1746 /* enabled/disabled notification */ 1747 DPRINTF(("state changed to %x\n", le32toh(*status))); 1748 1749 if (le32toh(*status) & 1) { 1750 /* the radio button has to be pushed */ 1751 aprint_error_dev(sc->sc_dev, "Radio transmitter is off\n"); 1752 /* turn the interface down */ 1753 ifp->if_flags &= ~IFF_UP; 1754 iwn_stop(ifp, 1); 1755 return; /* no further processing */ 1756 } 1757 break; 1758 } 1759 case IWN_START_SCAN: 1760 { 1761 struct iwn_start_scan *scan = 1762 (struct iwn_start_scan *)(desc + 1); 1763 1764 DPRINTFN(2, ("scanning channel %d status %x\n", 1765 scan->chan, le32toh(scan->status))); 1766 1767 /* fix current channel */ 1768 ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan]; 1769 break; 1770 } 1771 case IWN_STOP_SCAN: 1772 { 1773 struct iwn_stop_scan *scan = 1774 (struct iwn_stop_scan *)(desc + 1); 1775 1776 DPRINTF(("scan finished nchan=%d status=%d chan=%d\n", 1777 scan->nchan, scan->status, scan->chan)); 1778 1779 if (scan->status == 1 && scan->chan <= 14) { 1780 /* 1781 * We just finished scanning 802.11g channels, 1782 * start scanning 802.11a ones. 1783 */ 1784 if (iwn_scan(sc, IEEE80211_CHAN_A) == 0) 1785 break; 1786 } 1787 sc->is_scanning = false; 1788 ieee80211_end_scan(ic); 1789 break; 1790 } 1791 } 1792 1793 sc->rxq.cur = (sc->rxq.cur + 1) % IWN_RX_RING_COUNT; 1794 } 1795 1796 /* tell the firmware what we have processed */ 1797 hw = (hw == 0) ? IWN_RX_RING_COUNT - 1 : hw - 1; 1798 IWN_WRITE(sc, IWN_RX_WIDX, hw & ~7); 1799 } 1800 1801 static int 1802 iwn_intr(void *arg) 1803 { 1804 struct iwn_softc *sc = arg; 1805 struct ifnet *ifp = sc->sc_ic.ic_ifp; 1806 uint32_t r1, r2; 1807 1808 /* disable interrupts */ 1809 IWN_WRITE(sc, IWN_MASK, 0); 1810 1811 r1 = IWN_READ(sc, IWN_INTR); 1812 r2 = IWN_READ(sc, IWN_INTR_STATUS); 1813 1814 if (r1 == 0 && r2 == 0) { 1815 if (ifp->if_flags & IFF_UP) 1816 IWN_WRITE(sc, IWN_MASK, IWN_INTR_MASK); 1817 return 0; /* not for us */ 1818 } 1819 1820 if (r1 == 0xffffffff) 1821 return 0; /* hardware gone */ 1822 1823 /* ack interrupts */ 1824 IWN_WRITE(sc, IWN_INTR, r1); 1825 IWN_WRITE(sc, IWN_INTR_STATUS, r2); 1826 1827 DPRINTFN(5, ("interrupt reg1=%x reg2=%x\n", r1, r2)); 1828 1829 if (r1 & IWN_RF_TOGGLED) { 1830 uint32_t tmp = IWN_READ(sc, IWN_GPIO_CTL); 1831 aprint_error_dev(sc->sc_dev, "RF switch: radio %s\n", 1832 (tmp & IWN_GPIO_RF_ENABLED) ? "enabled" : "disabled"); 1833 } 1834 if (r1 & IWN_CT_REACHED) { 1835 aprint_error_dev(sc->sc_dev, "critical temperature reached!\n"); 1836 } 1837 if (r1 & (IWN_SW_ERROR | IWN_HW_ERROR)) { 1838 aprint_error_dev(sc->sc_dev, "fatal firmware error\n"); 1839 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP; 1840 iwn_stop(sc->sc_ic.ic_ifp, 1); 1841 return 1; 1842 } 1843 1844 if ((r1 & (IWN_RX_INTR | IWN_SW_RX_INTR)) || 1845 (r2 & IWN_RX_STATUS_INTR)) 1846 iwn_notif_intr(sc); 1847 1848 if (r1 & IWN_ALIVE_INTR) 1849 wakeup(sc); 1850 1851 /* re-enable interrupts */ 1852 if (ifp->if_flags & IFF_UP) 1853 IWN_WRITE(sc, IWN_MASK, IWN_INTR_MASK); 1854 1855 return 1; 1856 } 1857 1858 static uint8_t 1859 iwn_plcp_signal(int rate) 1860 { 1861 switch (rate) { 1862 /* CCK rates (returned values are device-dependent) */ 1863 case 2: return 10; 1864 case 4: return 20; 1865 case 11: return 55; 1866 case 22: return 110; 1867 1868 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1869 /* R1-R4, (u)ral is R4-R1 */ 1870 case 12: return 0xd; 1871 case 18: return 0xf; 1872 case 24: return 0x5; 1873 case 36: return 0x7; 1874 case 48: return 0x9; 1875 case 72: return 0xb; 1876 case 96: return 0x1; 1877 case 108: return 0x3; 1878 case 120: return 0x3; 1879 } 1880 /* unknown rate (should not get there) */ 1881 return 0; 1882 } 1883 1884 /* determine if a given rate is CCK or OFDM */ 1885 #define IWN_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22) 1886 1887 static int 1888 iwn_tx_data(struct iwn_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1889 int ac) 1890 { 1891 struct ieee80211com *ic = &sc->sc_ic; 1892 struct iwn_tx_ring *ring = &sc->txq[ac]; 1893 struct iwn_tx_desc *desc; 1894 struct iwn_tx_data *data; 1895 struct iwn_tx_cmd *cmd; 1896 struct iwn_cmd_data *tx; 1897 struct ieee80211_frame *wh; 1898 struct ieee80211_key *k; 1899 const struct chanAccParams *cap; 1900 struct mbuf *mnew; 1901 bus_addr_t paddr; 1902 uint32_t flags; 1903 uint8_t type; 1904 int i, error, pad, rate, hdrlen, noack = 0; 1905 1906 desc = &ring->desc[ring->cur]; 1907 data = &ring->data[ring->cur]; 1908 1909 wh = mtod(m0, struct ieee80211_frame *); 1910 type = wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK; 1911 1912 if (IEEE80211_QOS_HAS_SEQ(wh)) { 1913 hdrlen = sizeof (struct ieee80211_qosframe); 1914 cap = &ic->ic_wme.wme_chanParams; 1915 noack = cap->cap_wmeParams[ac].wmep_noackPolicy; 1916 } else 1917 hdrlen = sizeof (struct ieee80211_frame); 1918 1919 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1920 k = ieee80211_crypto_encap(ic, ni, m0); 1921 if (k == NULL) { 1922 m_freem(m0); 1923 return ENOBUFS; 1924 } 1925 /* packet header may have moved, reset our local pointer */ 1926 wh = mtod(m0, struct ieee80211_frame *); 1927 } 1928 1929 /* pickup a rate */ 1930 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 1931 IEEE80211_FC0_TYPE_MGT) { 1932 /* mgmt frames are sent at the lowest available bit-rate */ 1933 rate = ni->ni_rates.rs_rates[0]; 1934 } else { 1935 if (ic->ic_fixed_rate != -1) { 1936 rate = ic->ic_sup_rates[ic->ic_curmode]. 1937 rs_rates[ic->ic_fixed_rate]; 1938 } else 1939 rate = ni->ni_rates.rs_rates[ni->ni_txrate]; 1940 } 1941 rate &= IEEE80211_RATE_VAL; 1942 1943 #if NBPFILTER > 0 1944 if (sc->sc_drvbpf != NULL) { 1945 struct iwn_tx_radiotap_header *tap = &sc->sc_txtap; 1946 1947 tap->wt_flags = 0; 1948 tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq); 1949 tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags); 1950 tap->wt_rate = rate; 1951 tap->wt_hwqueue = ac; 1952 if (wh->i_fc[1] & IEEE80211_FC1_WEP) 1953 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP; 1954 1955 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1956 } 1957 #endif 1958 1959 cmd = &ring->cmd[ring->cur]; 1960 cmd->code = IWN_CMD_TX_DATA; 1961 cmd->flags = 0; 1962 cmd->qid = ring->qid; 1963 cmd->idx = ring->cur; 1964 1965 tx = (struct iwn_cmd_data *)cmd->data; 1966 1967 flags = IWN_TX_AUTO_SEQ; 1968 if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)){ 1969 flags |= IWN_TX_NEED_ACK; 1970 }else if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold) 1971 flags |= (IWN_TX_NEED_RTS | IWN_TX_FULL_TXOP); 1972 1973 tx->id = IEEE80211_IS_MULTICAST(wh->i_addr1) ? IWN_ID_BROADCAST : IWN_ID_BSS; 1974 1975 if (type == IEEE80211_FC0_TYPE_MGT) { 1976 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 1977 1978 /* tell h/w to set timestamp in probe responses */ 1979 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1980 flags |= IWN_TX_INSERT_TSTAMP; 1981 1982 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ || 1983 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) 1984 tx->timeout = htole16(3); 1985 else 1986 tx->timeout = htole16(2); 1987 } else 1988 tx->timeout = htole16(0); 1989 1990 if (hdrlen & 3) { 1991 /* first segment's length must be a multiple of 4 */ 1992 flags |= IWN_TX_NEED_PADDING; 1993 pad = 4 - (hdrlen & 3); 1994 } else 1995 pad = 0; 1996 1997 tx->flags = htole32(flags); 1998 tx->len = htole16(m0->m_pkthdr.len); 1999 tx->rate = iwn_plcp_signal(rate); 2000 tx->rts_ntries = 60; 2001 tx->data_ntries = 15; 2002 tx->lifetime = htole32(IWN_LIFETIME_INFINITE); 2003 2004 /* XXX alternate between Ant A and Ant B ? */ 2005 tx->rflags = IWN_RFLAG_ANT_B; 2006 if (tx->id == IWN_ID_BROADCAST) { 2007 tx->ridx = IWN_MAX_TX_RETRIES - 1; 2008 if (!IWN_RATE_IS_OFDM(rate)) 2009 tx->rflags |= IWN_RFLAG_CCK; 2010 } else { 2011 tx->ridx = 0; 2012 /* tell adapter to ignore rflags */ 2013 tx->flags |= htole32(IWN_TX_USE_NODE_RATE); 2014 } 2015 2016 /* copy and trim IEEE802.11 header */ 2017 memcpy((uint8_t *)(tx + 1), wh, hdrlen); 2018 m_adj(m0, hdrlen); 2019 2020 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, 2021 BUS_DMA_WRITE | BUS_DMA_NOWAIT); 2022 if (error != 0 && error != EFBIG) { 2023 aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n", error); 2024 m_freem(m0); 2025 return error; 2026 } 2027 if (error != 0) { 2028 /* too many fragments, linearize */ 2029 2030 MGETHDR(mnew, M_DONTWAIT, MT_DATA); 2031 if (mnew == NULL) { 2032 m_freem(m0); 2033 return ENOMEM; 2034 } 2035 M_COPY_PKTHDR(mnew, m0); 2036 if (m0->m_pkthdr.len > MHLEN) { 2037 MCLGET(mnew, M_DONTWAIT); 2038 if (!(mnew->m_flags & M_EXT)) { 2039 m_freem(m0); 2040 m_freem(mnew); 2041 return ENOMEM; 2042 } 2043 } 2044 2045 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *)); 2046 m_freem(m0); 2047 mnew->m_len = mnew->m_pkthdr.len; 2048 m0 = mnew; 2049 2050 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, 2051 BUS_DMA_WRITE | BUS_DMA_NOWAIT); 2052 if (error != 0) { 2053 aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n", error); 2054 m_freem(m0); 2055 return error; 2056 } 2057 } 2058 2059 data->m = m0; 2060 data->ni = ni; 2061 2062 DPRINTFN(4, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n", 2063 ring->qid, ring->cur, m0->m_pkthdr.len, data->map->dm_nsegs)); 2064 2065 paddr = ring->cmd_dma.paddr + ring->cur * sizeof (struct iwn_tx_cmd); 2066 tx->loaddr = htole32(paddr + 4 + 2067 offsetof(struct iwn_cmd_data, ntries)); 2068 tx->hiaddr = 0; /* limit to 32-bit physical addresses */ 2069 2070 /* first scatter/gather segment is used by the tx data command */ 2071 IWN_SET_DESC_NSEGS(desc, 1 + data->map->dm_nsegs); 2072 IWN_SET_DESC_SEG(desc, 0, paddr, 4 + sizeof (*tx) + hdrlen + pad); 2073 for (i = 1; i <= data->map->dm_nsegs; i++) { 2074 IWN_SET_DESC_SEG(desc, i, data->map->dm_segs[i - 1].ds_addr, 2075 data->map->dm_segs[i - 1].ds_len); 2076 } 2077 sc->shared->len[ring->qid][ring->cur] = 2078 htole16(hdrlen + m0->m_pkthdr.len + 8); 2079 if (ring->cur < IWN_TX_WINDOW) { 2080 sc->shared->len[ring->qid][ring->cur + IWN_TX_RING_COUNT] = 2081 htole16(hdrlen + m0->m_pkthdr.len + 8); 2082 } 2083 2084 ring->queued++; 2085 2086 /* kick ring */ 2087 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT; 2088 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur); 2089 2090 return 0; 2091 } 2092 2093 static void 2094 iwn_start(struct ifnet *ifp) 2095 { 2096 struct iwn_softc *sc = ifp->if_softc; 2097 struct ieee80211com *ic = &sc->sc_ic; 2098 struct ieee80211_node *ni; 2099 struct ether_header *eh; 2100 struct mbuf *m0; 2101 int ac; 2102 2103 /* 2104 * net80211 may still try to send management frames even if the 2105 * IFF_RUNNING flag is not set... 2106 */ 2107 if ((ifp->if_flags & (IFF_RUNNING | IFF_OACTIVE)) != IFF_RUNNING) 2108 return; 2109 2110 for (;;) { 2111 IF_DEQUEUE(&ic->ic_mgtq, m0); 2112 if (m0 != NULL) { 2113 /* management frames go into ring 0 */ 2114 2115 2116 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif; 2117 m0->m_pkthdr.rcvif = NULL; 2118 2119 /* management goes into ring 0 */ 2120 if (sc->txq[0].queued > sc->txq[0].count - 8) { 2121 ifp->if_oerrors++; 2122 continue; 2123 } 2124 2125 #if NBPFILTER > 0 2126 if (ic->ic_rawbpf != NULL) 2127 bpf_mtap(ic->ic_rawbpf, m0); 2128 #endif 2129 if (iwn_tx_data(sc, m0, ni, 0) != 0) { 2130 ifp->if_oerrors++; 2131 break; 2132 } 2133 } else { 2134 if (ic->ic_state != IEEE80211_S_RUN) 2135 break; 2136 IFQ_POLL(&ifp->if_snd, m0); 2137 if (m0 == NULL) 2138 break; 2139 2140 if (m0->m_len < sizeof (*eh) && 2141 (m0 = m_pullup(m0, sizeof (*eh))) == NULL) { 2142 ifp->if_oerrors++; 2143 continue; 2144 } 2145 eh = mtod(m0, struct ether_header *); 2146 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 2147 if (ni == NULL) { 2148 m_freem(m0); 2149 ifp->if_oerrors++; 2150 continue; 2151 } 2152 /* classify mbuf so we can find which tx ring to use */ 2153 if (ieee80211_classify(ic, m0, ni) != 0) { 2154 m_freem(m0); 2155 ieee80211_free_node(ni); 2156 ifp->if_oerrors++; 2157 continue; 2158 } 2159 2160 /* no QoS encapsulation for EAPOL frames */ 2161 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ? 2162 M_WME_GETAC(m0) : WME_AC_BE; 2163 2164 if (sc->txq[ac].queued > sc->txq[ac].count - 8) { 2165 2166 /* there is no place left in this ring */ 2167 ifp->if_flags |= IFF_OACTIVE; 2168 break; 2169 } 2170 IFQ_DEQUEUE(&ifp->if_snd, m0); 2171 #if NBPFILTER > 0 2172 if (ifp->if_bpf != NULL) 2173 bpf_mtap(ifp->if_bpf, m0); 2174 #endif 2175 m0 = ieee80211_encap(ic, m0, ni); 2176 if (m0 == NULL) { 2177 ieee80211_free_node(ni); 2178 ifp->if_oerrors++; 2179 continue; 2180 } 2181 #if NBPFILTER > 0 2182 if (ic->ic_rawbpf != NULL) 2183 bpf_mtap(ic->ic_rawbpf, m0); 2184 #endif 2185 if (iwn_tx_data(sc, m0, ni, ac) != 0) { 2186 ieee80211_free_node(ni); 2187 ifp->if_oerrors++; 2188 break; 2189 } 2190 } 2191 2192 sc->sc_tx_timer = 5; 2193 ifp->if_timer = 1; 2194 } 2195 } 2196 2197 static void 2198 iwn_watchdog(struct ifnet *ifp) 2199 { 2200 struct iwn_softc *sc = ifp->if_softc; 2201 2202 ifp->if_timer = 0; 2203 2204 if (sc->sc_tx_timer > 0) { 2205 if (--sc->sc_tx_timer == 0) { 2206 aprint_error_dev(sc->sc_dev, "device timeout\n"); 2207 ifp->if_flags &= ~IFF_UP; 2208 iwn_stop(ifp, 1); 2209 ifp->if_oerrors++; 2210 return; 2211 } 2212 ifp->if_timer = 1; 2213 } 2214 2215 ieee80211_watchdog(&sc->sc_ic); 2216 } 2217 2218 static int 2219 iwn_ioctl(struct ifnet *ifp, u_long cmd, void * data) 2220 { 2221 2222 #define IS_RUNNING(ifp) \ 2223 ((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING)) 2224 2225 struct iwn_softc *sc = ifp->if_softc; 2226 struct ieee80211com *ic = &sc->sc_ic; 2227 int s, error = 0; 2228 2229 s = splnet(); 2230 2231 switch (cmd) { 2232 case SIOCSIFFLAGS: 2233 if (ifp->if_flags & IFF_UP) { 2234 if (!(ifp->if_flags & IFF_RUNNING)) 2235 iwn_init(ifp); 2236 } else { 2237 if (ifp->if_flags & IFF_RUNNING) 2238 iwn_stop(ifp, 1); 2239 } 2240 break; 2241 2242 case SIOCADDMULTI: 2243 case SIOCDELMULTI: 2244 /* XXX no h/w multicast filter? --dyoung */ 2245 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) { 2246 /* setup multicast filter, etc */ 2247 error = 0; 2248 } 2249 break; 2250 2251 default: 2252 error = ieee80211_ioctl(ic, cmd, data); 2253 } 2254 2255 if (error == ENETRESET) { 2256 if (IS_RUNNING(ifp) && 2257 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)) 2258 iwn_init(ifp); 2259 error = 0; 2260 } 2261 2262 splx(s); 2263 return error; 2264 2265 #undef IS_RUNNING 2266 } 2267 2268 static void 2269 iwn_read_eeprom(struct iwn_softc *sc) 2270 { 2271 struct ieee80211com *ic = &sc->sc_ic; 2272 char domain[4]; 2273 uint16_t val; 2274 int i, error; 2275 2276 if ((error = iwn_eeprom_lock(sc)) != 0) { 2277 aprint_error_dev(sc->sc_dev, "could not lock EEPROM (error=%d)\n", error); 2278 return; 2279 } 2280 /* read and print regulatory domain */ 2281 iwn_read_prom_data(sc, IWN_EEPROM_DOMAIN, domain, 4); 2282 aprint_error_dev(sc->sc_dev, "%.4s", domain); 2283 2284 /* read and print MAC address */ 2285 iwn_read_prom_data(sc, IWN_EEPROM_MAC, ic->ic_myaddr, 6); 2286 aprint_error(", address %s\n", ether_sprintf(ic->ic_myaddr)); 2287 2288 /* read the list of authorized channels */ 2289 for (i = 0; i < IWN_CHAN_BANDS_COUNT; i++) 2290 iwn_read_eeprom_channels(sc, i); 2291 2292 /* read maximum allowed Tx power for 2GHz and 5GHz bands */ 2293 iwn_read_prom_data(sc, IWN_EEPROM_MAXPOW, &val, 2); 2294 sc->maxpwr2GHz = val & 0xff; 2295 sc->maxpwr5GHz = val >> 8; 2296 /* check that EEPROM values are correct */ 2297 if (sc->maxpwr5GHz < 20 || sc->maxpwr5GHz > 50) 2298 sc->maxpwr5GHz = 38; 2299 if (sc->maxpwr2GHz < 20 || sc->maxpwr2GHz > 50) 2300 sc->maxpwr2GHz = 38; 2301 DPRINTF(("maxpwr 2GHz=%d 5GHz=%d\n", sc->maxpwr2GHz, sc->maxpwr5GHz)); 2302 2303 /* read voltage at which samples were taken */ 2304 iwn_read_prom_data(sc, IWN_EEPROM_VOLTAGE, &val, 2); 2305 sc->eeprom_voltage = (int16_t)le16toh(val); 2306 DPRINTF(("voltage=%d (in 0.3V)\n", sc->eeprom_voltage)); 2307 2308 /* read power groups */ 2309 iwn_read_prom_data(sc, IWN_EEPROM_BANDS, sc->bands, sizeof sc->bands); 2310 #ifdef IWN_DEBUG 2311 if (iwn_debug > 0) { 2312 for (i = 0; i < IWN_NBANDS; i++) 2313 iwn_print_power_group(sc, i); 2314 } 2315 #endif 2316 iwn_eeprom_unlock(sc); 2317 } 2318 2319 static void 2320 iwn_read_eeprom_channels(struct iwn_softc *sc, int n) 2321 { 2322 struct ieee80211com *ic = &sc->sc_ic; 2323 const struct iwn_chan_band *band = &iwn_bands[n]; 2324 struct iwn_eeprom_chan channels[IWN_MAX_CHAN_PER_BAND]; 2325 int chan, i; 2326 2327 iwn_read_prom_data(sc, band->addr, channels, 2328 band->nchan * sizeof (struct iwn_eeprom_chan)); 2329 2330 for (i = 0; i < band->nchan; i++) { 2331 if (!(channels[i].flags & IWN_EEPROM_CHAN_VALID)) 2332 continue; 2333 2334 chan = band->chan[i]; 2335 2336 if (n == 0) { /* 2GHz band */ 2337 ic->ic_channels[chan].ic_freq = 2338 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ); 2339 ic->ic_channels[chan].ic_flags = 2340 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 2341 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 2342 2343 } else { /* 5GHz band */ 2344 /* 2345 * Some adapters support channels 7, 8, 11 and 12 2346 * both in the 2GHz *and* 5GHz bands. 2347 * Because of limitations in our net80211(9) stack, 2348 * we can't support these channels in 5GHz band. 2349 */ 2350 if (chan <= 14) 2351 continue; 2352 2353 ic->ic_channels[chan].ic_freq = 2354 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ); 2355 ic->ic_channels[chan].ic_flags = IEEE80211_CHAN_A; 2356 } 2357 2358 /* is active scan allowed on this channel? */ 2359 if (!(channels[i].flags & IWN_EEPROM_CHAN_ACTIVE)) { 2360 ic->ic_channels[chan].ic_flags |= 2361 IEEE80211_CHAN_PASSIVE; 2362 } 2363 2364 /* save maximum allowed power for this channel */ 2365 sc->maxpwr[chan] = channels[i].maxpwr; 2366 2367 DPRINTF(("adding chan %d flags=0x%x maxpwr=%d\n", 2368 chan, channels[i].flags, sc->maxpwr[chan])); 2369 } 2370 } 2371 2372 #ifdef IWN_DEBUG 2373 static void 2374 iwn_print_power_group(struct iwn_softc *sc, int i) 2375 { 2376 struct iwn_eeprom_band *band = &sc->bands[i]; 2377 struct iwn_eeprom_chan_samples *chans = band->chans; 2378 int j, c; 2379 2380 DPRINTF(("===band %d===\n", i)); 2381 DPRINTF(("chan lo=%d, chan hi=%d\n", band->lo, band->hi)); 2382 DPRINTF(("chan1 num=%d\n", chans[0].num)); 2383 for (c = 0; c < IWN_NTXCHAINS; c++) { 2384 for (j = 0; j < IWN_NSAMPLES; j++) { 2385 DPRINTF(("chain %d, sample %d: temp=%d gain=%d " 2386 "power=%d pa_det=%d\n", c, j, 2387 chans[0].samples[c][j].temp, 2388 chans[0].samples[c][j].gain, 2389 chans[0].samples[c][j].power, 2390 chans[0].samples[c][j].pa_det)); 2391 } 2392 } 2393 DPRINTF(("chan2 num=%d\n", chans[1].num)); 2394 for (c = 0; c < IWN_NTXCHAINS; c++) { 2395 for (j = 0; j < IWN_NSAMPLES; j++) { 2396 DPRINTF(("chain %d, sample %d: temp=%d gain=%d " 2397 "power=%d pa_det=%d\n", c, j, 2398 chans[1].samples[c][j].temp, 2399 chans[1].samples[c][j].gain, 2400 chans[1].samples[c][j].power, 2401 chans[1].samples[c][j].pa_det)); 2402 } 2403 } 2404 } 2405 #endif 2406 2407 /* 2408 * Send a command to the firmware. 2409 */ 2410 static int 2411 iwn_cmd(struct iwn_softc *sc, int code, const void *buf, int size, int async) 2412 { 2413 struct iwn_tx_ring *ring = &sc->txq[4]; 2414 struct iwn_tx_desc *desc; 2415 struct iwn_tx_cmd *cmd; 2416 bus_addr_t paddr; 2417 2418 KASSERT(size <= sizeof cmd->data); 2419 2420 desc = &ring->desc[ring->cur]; 2421 cmd = &ring->cmd[ring->cur]; 2422 2423 cmd->code = code; 2424 cmd->flags = 0; 2425 cmd->qid = ring->qid; 2426 cmd->idx = ring->cur; 2427 memcpy(cmd->data, buf, size); 2428 2429 paddr = ring->cmd_dma.paddr + ring->cur * sizeof (struct iwn_tx_cmd); 2430 2431 IWN_SET_DESC_NSEGS(desc, 1); 2432 IWN_SET_DESC_SEG(desc, 0, paddr, 4 + size); 2433 sc->shared->len[ring->qid][ring->cur] = htole16(8); 2434 if (ring->cur < IWN_TX_WINDOW) { 2435 sc->shared->len[ring->qid][ring->cur + IWN_TX_RING_COUNT] = 2436 htole16(8); 2437 } 2438 2439 /* kick cmd ring */ 2440 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT; 2441 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur); 2442 2443 return async ? 0 : tsleep(cmd, PCATCH, "iwncmd", hz); 2444 } 2445 2446 /* 2447 * Configure hardware multi-rate retries for one node. 2448 */ 2449 static int 2450 iwn_setup_node_mrr(struct iwn_softc *sc, uint8_t id, int async) 2451 { 2452 struct ieee80211com *ic = &sc->sc_ic; 2453 struct iwn_cmd_mrr mrr; 2454 int i, ridx; 2455 2456 memset(&mrr, 0, sizeof mrr); 2457 mrr.id = id; 2458 mrr.ssmask = 2; 2459 mrr.dsmask = 3; 2460 mrr.ampdu_disable = 3; 2461 mrr.ampdu_limit = 4000; 2462 2463 if (id == IWN_ID_BSS) 2464 ridx = IWN_OFDM54; 2465 else if (ic->ic_curmode == IEEE80211_MODE_11A) 2466 ridx = IWN_OFDM6; 2467 else 2468 ridx = IWN_CCK1; 2469 for (i = 0; i < IWN_MAX_TX_RETRIES; i++) { 2470 mrr.table[i].rate = iwn_ridx_to_plcp[ridx]; 2471 mrr.table[i].rflags = IWN_RFLAG_ANT_B; 2472 if (ridx <= IWN_CCK11) 2473 mrr.table[i].rflags |= IWN_RFLAG_CCK; 2474 ridx = iwn_prev_ridx[ridx]; 2475 } 2476 return iwn_cmd(sc, IWN_CMD_NODE_MRR_SETUP, &mrr, sizeof mrr, async); 2477 } 2478 2479 static int 2480 iwn_wme_update(struct ieee80211com *ic) 2481 { 2482 #define IWN_EXP2(v) htole16((1 << (v)) - 1) 2483 #define IWN_USEC(v) htole16(IEEE80211_TXOP_TO_US(v)) 2484 struct iwn_softc *sc = ic->ic_ifp->if_softc; 2485 const struct wmeParams *wmep; 2486 struct iwn_wme_setup wme; 2487 int ac; 2488 2489 /* don't override default WME values if WME is not actually enabled */ 2490 if (!(ic->ic_flags & IEEE80211_F_WME)) 2491 return 0; 2492 2493 wme.flags = 0; 2494 for (ac = 0; ac < WME_NUM_AC; ac++) { 2495 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac]; 2496 wme.ac[ac].aifsn = wmep->wmep_aifsn; 2497 wme.ac[ac].cwmin = IWN_EXP2(wmep->wmep_logcwmin); 2498 wme.ac[ac].cwmax = IWN_EXP2(wmep->wmep_logcwmax); 2499 wme.ac[ac].txop = IWN_USEC(wmep->wmep_txopLimit); 2500 2501 DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d " 2502 "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin, 2503 wme.ac[ac].cwmax, wme.ac[ac].txop)); 2504 } 2505 2506 return iwn_cmd(sc, IWN_CMD_SET_WME, &wme, sizeof wme, 1); 2507 #undef IWN_USEC 2508 #undef IWN_EXP2 2509 } 2510 2511 2512 2513 static void 2514 iwn_set_led(struct iwn_softc *sc, uint8_t which, uint8_t off, uint8_t on) 2515 { 2516 struct iwn_cmd_led led; 2517 2518 led.which = which; 2519 led.unit = htole32(100000); /* on/off in unit of 100ms */ 2520 led.off = off; 2521 led.on = on; 2522 2523 (void)iwn_cmd(sc, IWN_CMD_SET_LED, &led, sizeof led, 1); 2524 } 2525 2526 /* 2527 * Set the critical temperature at which the firmware will automatically stop 2528 * the radio transmitter. 2529 */ 2530 static int 2531 iwn_set_critical_temp(struct iwn_softc *sc) 2532 { 2533 struct iwn_ucode_info *uc = &sc->ucode_info; 2534 struct iwn_critical_temp crit; 2535 uint32_t r1, r2, r3, temp; 2536 2537 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_CTEMP_STOP_RF); 2538 2539 r1 = le32toh(uc->temp[0].chan20MHz); 2540 r2 = le32toh(uc->temp[1].chan20MHz); 2541 r3 = le32toh(uc->temp[2].chan20MHz); 2542 /* inverse function of iwn_get_temperature() */ 2543 2544 temp = r2 + ((IWN_CTOK(110) * (r3 - r1)) / 259); 2545 2546 memset(&crit, 0, sizeof crit); 2547 crit.tempR = htole32(temp); 2548 DPRINTF(("setting critical temperature to %u\n", temp)); 2549 return iwn_cmd(sc, IWN_CMD_SET_CRITICAL_TEMP, &crit, sizeof crit, 0); 2550 } 2551 2552 static void 2553 iwn_enable_tsf(struct iwn_softc *sc, struct ieee80211_node *ni) 2554 { 2555 struct iwn_cmd_tsf tsf; 2556 uint64_t val, mod; 2557 2558 memset(&tsf, 0, sizeof tsf); 2559 memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8); 2560 tsf.bintval = htole16(ni->ni_intval); 2561 tsf.lintval = htole16(10); 2562 2563 /* compute remaining time until next beacon */ 2564 val = (uint64_t)ni->ni_intval * 1024; /* msecs -> usecs */ 2565 mod = le64toh(tsf.tstamp) % val; 2566 tsf.binitval = htole32((uint32_t)(val - mod)); 2567 2568 DPRINTF(("TSF bintval=%u tstamp=%" PRIu64 ", init=%" PRIu64 "\n", 2569 ni->ni_intval, le64toh(tsf.tstamp), val - mod)); 2570 2571 if (iwn_cmd(sc, IWN_CMD_TSF, &tsf, sizeof tsf, 1) != 0) 2572 aprint_error_dev(sc->sc_dev, "could not enable TSF\n"); 2573 } 2574 2575 static void 2576 iwn_power_calibration(struct iwn_softc *sc, int temp) 2577 { 2578 struct ieee80211com *ic = &sc->sc_ic; 2579 2580 DPRINTF(("temperature %d->%d\n", sc->temp, temp)); 2581 2582 /* adjust Tx power if need be (delta >= 3�C) */ 2583 if (abs(temp - sc->temp) < 3) 2584 return; 2585 2586 sc->temp = temp; 2587 2588 DPRINTF(("setting Tx power for channel %d\n", 2589 ieee80211_chan2ieee(ic, ic->ic_bss->ni_chan))); 2590 if (iwn_set_txpower(sc, ic->ic_bss->ni_chan, 1) != 0) { 2591 /* just warn, too bad for the automatic calibration... */ 2592 aprint_error_dev(sc->sc_dev, "could not adjust Tx power\n"); 2593 } 2594 } 2595 2596 /* 2597 * Set Tx power for a given channel (each rate has its own power settings). 2598 * This function takes into account the regulatory information from EEPROM, 2599 * the current temperature and the current voltage. 2600 */ 2601 static int 2602 iwn_set_txpower(struct iwn_softc *sc, struct ieee80211_channel *ch, int async) 2603 { 2604 /* fixed-point arithmetic division using a n-bit fractional part */ 2605 #define fdivround(a, b, n) \ 2606 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n)) 2607 /* linear interpolation */ 2608 #define interpolate(x, x1, y1, x2, y2, n) \ 2609 ((y1) + fdivround(((int)(x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n)) 2610 2611 static const int tdiv[IWN_NATTEN_GROUPS] = { 9, 8, 8, 8, 6 }; 2612 struct ieee80211com *ic = &sc->sc_ic; 2613 struct iwn_ucode_info *uc = &sc->ucode_info; 2614 struct iwn_cmd_txpower cmd; 2615 struct iwn_eeprom_chan_samples *chans; 2616 const uint8_t *rf_gain, *dsp_gain; 2617 int32_t vdiff, tdiff; 2618 int i, c, grp, maxpwr; 2619 u_int chan; 2620 2621 /* get channel number */ 2622 chan = ieee80211_chan2ieee(ic, ch); 2623 2624 memset(&cmd, 0, sizeof cmd); 2625 cmd.band = IEEE80211_IS_CHAN_5GHZ(ch) ? 0 : 1; 2626 cmd.chan = chan; 2627 2628 if (IEEE80211_IS_CHAN_5GHZ(ch)) { 2629 maxpwr = sc->maxpwr5GHz; 2630 rf_gain = iwn_rf_gain_5ghz; 2631 dsp_gain = iwn_dsp_gain_5ghz; 2632 } else { 2633 maxpwr = sc->maxpwr2GHz; 2634 rf_gain = iwn_rf_gain_2ghz; 2635 dsp_gain = iwn_dsp_gain_2ghz; 2636 } 2637 2638 /* compute voltage compensation */ 2639 vdiff = ((int32_t)le32toh(uc->volt) - sc->eeprom_voltage) / 7; 2640 if (vdiff > 0) 2641 vdiff *= 2; 2642 if (abs(vdiff) > 2) 2643 vdiff = 0; 2644 DPRINTF(("voltage compensation=%d (UCODE=%d, EEPROM=%d)\n", 2645 vdiff, le32toh(uc->volt), sc->eeprom_voltage)); 2646 2647 /* get channel's attenuation group */ 2648 if (chan <= 20) /* 1-20 */ 2649 grp = 4; 2650 else if (chan <= 43) /* 34-43 */ 2651 grp = 0; 2652 else if (chan <= 70) /* 44-70 */ 2653 grp = 1; 2654 else if (chan <= 124) /* 71-124 */ 2655 grp = 2; 2656 else /* 125-200 */ 2657 grp = 3; 2658 DPRINTF(("chan %d, attenuation group=%d\n", chan, grp)); 2659 2660 /* get channel's sub-band */ 2661 for (i = 0; i < IWN_NBANDS; i++) 2662 if (sc->bands[i].lo != 0 && 2663 sc->bands[i].lo <= chan && chan <= sc->bands[i].hi) 2664 break; 2665 chans = sc->bands[i].chans; 2666 DPRINTF(("chan %d sub-band=%d\n", chan, i)); 2667 2668 for (c = 0; c < IWN_NTXCHAINS; c++) { 2669 uint8_t power, gain, temp; 2670 int maxchpwr, pwr, ridx, idx; 2671 2672 power = interpolate(chan, 2673 chans[0].num, chans[0].samples[c][1].power, 2674 chans[1].num, chans[1].samples[c][1].power, 1); 2675 gain = interpolate(chan, 2676 chans[0].num, chans[0].samples[c][1].gain, 2677 chans[1].num, chans[1].samples[c][1].gain, 1); 2678 temp = interpolate(chan, 2679 chans[0].num, chans[0].samples[c][1].temp, 2680 chans[1].num, chans[1].samples[c][1].temp, 1); 2681 DPRINTF(("Tx chain %d: power=%d gain=%d temp=%d\n", 2682 c, power, gain, temp)); 2683 2684 /* compute temperature compensation */ 2685 tdiff = ((sc->temp - temp) * 2) / tdiv[grp]; 2686 DPRINTF(("temperature compensation=%d (current=%d, " 2687 "EEPROM=%d)\n", tdiff, sc->temp, temp)); 2688 2689 for (ridx = 0; ridx <= IWN_RIDX_MAX; ridx++) { 2690 maxchpwr = sc->maxpwr[chan] * 2; 2691 if ((ridx / 8) & 1) { 2692 /* MIMO: decrease Tx power (-3dB) */ 2693 maxchpwr -= 6; 2694 } 2695 2696 pwr = maxpwr - 10; 2697 2698 /* decrease power for highest OFDM rates */ 2699 if ((ridx % 8) == 5) /* 48Mbit/s */ 2700 pwr -= 5; 2701 else if ((ridx % 8) == 6) /* 54Mbit/s */ 2702 pwr -= 7; 2703 else if ((ridx % 8) == 7) /* 60Mbit/s */ 2704 pwr -= 10; 2705 2706 if (pwr > maxchpwr) 2707 pwr = maxchpwr; 2708 2709 idx = gain - (pwr - power) - tdiff - vdiff; 2710 if ((ridx / 8) & 1) /* MIMO */ 2711 idx += (int32_t)le32toh(uc->atten[grp][c]); 2712 2713 if (cmd.band == 0) 2714 idx += 9; /* 5GHz */ 2715 if (ridx == IWN_RIDX_MAX) 2716 idx += 5; /* CCK */ 2717 2718 /* make sure idx stays in a valid range */ 2719 if (idx < 0) 2720 idx = 0; 2721 else if (idx > IWN_MAX_PWR_INDEX) 2722 idx = IWN_MAX_PWR_INDEX; 2723 2724 DPRINTF(("Tx chain %d, rate idx %d: power=%d\n", 2725 c, ridx, idx)); 2726 cmd.power[ridx].rf_gain[c] = rf_gain[idx]; 2727 cmd.power[ridx].dsp_gain[c] = dsp_gain[idx]; 2728 } 2729 } 2730 2731 DPRINTF(("setting tx power for chan %d\n", chan)); 2732 return iwn_cmd(sc, IWN_CMD_TXPOWER, &cmd, sizeof cmd, async); 2733 2734 #undef interpolate 2735 #undef fdivround 2736 } 2737 2738 /* 2739 * Get the best (maximum) RSSI among Rx antennas (in dBm). 2740 */ 2741 static int 2742 iwn_get_rssi(const struct iwn_rx_stat *stat) 2743 { 2744 uint8_t mask, agc; 2745 int rssi; 2746 2747 mask = (le16toh(stat->antenna) >> 4) & 0x7; 2748 agc = (le16toh(stat->agc) >> 7) & 0x7f; 2749 2750 rssi = 0; 2751 if (mask & (1 << 0)) /* Ant A */ 2752 rssi = max(rssi, stat->rssi[0]); 2753 if (mask & (1 << 1)) /* Ant B */ 2754 rssi = max(rssi, stat->rssi[2]); 2755 if (mask & (1 << 2)) /* Ant C */ 2756 rssi = max(rssi, stat->rssi[4]); 2757 2758 return rssi - agc - IWN_RSSI_TO_DBM; 2759 } 2760 2761 /* 2762 * Get the average noise among Rx antennas (in dBm). 2763 */ 2764 static int 2765 iwn_get_noise(const struct iwn_rx_general_stats *stats) 2766 { 2767 int i, total, nbant, noise; 2768 2769 total = nbant = 0; 2770 for (i = 0; i < 3; i++) { 2771 if ((noise = le32toh(stats->noise[i]) & 0xff) == 0) 2772 continue; 2773 total += noise; 2774 nbant++; 2775 } 2776 /* there should be at least one antenna but check anyway */ 2777 return (nbant == 0) ? -127 : (total / nbant) - 107; 2778 } 2779 2780 /* 2781 * Read temperature (in degC) from the on-board thermal sensor. 2782 */ 2783 static int 2784 iwn_get_temperature(struct iwn_softc *sc) 2785 { 2786 struct iwn_ucode_info *uc = &sc->ucode_info; 2787 int32_t r1, r2, r3, r4, temp; 2788 2789 r1 = le32toh(uc->temp[0].chan20MHz); 2790 r2 = le32toh(uc->temp[1].chan20MHz); 2791 r3 = le32toh(uc->temp[2].chan20MHz); 2792 r4 = le32toh(sc->rawtemp); 2793 2794 if (r1 == r3) /* prevents division by 0 (should not happen) */ 2795 return 0; 2796 2797 /* sign-extend 23-bit R4 value to 32-bit */ 2798 r4 = (r4 << 8) >> 8; 2799 /* compute temperature */ 2800 temp = (259 * (r4 - r2)) / (r3 - r1); 2801 temp = (temp * 97) / 100 + 8; 2802 2803 DPRINTF(("temperature %dK/%dC\n", temp, IWN_KTOC(temp))); 2804 return IWN_KTOC(temp); 2805 } 2806 2807 /* 2808 * Initialize sensitivity calibration state machine. 2809 */ 2810 static int 2811 iwn_init_sensitivity(struct iwn_softc *sc) 2812 { 2813 struct iwn_calib_state *calib = &sc->calib; 2814 struct iwn_phy_calib_cmd cmd; 2815 int error; 2816 2817 /* reset calibration state */ 2818 memset(calib, 0, sizeof (*calib)); 2819 calib->state = IWN_CALIB_STATE_INIT; 2820 calib->cck_state = IWN_CCK_STATE_HIFA; 2821 /* initial values taken from the reference driver */ 2822 calib->corr_ofdm_x1 = 105; 2823 calib->corr_ofdm_mrc_x1 = 220; 2824 calib->corr_ofdm_x4 = 90; 2825 calib->corr_ofdm_mrc_x4 = 170; 2826 calib->corr_cck_x4 = 125; 2827 calib->corr_cck_mrc_x4 = 200; 2828 calib->energy_cck = 100; 2829 2830 /* write initial sensitivity values */ 2831 if ((error = iwn_send_sensitivity(sc)) != 0) 2832 return error; 2833 2834 memset(&cmd, 0, sizeof cmd); 2835 cmd.code = IWN_SET_DIFF_GAIN; 2836 /* differential gains initially set to 0 for all 3 antennas */ 2837 DPRINTF(("setting differential gains\n")); 2838 return iwn_cmd(sc, IWN_PHY_CALIB, &cmd, sizeof cmd, 1); 2839 } 2840 2841 /* 2842 * Collect noise and RSSI statistics for the first 20 beacons received 2843 * after association and use them to determine connected antennas and 2844 * set differential gains. 2845 */ 2846 static void 2847 iwn_compute_differential_gain(struct iwn_softc *sc, 2848 const struct iwn_rx_general_stats *stats) 2849 { 2850 struct iwn_calib_state *calib = &sc->calib; 2851 struct iwn_phy_calib_cmd cmd; 2852 int i, val; 2853 2854 /* accumulate RSSI and noise for all 3 antennas */ 2855 for (i = 0; i < 3; i++) { 2856 calib->rssi[i] += le32toh(stats->rssi[i]) & 0xff; 2857 calib->noise[i] += le32toh(stats->noise[i]) & 0xff; 2858 } 2859 2860 /* we update differential gain only once after 20 beacons */ 2861 if (++calib->nbeacons < 20) 2862 return; 2863 2864 /* determine antenna with highest average RSSI */ 2865 val = max(calib->rssi[0], calib->rssi[1]); 2866 val = max(calib->rssi[2], val); 2867 2868 /* determine which antennas are connected */ 2869 sc->antmsk = 0; 2870 for (i = 0; i < 3; i++) 2871 if (val - calib->rssi[i] <= 15 * 20) 2872 sc->antmsk |= 1 << i; 2873 /* if neither Ant A and Ant B are connected.. */ 2874 if ((sc->antmsk & (1 << 0 | 1 << 1)) == 0) 2875 sc->antmsk |= 1 << 1; /* ..mark Ant B as connected! */ 2876 2877 /* get minimal noise among connected antennas */ 2878 val = INT_MAX; /* ok, there's at least one */ 2879 for (i = 0; i < 3; i++) 2880 if (sc->antmsk & (1 << i)) 2881 val = min(calib->noise[i], val); 2882 2883 memset(&cmd, 0, sizeof cmd); 2884 cmd.code = IWN_SET_DIFF_GAIN; 2885 /* set differential gains for connected antennas */ 2886 for (i = 0; i < 3; i++) { 2887 if (sc->antmsk & (1 << i)) { 2888 cmd.gain[i] = (calib->noise[i] - val) / 30; 2889 /* limit differential gain to 3 */ 2890 cmd.gain[i] = min(cmd.gain[i], 3); 2891 cmd.gain[i] |= IWN_GAIN_SET; 2892 } 2893 } 2894 DPRINTF(("setting differential gains Ant A/B/C: %x/%x/%x (%x)\n", 2895 cmd.gain[0], cmd.gain[1], cmd.gain[2], sc->antmsk)); 2896 if (iwn_cmd(sc, IWN_PHY_CALIB, &cmd, sizeof cmd, 1) == 0) 2897 calib->state = IWN_CALIB_STATE_RUN; 2898 } 2899 2900 /* 2901 * Tune RF Rx sensitivity based on the number of false alarms detected 2902 * during the last beacon period. 2903 */ 2904 static void 2905 iwn_tune_sensitivity(struct iwn_softc *sc, const struct iwn_rx_stats *stats) 2906 { 2907 #define inc_clip(val, inc, max) \ 2908 if ((val) < (max)) { \ 2909 if ((val) < (max) - (inc)) \ 2910 (val) += (inc); \ 2911 else \ 2912 (val) = (max); \ 2913 needs_update = 1; \ 2914 } 2915 #define dec_clip(val, dec, min) \ 2916 if ((val) > (min)) { \ 2917 if ((val) > (min) + (dec)) \ 2918 (val) -= (dec); \ 2919 else \ 2920 (val) = (min); \ 2921 needs_update = 1; \ 2922 } 2923 2924 struct iwn_calib_state *calib = &sc->calib; 2925 uint32_t val, rxena, fa; 2926 uint32_t energy[3], energy_min; 2927 uint8_t noise[3], noise_ref; 2928 int i, needs_update = 0; 2929 2930 /* check that we've been enabled long enough */ 2931 if ((rxena = le32toh(stats->general.load)) == 0) 2932 return; 2933 2934 /* compute number of false alarms since last call for OFDM */ 2935 fa = le32toh(stats->ofdm.bad_plcp) - calib->bad_plcp_ofdm; 2936 fa += le32toh(stats->ofdm.fa) - calib->fa_ofdm; 2937 fa *= 200 * 1024; /* 200TU */ 2938 2939 /* save counters values for next call */ 2940 calib->bad_plcp_ofdm = le32toh(stats->ofdm.bad_plcp); 2941 calib->fa_ofdm = le32toh(stats->ofdm.fa); 2942 2943 if (fa > 50 * rxena) { 2944 /* high false alarm count, decrease sensitivity */ 2945 DPRINTFN(2, ("OFDM high false alarm count: %u\n", fa)); 2946 inc_clip(calib->corr_ofdm_x1, 1, 140); 2947 inc_clip(calib->corr_ofdm_mrc_x1, 1, 270); 2948 inc_clip(calib->corr_ofdm_x4, 1, 120); 2949 inc_clip(calib->corr_ofdm_mrc_x4, 1, 210); 2950 2951 } else if (fa < 5 * rxena) { 2952 /* low false alarm count, increase sensitivity */ 2953 DPRINTFN(2, ("OFDM low false alarm count: %u\n", fa)); 2954 dec_clip(calib->corr_ofdm_x1, 1, 105); 2955 dec_clip(calib->corr_ofdm_mrc_x1, 1, 220); 2956 dec_clip(calib->corr_ofdm_x4, 1, 85); 2957 dec_clip(calib->corr_ofdm_mrc_x4, 1, 170); 2958 } 2959 2960 /* compute maximum noise among 3 antennas */ 2961 for (i = 0; i < 3; i++) 2962 noise[i] = (le32toh(stats->general.noise[i]) >> 8) & 0xff; 2963 val = max(noise[0], noise[1]); 2964 val = max(noise[2], val); 2965 /* insert it into our samples table */ 2966 calib->noise_samples[calib->cur_noise_sample] = val; 2967 calib->cur_noise_sample = (calib->cur_noise_sample + 1) % 20; 2968 2969 /* compute maximum noise among last 20 samples */ 2970 noise_ref = calib->noise_samples[0]; 2971 for (i = 1; i < 20; i++) 2972 noise_ref = max(noise_ref, calib->noise_samples[i]); 2973 2974 /* compute maximum energy among 3 antennas */ 2975 for (i = 0; i < 3; i++) 2976 energy[i] = le32toh(stats->general.energy[i]); 2977 val = min(energy[0], energy[1]); 2978 val = min(energy[2], val); 2979 /* insert it into our samples table */ 2980 calib->energy_samples[calib->cur_energy_sample] = val; 2981 calib->cur_energy_sample = (calib->cur_energy_sample + 1) % 10; 2982 2983 /* compute minimum energy among last 10 samples */ 2984 energy_min = calib->energy_samples[0]; 2985 for (i = 1; i < 10; i++) 2986 energy_min = max(energy_min, calib->energy_samples[i]); 2987 energy_min += 6; 2988 2989 /* compute number of false alarms since last call for CCK */ 2990 fa = le32toh(stats->cck.bad_plcp) - calib->bad_plcp_cck; 2991 fa += le32toh(stats->cck.fa) - calib->fa_cck; 2992 fa *= 200 * 1024; /* 200TU */ 2993 2994 /* save counters values for next call */ 2995 calib->bad_plcp_cck = le32toh(stats->cck.bad_plcp); 2996 calib->fa_cck = le32toh(stats->cck.fa); 2997 2998 if (fa > 50 * rxena) { 2999 /* high false alarm count, decrease sensitivity */ 3000 DPRINTFN(2, ("CCK high false alarm count: %u\n", fa)); 3001 calib->cck_state = IWN_CCK_STATE_HIFA; 3002 calib->low_fa = 0; 3003 3004 if (calib->corr_cck_x4 > 160) { 3005 calib->noise_ref = noise_ref; 3006 if (calib->energy_cck > 2) 3007 dec_clip(calib->energy_cck, 2, energy_min); 3008 } 3009 if (calib->corr_cck_x4 < 160) { 3010 calib->corr_cck_x4 = 161; 3011 needs_update = 1; 3012 } else 3013 inc_clip(calib->corr_cck_x4, 3, 200); 3014 3015 inc_clip(calib->corr_cck_mrc_x4, 3, 400); 3016 3017 } else if (fa < 5 * rxena) { 3018 /* low false alarm count, increase sensitivity */ 3019 DPRINTFN(2, ("CCK low false alarm count: %u\n", fa)); 3020 calib->cck_state = IWN_CCK_STATE_LOFA; 3021 calib->low_fa++; 3022 3023 if (calib->cck_state != 0 && 3024 ((calib->noise_ref - noise_ref) > 2 || 3025 calib->low_fa > 100)) { 3026 inc_clip(calib->energy_cck, 2, 97); 3027 dec_clip(calib->corr_cck_x4, 3, 125); 3028 dec_clip(calib->corr_cck_mrc_x4, 3, 200); 3029 } 3030 } else { 3031 /* not worth to increase or decrease sensitivity */ 3032 DPRINTFN(2, ("CCK normal false alarm count: %u\n", fa)); 3033 calib->low_fa = 0; 3034 calib->noise_ref = noise_ref; 3035 3036 if (calib->cck_state == IWN_CCK_STATE_HIFA) { 3037 /* previous interval had many false alarms */ 3038 dec_clip(calib->energy_cck, 8, energy_min); 3039 } 3040 calib->cck_state = IWN_CCK_STATE_INIT; 3041 } 3042 3043 if (needs_update) 3044 (void)iwn_send_sensitivity(sc); 3045 #undef dec_clip 3046 #undef inc_clip 3047 } 3048 3049 static int 3050 iwn_send_sensitivity(struct iwn_softc *sc) 3051 { 3052 struct iwn_calib_state *calib = &sc->calib; 3053 struct iwn_sensitivity_cmd cmd; 3054 3055 memset(&cmd, 0, sizeof cmd); 3056 cmd.which = IWN_SENSITIVITY_WORKTBL; 3057 /* OFDM modulation */ 3058 cmd.corr_ofdm_x1 = le16toh(calib->corr_ofdm_x1); 3059 cmd.corr_ofdm_mrc_x1 = le16toh(calib->corr_ofdm_mrc_x1); 3060 cmd.corr_ofdm_x4 = le16toh(calib->corr_ofdm_x4); 3061 cmd.corr_ofdm_mrc_x4 = le16toh(calib->corr_ofdm_mrc_x4); 3062 cmd.energy_ofdm = le16toh(100); 3063 cmd.energy_ofdm_th = le16toh(62); 3064 /* CCK modulation */ 3065 cmd.corr_cck_x4 = le16toh(calib->corr_cck_x4); 3066 cmd.corr_cck_mrc_x4 = le16toh(calib->corr_cck_mrc_x4); 3067 cmd.energy_cck = le16toh(calib->energy_cck); 3068 /* Barker modulation: use default values */ 3069 cmd.corr_barker = le16toh(190); 3070 cmd.corr_barker_mrc = le16toh(390); 3071 3072 DPRINTFN(2, ("setting sensitivity\n")); 3073 return iwn_cmd(sc, IWN_SENSITIVITY, &cmd, sizeof cmd, 1); 3074 } 3075 3076 static int 3077 iwn_auth(struct iwn_softc *sc) 3078 { 3079 struct ieee80211com *ic = &sc->sc_ic; 3080 struct ieee80211_node *ni = ic->ic_bss; 3081 struct iwn_node_info node; 3082 int error; 3083 3084 /* update adapter's configuration */ 3085 IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid); 3086 sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 3087 sc->config.flags = htole32(IWN_CONFIG_TSF); 3088 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) { 3089 sc->config.flags |= htole32(IWN_CONFIG_AUTO | 3090 IWN_CONFIG_24GHZ); 3091 } 3092 switch (ic->ic_curmode) { 3093 case IEEE80211_MODE_11A: 3094 sc->config.cck_mask = 0; 3095 sc->config.ofdm_mask = 0x15; 3096 break; 3097 case IEEE80211_MODE_11B: 3098 sc->config.cck_mask = 0x03; 3099 sc->config.ofdm_mask = 0; 3100 break; 3101 default: /* assume 802.11b/g */ 3102 sc->config.cck_mask = 0xf; 3103 sc->config.ofdm_mask = 0x15; 3104 } 3105 DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan, 3106 sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask)); 3107 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->config, 3108 sizeof (struct iwn_config), 1); 3109 if (error != 0) { 3110 aprint_error_dev(sc->sc_dev, "could not configure\n"); 3111 return error; 3112 } 3113 3114 /* configuration has changed, set Tx power accordingly */ 3115 if ((error = iwn_set_txpower(sc, ni->ni_chan, 1)) != 0) { 3116 aprint_error_dev(sc->sc_dev, "could not set Tx power\n"); 3117 return error; 3118 } 3119 3120 /* 3121 * Reconfiguring clears the adapter's nodes table so we must 3122 * add the broadcast node again. 3123 */ 3124 memset(&node, 0, sizeof node); 3125 IEEE80211_ADDR_COPY(node.macaddr, etherbroadcastaddr); 3126 node.id = IWN_ID_BROADCAST; 3127 DPRINTF(("adding broadcast node\n")); 3128 error = iwn_cmd(sc, IWN_CMD_ADD_NODE, &node, sizeof node, 1); 3129 if (error != 0) { 3130 aprint_error_dev(sc->sc_dev, "could not add broadcast node\n"); 3131 return error; 3132 } 3133 DPRINTF(("setting MRR for node %d\n", node.id)); 3134 if ((error = iwn_setup_node_mrr(sc, node.id, 1)) != 0) { 3135 aprint_error_dev(sc->sc_dev, "could not setup MRR for broadcast node\n"); 3136 return error; 3137 } 3138 3139 return 0; 3140 } 3141 3142 /* 3143 * Configure the adapter for associated state. 3144 */ 3145 static int 3146 iwn_run(struct iwn_softc *sc) 3147 { 3148 struct ieee80211com *ic = &sc->sc_ic; 3149 struct ieee80211_node *ni = ic->ic_bss; 3150 struct iwn_node_info node; 3151 int error; 3152 3153 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 3154 /* link LED blinks while monitoring */ 3155 iwn_set_led(sc, IWN_LED_LINK, 5, 5); 3156 return 0; 3157 } 3158 3159 iwn_enable_tsf(sc, ni); 3160 3161 /* update adapter's configuration */ 3162 sc->config.associd = htole16(ni->ni_associd & ~0xc000); 3163 /* short preamble/slot time are negotiated when associating */ 3164 sc->config.flags &= ~htole32(IWN_CONFIG_SHPREAMBLE | 3165 IWN_CONFIG_SHSLOT); 3166 if (ic->ic_flags & IEEE80211_F_SHSLOT) 3167 sc->config.flags |= htole32(IWN_CONFIG_SHSLOT); 3168 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 3169 sc->config.flags |= htole32(IWN_CONFIG_SHPREAMBLE); 3170 sc->config.filter |= htole32(IWN_FILTER_BSS); 3171 3172 DPRINTF(("config chan %d flags %x\n", sc->config.chan, 3173 sc->config.flags)); 3174 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->config, 3175 sizeof (struct iwn_config), 1); 3176 if (error != 0) { 3177 aprint_error_dev(sc->sc_dev, "could not update configuration\n"); 3178 return error; 3179 } 3180 3181 /* configuration has changed, set Tx power accordingly */ 3182 if ((error = iwn_set_txpower(sc, ni->ni_chan, 1)) != 0) { 3183 aprint_error_dev(sc->sc_dev, "could not set Tx power\n"); 3184 return error; 3185 } 3186 3187 /* add BSS node */ 3188 memset(&node, 0, sizeof node); 3189 IEEE80211_ADDR_COPY(node.macaddr, ni->ni_macaddr); 3190 node.id = IWN_ID_BSS; 3191 node.htflags = htole32(3 << IWN_AMDPU_SIZE_FACTOR_SHIFT | 3192 5 << IWN_AMDPU_DENSITY_SHIFT); 3193 DPRINTF(("adding BSS node\n")); 3194 error = iwn_cmd(sc, IWN_CMD_ADD_NODE, &node, sizeof node, 1); 3195 if (error != 0) { 3196 aprint_error_dev(sc->sc_dev, "could not add BSS node\n"); 3197 return error; 3198 } 3199 DPRINTF(("setting MRR for node %d\n", node.id)); 3200 if ((error = iwn_setup_node_mrr(sc, node.id, 1)) != 0) { 3201 aprint_error_dev(sc->sc_dev, "could not setup MRR for node %d\n", node.id); 3202 return error; 3203 } 3204 3205 if (ic->ic_opmode == IEEE80211_M_STA) { 3206 /* fake a join to init the tx rate */ 3207 iwn_newassoc(ni, 1); 3208 } 3209 3210 if ((error = iwn_init_sensitivity(sc)) != 0) { 3211 aprint_error_dev(sc->sc_dev, "could not set sensitivity\n"); 3212 return error; 3213 } 3214 3215 /* start periodic calibration timer */ 3216 sc->calib.state = IWN_CALIB_STATE_ASSOC; 3217 sc->calib_cnt = 0; 3218 callout_schedule(&sc->calib_to, hz / 2); 3219 3220 /* link LED always on while associated */ 3221 iwn_set_led(sc, IWN_LED_LINK, 0, 1); 3222 3223 return 0; 3224 } 3225 3226 /* 3227 * Send a scan request to the firmware. Since this command is huge, we map it 3228 * into a mbuf instead of using the pre-allocated set of commands. 3229 */ 3230 static int 3231 iwn_scan(struct iwn_softc *sc, uint16_t flags) 3232 { 3233 struct ieee80211com *ic = &sc->sc_ic; 3234 struct iwn_tx_ring *ring = &sc->txq[4]; 3235 struct iwn_tx_desc *desc; 3236 struct iwn_tx_data *data; 3237 struct iwn_tx_cmd *cmd; 3238 struct iwn_cmd_data *tx; 3239 struct iwn_scan_hdr *hdr; 3240 struct iwn_scan_essid *essid; 3241 struct iwn_scan_chan *chan; 3242 struct ieee80211_frame *wh; 3243 struct ieee80211_rateset *rs; 3244 struct ieee80211_channel *c; 3245 enum ieee80211_phymode mode; 3246 uint8_t *frm; 3247 int pktlen, error, nrates; 3248 3249 desc = &ring->desc[ring->cur]; 3250 data = &ring->data[ring->cur]; 3251 3252 MGETHDR(data->m, M_DONTWAIT, MT_DATA); 3253 if (data->m == NULL) { 3254 aprint_error_dev(sc->sc_dev, "could not allocate mbuf for scan command\n"); 3255 return ENOMEM; 3256 } 3257 MCLGET(data->m, M_DONTWAIT); 3258 if (!(data->m->m_flags & M_EXT)) { 3259 m_freem(data->m); 3260 data->m = NULL; 3261 aprint_error_dev(sc->sc_dev, "could not allocate mbuf for scan command\n"); 3262 return ENOMEM; 3263 } 3264 3265 cmd = mtod(data->m, struct iwn_tx_cmd *); 3266 cmd->code = IWN_CMD_SCAN; 3267 cmd->flags = 0; 3268 cmd->qid = ring->qid; 3269 cmd->idx = ring->cur; 3270 3271 hdr = (struct iwn_scan_hdr *)cmd->data; 3272 memset(hdr, 0, sizeof (struct iwn_scan_hdr)); 3273 /* 3274 * Move to the next channel if no packets are received within 5 msecs 3275 * after sending the probe request (this helps to reduce the duration 3276 * of active scans). 3277 */ 3278 hdr->quiet = htole16(5); /* timeout in milliseconds */ 3279 hdr->plcp_threshold = htole16(1); /* min # of packets */ 3280 3281 /* select Ant B and Ant C for scanning */ 3282 hdr->rxchain = htole16(0x3e1 | 7 << IWN_RXCHAIN_ANTMSK_SHIFT); 3283 3284 tx = (struct iwn_cmd_data *)(hdr + 1); 3285 memset(tx, 0, sizeof (struct iwn_cmd_data)); 3286 tx->flags = htole32(IWN_TX_AUTO_SEQ | 0x200); // XXX 3287 tx->id = IWN_ID_BROADCAST; 3288 tx->lifetime = htole32(IWN_LIFETIME_INFINITE); 3289 tx->rflags = IWN_RFLAG_ANT_B; 3290 3291 if (flags & IEEE80211_CHAN_A) { 3292 hdr->crc_threshold = htole16(1); 3293 /* send probe requests at 6Mbps */ 3294 tx->rate = iwn_ridx_to_plcp[IWN_OFDM6]; 3295 } else { 3296 hdr->flags = htole32(IWN_CONFIG_24GHZ | IWN_CONFIG_AUTO); 3297 /* send probe requests at 1Mbps */ 3298 tx->rate = iwn_ridx_to_plcp[IWN_CCK1]; 3299 tx->rflags |= IWN_RFLAG_CCK; 3300 } 3301 3302 essid = (struct iwn_scan_essid *)(tx + 1); 3303 memset(essid, 0, 4 * sizeof (struct iwn_scan_essid)); 3304 essid[0].id = IEEE80211_ELEMID_SSID; 3305 essid[0].len = ic->ic_des_esslen; 3306 memcpy(essid[0].data, ic->ic_des_essid, ic->ic_des_esslen); 3307 3308 /* 3309 * Build a probe request frame. Most of the following code is a 3310 * copy & paste of what is done in net80211. 3311 */ 3312 wh = (struct ieee80211_frame *)&essid[4]; 3313 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 3314 IEEE80211_FC0_SUBTYPE_PROBE_REQ; 3315 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 3316 IEEE80211_ADDR_COPY(wh->i_addr1, etherbroadcastaddr); 3317 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 3318 IEEE80211_ADDR_COPY(wh->i_addr3, etherbroadcastaddr); 3319 *(u_int16_t *)&wh->i_dur[0] = 0; /* filled by h/w */ 3320 *(u_int16_t *)&wh->i_seq[0] = 0; /* filled by h/w */ 3321 3322 frm = (uint8_t *)(wh + 1); 3323 3324 /* add empty SSID IE (firmware generates it for directed scans) */ 3325 *frm++ = IEEE80211_ELEMID_SSID; 3326 *frm++ = 0; 3327 3328 mode = ieee80211_chan2mode(ic, ic->ic_ibss_chan); 3329 rs = &ic->ic_sup_rates[mode]; 3330 3331 /* add supported rates IE */ 3332 3333 *frm++ = IEEE80211_ELEMID_RATES; 3334 nrates = rs->rs_nrates; 3335 if (nrates > IEEE80211_RATE_SIZE) 3336 nrates = IEEE80211_RATE_SIZE; 3337 *frm++ = nrates; 3338 memcpy(frm, rs->rs_rates, nrates); 3339 frm += nrates; 3340 3341 /* add supported xrates IE */ 3342 3343 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 3344 nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 3345 *frm++ = IEEE80211_ELEMID_XRATES; 3346 *frm++ = nrates; 3347 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 3348 frm += nrates; 3349 } 3350 3351 /* setup length of probe request */ 3352 tx->len = htole16(frm - (uint8_t *)wh); 3353 3354 chan = (struct iwn_scan_chan *)frm; 3355 for (c = &ic->ic_channels[1]; 3356 c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) { 3357 if ((c->ic_flags & flags) != flags) 3358 continue; 3359 3360 chan->chan = ieee80211_chan2ieee(ic, c); 3361 chan->flags = 0; 3362 if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) { 3363 chan->flags |= IWN_CHAN_ACTIVE; 3364 if (ic->ic_des_esslen != 0) 3365 chan->flags |= IWN_CHAN_DIRECT; 3366 } 3367 chan->dsp_gain = 0x6e; 3368 if (IEEE80211_IS_CHAN_5GHZ(c)) { 3369 chan->rf_gain = 0x3b; 3370 chan->active = htole16(10); 3371 chan->passive = htole16(110); 3372 } else { 3373 chan->rf_gain = 0x28; 3374 chan->active = htole16(20); 3375 chan->passive = htole16(120); 3376 } 3377 hdr->nchan++; 3378 chan++; 3379 3380 frm += sizeof (struct iwn_scan_chan); 3381 } 3382 3383 hdr->len = htole16(frm - (uint8_t *)hdr); 3384 pktlen = frm - (uint8_t *)cmd; 3385 3386 error = bus_dmamap_load(sc->sc_dmat, data->map, cmd, pktlen, NULL, 3387 BUS_DMA_NOWAIT); 3388 if (error) { 3389 aprint_error_dev(sc->sc_dev, "could not map scan command\n"); 3390 m_freem(data->m); 3391 data->m = NULL; 3392 return error; 3393 } 3394 3395 IWN_SET_DESC_NSEGS(desc, 1); 3396 IWN_SET_DESC_SEG(desc, 0, data->map->dm_segs[0].ds_addr, 3397 data->map->dm_segs[0].ds_len); 3398 sc->shared->len[ring->qid][ring->cur] = htole16(8); 3399 if (ring->cur < IWN_TX_WINDOW) { 3400 sc->shared->len[ring->qid][ring->cur + IWN_TX_RING_COUNT] = 3401 htole16(8); 3402 } 3403 3404 /* kick cmd ring */ 3405 ring->cur = (ring->cur + 1) % IWN_TX_RING_COUNT; 3406 IWN_WRITE(sc, IWN_TX_WIDX, ring->qid << 8 | ring->cur); 3407 3408 return 0; /* will be notified async. of failure/success */ 3409 } 3410 3411 static int 3412 iwn_config(struct iwn_softc *sc) 3413 { 3414 struct ieee80211com *ic = &sc->sc_ic; 3415 struct ifnet *ifp = ic->ic_ifp; 3416 struct iwn_power power; 3417 struct iwn_bluetooth bluetooth; 3418 struct iwn_node_info node; 3419 int error; 3420 3421 /* set power mode */ 3422 memset(&power, 0, sizeof power); 3423 power.flags = htole16(IWN_POWER_CAM | 0x8); 3424 DPRINTF(("setting power mode\n")); 3425 error = iwn_cmd(sc, IWN_CMD_SET_POWER_MODE, &power, sizeof power, 0); 3426 if (error != 0) { 3427 aprint_error_dev(sc->sc_dev, "could not set power mode\n"); 3428 return error; 3429 } 3430 3431 /* configure bluetooth coexistence */ 3432 memset(&bluetooth, 0, sizeof bluetooth); 3433 bluetooth.flags = 3; 3434 bluetooth.lead = 0xaa; 3435 bluetooth.kill = 1; 3436 DPRINTF(("configuring bluetooth coexistence\n")); 3437 error = iwn_cmd(sc, IWN_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth, 3438 0); 3439 if (error != 0) { 3440 aprint_error_dev(sc->sc_dev, "could not configure bluetooth coexistence\n"); 3441 return error; 3442 } 3443 3444 /* configure adapter */ 3445 memset(&sc->config, 0, sizeof (struct iwn_config)); 3446 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl)); 3447 IEEE80211_ADDR_COPY(sc->config.myaddr, ic->ic_myaddr); 3448 IEEE80211_ADDR_COPY(sc->config.wlap, ic->ic_myaddr); 3449 /* set default channel */ 3450 sc->config.chan = ieee80211_chan2ieee(ic, ic->ic_ibss_chan); 3451 sc->config.flags = htole32(IWN_CONFIG_TSF); 3452 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_ibss_chan)) { 3453 sc->config.flags |= htole32(IWN_CONFIG_AUTO | 3454 IWN_CONFIG_24GHZ); 3455 } 3456 sc->config.filter = 0; 3457 switch (ic->ic_opmode) { 3458 case IEEE80211_M_STA: 3459 sc->config.mode = IWN_MODE_STA; 3460 sc->config.filter |= htole32(IWN_FILTER_MULTICAST); 3461 break; 3462 case IEEE80211_M_IBSS: 3463 case IEEE80211_M_AHDEMO: 3464 sc->config.mode = IWN_MODE_IBSS; 3465 break; 3466 case IEEE80211_M_HOSTAP: 3467 sc->config.mode = IWN_MODE_HOSTAP; 3468 break; 3469 case IEEE80211_M_MONITOR: 3470 sc->config.mode = IWN_MODE_MONITOR; 3471 sc->config.filter |= htole32(IWN_FILTER_MULTICAST | 3472 IWN_FILTER_CTL | IWN_FILTER_PROMISC); 3473 break; 3474 } 3475 sc->config.cck_mask = 0x0f; /* not yet negotiated */ 3476 sc->config.ofdm_mask = 0xff; /* not yet negotiated */ 3477 sc->config.ht_single_mask = 0xff; 3478 sc->config.ht_dual_mask = 0xff; 3479 sc->config.rxchain = htole16(0x2800 | 7 << IWN_RXCHAIN_ANTMSK_SHIFT); 3480 DPRINTF(("setting configuration\n")); 3481 error = iwn_cmd(sc, IWN_CMD_CONFIGURE, &sc->config, 3482 sizeof (struct iwn_config), 0); 3483 if (error != 0) { 3484 aprint_error_dev(sc->sc_dev, "configure command failed\n"); 3485 return error; 3486 } 3487 3488 /* configuration has changed, set Tx power accordingly */ 3489 if ((error = iwn_set_txpower(sc, ic->ic_ibss_chan, 0)) != 0) { 3490 aprint_error_dev(sc->sc_dev, "could not set Tx power\n"); 3491 return error; 3492 } 3493 3494 /* add broadcast node */ 3495 memset(&node, 0, sizeof node); 3496 IEEE80211_ADDR_COPY(node.macaddr, etherbroadcastaddr); 3497 node.id = IWN_ID_BROADCAST; 3498 DPRINTF(("adding broadcast node\n")); 3499 error = iwn_cmd(sc, IWN_CMD_ADD_NODE, &node, sizeof node, 0); 3500 if (error != 0) { 3501 aprint_error_dev(sc->sc_dev, "could not add broadcast node\n"); 3502 return error; 3503 } 3504 DPRINTF(("setting MRR for node %d\n", node.id)); 3505 if ((error = iwn_setup_node_mrr(sc, node.id, 0)) != 0) { 3506 aprint_error_dev(sc->sc_dev, "could not setup MRR for node %d\n", node.id); 3507 return error; 3508 } 3509 3510 if ((error = iwn_set_critical_temp(sc)) != 0) { 3511 aprint_error_dev(sc->sc_dev, "could not set critical temperature\n"); 3512 return error; 3513 } 3514 3515 return 0; 3516 } 3517 3518 /* 3519 * Do post-alive initialization of the NIC (after firmware upload). 3520 */ 3521 static void 3522 iwn_post_alive(struct iwn_softc *sc) 3523 { 3524 uint32_t base; 3525 uint16_t offset; 3526 int qid; 3527 3528 iwn_mem_lock(sc); 3529 3530 /* clear SRAM */ 3531 base = iwn_mem_read(sc, IWN_SRAM_BASE); 3532 for (offset = 0x380; offset < 0x520; offset += 4) { 3533 IWN_WRITE(sc, IWN_MEM_WADDR, base + offset); 3534 IWN_WRITE(sc, IWN_MEM_WDATA, 0); 3535 } 3536 3537 /* shared area is aligned on a 1K boundary */ 3538 iwn_mem_write(sc, IWN_SRAM_BASE, sc->shared_dma.paddr >> 10); 3539 iwn_mem_write(sc, IWN_SELECT_QCHAIN, 0); 3540 3541 for (qid = 0; qid < IWN_NTXQUEUES; qid++) { 3542 iwn_mem_write(sc, IWN_QUEUE_RIDX(qid), 0); 3543 IWN_WRITE(sc, IWN_TX_WIDX, qid << 8 | 0); 3544 3545 /* set sched. window size */ 3546 IWN_WRITE(sc, IWN_MEM_WADDR, base + IWN_QUEUE_OFFSET(qid)); 3547 IWN_WRITE(sc, IWN_MEM_WDATA, 64); 3548 /* set sched. frame limit */ 3549 IWN_WRITE(sc, IWN_MEM_WADDR, base + IWN_QUEUE_OFFSET(qid) + 4); 3550 IWN_WRITE(sc, IWN_MEM_WDATA, 64 << 16); 3551 } 3552 3553 /* enable interrupts for all 16 queues */ 3554 iwn_mem_write(sc, IWN_QUEUE_INTR_MASK, 0xffff); 3555 3556 /* identify active Tx rings (0-7) */ 3557 iwn_mem_write(sc, IWN_TX_ACTIVE, 0xff); 3558 3559 /* mark Tx rings (4 EDCA + cmd + 2 HCCA) as active */ 3560 for (qid = 0; qid < 7; qid++) { 3561 iwn_mem_write(sc, IWN_TXQ_STATUS(qid), 3562 IWN_TXQ_STATUS_ACTIVE | qid << 1); 3563 } 3564 3565 iwn_mem_unlock(sc); 3566 } 3567 3568 static void 3569 iwn_stop_master(struct iwn_softc *sc) 3570 { 3571 uint32_t tmp; 3572 int ntries; 3573 3574 tmp = IWN_READ(sc, IWN_RESET); 3575 IWN_WRITE(sc, IWN_RESET, tmp | IWN_STOP_MASTER); 3576 3577 tmp = IWN_READ(sc, IWN_GPIO_CTL); 3578 if ((tmp & IWN_GPIO_PWR_STATUS) == IWN_GPIO_PWR_SLEEP) 3579 return; /* already asleep */ 3580 3581 for (ntries = 0; ntries < 100; ntries++) { 3582 if (IWN_READ(sc, IWN_RESET) & IWN_MASTER_DISABLED) 3583 break; 3584 DELAY(10); 3585 } 3586 if (ntries == 100) { 3587 aprint_error_dev(sc->sc_dev, "timeout waiting for master\n"); 3588 } 3589 } 3590 3591 static int 3592 iwn_reset(struct iwn_softc *sc) 3593 { 3594 uint32_t tmp; 3595 int ntries; 3596 3597 /* clear any pending interrupts */ 3598 IWN_WRITE(sc, IWN_INTR, 0xffffffff); 3599 3600 tmp = IWN_READ(sc, IWN_CHICKEN); 3601 IWN_WRITE(sc, IWN_CHICKEN, tmp | IWN_CHICKEN_DISLOS); 3602 3603 tmp = IWN_READ(sc, IWN_GPIO_CTL); 3604 IWN_WRITE(sc, IWN_GPIO_CTL, tmp | IWN_GPIO_INIT); 3605 3606 /* wait for clock stabilization */ 3607 for (ntries = 0; ntries < 1000; ntries++) { 3608 if (IWN_READ(sc, IWN_GPIO_CTL) & IWN_GPIO_CLOCK) 3609 break; 3610 DELAY(10); 3611 } 3612 if (ntries == 1000) { 3613 aprint_error_dev(sc->sc_dev, "timeout waiting for clock stabilization\n"); 3614 return ETIMEDOUT; 3615 } 3616 return 0; 3617 } 3618 3619 static void 3620 iwn_hw_config(struct iwn_softc *sc) 3621 { 3622 uint32_t tmp, hw; 3623 3624 /* enable interrupts mitigation */ 3625 IWN_WRITE(sc, IWN_INTR_MIT, 512 / 32); 3626 3627 /* voodoo from the reference driver */ 3628 tmp = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_CLASS_REG); 3629 tmp = PCI_REVISION(tmp); 3630 if ((tmp & 0x80) && (tmp & 0x7f) < 8) { 3631 /* enable "no snoop" field */ 3632 tmp = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0xe8); 3633 tmp &= ~IWN_DIS_NOSNOOP; 3634 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0xe8, tmp); 3635 } 3636 3637 /* disable L1 entry to work around a hardware bug */ 3638 tmp = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0xf0); 3639 tmp &= ~IWN_ENA_L1; 3640 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0xf0, tmp); 3641 3642 hw = IWN_READ(sc, IWN_HWCONFIG); 3643 IWN_WRITE(sc, IWN_HWCONFIG, hw | 0x310); 3644 3645 iwn_mem_lock(sc); 3646 tmp = iwn_mem_read(sc, IWN_MEM_POWER); 3647 iwn_mem_write(sc, IWN_MEM_POWER, tmp | IWN_POWER_RESET); 3648 DELAY(5); 3649 tmp = iwn_mem_read(sc, IWN_MEM_POWER); 3650 iwn_mem_write(sc, IWN_MEM_POWER, tmp & ~IWN_POWER_RESET); 3651 iwn_mem_unlock(sc); 3652 } 3653 3654 static int 3655 iwn_init(struct ifnet *ifp) 3656 { 3657 struct iwn_softc *sc = ifp->if_softc; 3658 struct ieee80211com *ic = &sc->sc_ic; 3659 uint32_t tmp; 3660 int error, qid; 3661 3662 iwn_stop(ifp, 1); 3663 if ((error = iwn_reset(sc)) != 0) { 3664 aprint_error_dev(sc->sc_dev, "could not reset adapter\n"); 3665 goto fail1; 3666 } 3667 3668 iwn_mem_lock(sc); 3669 iwn_mem_read(sc, IWN_CLOCK_CTL); 3670 iwn_mem_write(sc, IWN_CLOCK_CTL, 0xa00); 3671 iwn_mem_read(sc, IWN_CLOCK_CTL); 3672 iwn_mem_unlock(sc); 3673 3674 DELAY(20); 3675 3676 iwn_mem_lock(sc); 3677 tmp = iwn_mem_read(sc, IWN_MEM_PCIDEV); 3678 iwn_mem_write(sc, IWN_MEM_PCIDEV, tmp | 0x800); 3679 iwn_mem_unlock(sc); 3680 3681 iwn_mem_lock(sc); 3682 tmp = iwn_mem_read(sc, IWN_MEM_POWER); 3683 iwn_mem_write(sc, IWN_MEM_POWER, tmp & ~0x03000000); 3684 iwn_mem_unlock(sc); 3685 3686 iwn_hw_config(sc); 3687 3688 /* init Rx ring */ 3689 iwn_mem_lock(sc); 3690 IWN_WRITE(sc, IWN_RX_CONFIG, 0); 3691 IWN_WRITE(sc, IWN_RX_WIDX, 0); 3692 /* Rx ring is aligned on a 256-byte boundary */ 3693 IWN_WRITE(sc, IWN_RX_BASE, sc->rxq.desc_dma.paddr >> 8); 3694 /* shared area is aligned on a 16-byte boundary */ 3695 IWN_WRITE(sc, IWN_RW_WIDX_PTR, (sc->shared_dma.paddr + 3696 offsetof(struct iwn_shared, closed_count)) >> 4); 3697 IWN_WRITE(sc, IWN_RX_CONFIG, 0x80601000); 3698 iwn_mem_unlock(sc); 3699 3700 IWN_WRITE(sc, IWN_RX_WIDX, (IWN_RX_RING_COUNT - 1) & ~7); 3701 3702 iwn_mem_lock(sc); 3703 iwn_mem_write(sc, IWN_TX_ACTIVE, 0); 3704 3705 /* set physical address of "keep warm" page */ 3706 IWN_WRITE(sc, IWN_KW_BASE, sc->kw_dma.paddr >> 4); 3707 3708 /* init Tx rings */ 3709 for (qid = 0; qid < IWN_NTXQUEUES; qid++) { 3710 struct iwn_tx_ring *txq = &sc->txq[qid]; 3711 IWN_WRITE(sc, IWN_TX_BASE(qid), txq->desc_dma.paddr >> 8); 3712 IWN_WRITE(sc, IWN_TX_CONFIG(qid), 0x80000008); 3713 } 3714 iwn_mem_unlock(sc); 3715 3716 /* clear "radio off" and "disable command" bits (reversed logic) */ 3717 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_RADIO_OFF); 3718 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_DISABLE_CMD); 3719 3720 /* clear any pending interrupts */ 3721 IWN_WRITE(sc, IWN_INTR, 0xffffffff); 3722 /* enable interrupts */ 3723 IWN_WRITE(sc, IWN_MASK, IWN_INTR_MASK); 3724 3725 /* not sure why/if this is necessary... */ 3726 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_RADIO_OFF); 3727 IWN_WRITE(sc, IWN_UCODE_CLR, IWN_RADIO_OFF); 3728 3729 /* check that the radio is not disabled by RF switch */ 3730 if (!(IWN_READ(sc, IWN_GPIO_CTL) & IWN_GPIO_RF_ENABLED)) { 3731 aprint_error_dev(sc->sc_dev, "radio is disabled by hardware switch\n"); 3732 error = EBUSY; /* XXX ;-) */ 3733 goto fail1; 3734 } 3735 3736 if ((error = iwn_load_firmware(sc)) != 0) { 3737 aprint_error_dev(sc->sc_dev, "could not load firmware\n"); 3738 goto fail1; 3739 } 3740 3741 /* firmware has notified us that it is alive.. */ 3742 iwn_post_alive(sc); /* ..do post alive initialization */ 3743 3744 sc->rawtemp = sc->ucode_info.temp[3].chan20MHz; 3745 sc->temp = iwn_get_temperature(sc); 3746 DPRINTF(("temperature=%d\n", sc->temp)); 3747 3748 if ((error = iwn_config(sc)) != 0) { 3749 aprint_error_dev(sc->sc_dev, "could not configure device\n"); 3750 goto fail1; 3751 } 3752 3753 DPRINTF(("iwn_config end\n")); 3754 3755 ifp->if_flags &= ~IFF_OACTIVE; 3756 ifp->if_flags |= IFF_RUNNING; 3757 3758 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 3759 if (ic->ic_opmode != IEEE80211_ROAMING_MANUAL) 3760 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 3761 } 3762 else 3763 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 3764 3765 DPRINTF(("iwn_init ok\n")); 3766 return 0; 3767 3768 fail1: 3769 DPRINTF(("iwn_init error\n")); 3770 iwn_stop(ifp, 1); 3771 return error; 3772 } 3773 3774 static void 3775 iwn_stop(struct ifnet *ifp, int disable) 3776 { 3777 struct iwn_softc *sc = ifp->if_softc; 3778 struct ieee80211com *ic = &sc->sc_ic; 3779 uint32_t tmp; 3780 int i; 3781 3782 ifp->if_timer = sc->sc_tx_timer = 0; 3783 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 3784 3785 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 3786 3787 IWN_WRITE(sc, IWN_RESET, IWN_NEVO_RESET); 3788 3789 /* disable interrupts */ 3790 IWN_WRITE(sc, IWN_MASK, 0); 3791 IWN_WRITE(sc, IWN_INTR, 0xffffffff); 3792 IWN_WRITE(sc, IWN_INTR_STATUS, 0xffffffff); 3793 3794 /* make sure we no longer hold the memory lock */ 3795 iwn_mem_unlock(sc); 3796 3797 /* reset all Tx rings */ 3798 for (i = 0; i < IWN_NTXQUEUES; i++) 3799 iwn_reset_tx_ring(sc, &sc->txq[i]); 3800 3801 /* reset Rx ring */ 3802 iwn_reset_rx_ring(sc, &sc->rxq); 3803 3804 iwn_mem_lock(sc); 3805 iwn_mem_write(sc, IWN_MEM_CLOCK2, 0x200); 3806 iwn_mem_unlock(sc); 3807 3808 DELAY(5); 3809 3810 iwn_stop_master(sc); 3811 tmp = IWN_READ(sc, IWN_RESET); 3812 IWN_WRITE(sc, IWN_RESET, tmp | IWN_SW_RESET); 3813 } 3814 3815 static bool 3816 iwn_resume(device_t dv PMF_FN_ARGS) 3817 { 3818 struct iwn_softc *sc = device_private(dv); 3819 3820 (void)iwn_reset(sc); 3821 3822 return true; 3823 } 3824