1 /* $NetBSD: if_iwi.c,v 1.67 2007/10/22 15:31:46 joerg Exp $ */ 2 3 /*- 4 * Copyright (c) 2004, 2005 5 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: if_iwi.c,v 1.67 2007/10/22 15:31:46 joerg Exp $"); 32 33 /*- 34 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver 35 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm 36 */ 37 38 #include "bpfilter.h" 39 40 #include <sys/param.h> 41 #include <sys/sockio.h> 42 #include <sys/sysctl.h> 43 #include <sys/mbuf.h> 44 #include <sys/kernel.h> 45 #include <sys/socket.h> 46 #include <sys/systm.h> 47 #include <sys/malloc.h> 48 #include <sys/conf.h> 49 #include <sys/kauth.h> 50 51 #include <sys/bus.h> 52 #include <machine/endian.h> 53 #include <sys/intr.h> 54 55 #include <dev/firmload.h> 56 57 #include <dev/pci/pcireg.h> 58 #include <dev/pci/pcivar.h> 59 #include <dev/pci/pcidevs.h> 60 61 #if NBPFILTER > 0 62 #include <net/bpf.h> 63 #endif 64 #include <net/if.h> 65 #include <net/if_arp.h> 66 #include <net/if_dl.h> 67 #include <net/if_ether.h> 68 #include <net/if_media.h> 69 #include <net/if_types.h> 70 71 #include <net80211/ieee80211_var.h> 72 #include <net80211/ieee80211_radiotap.h> 73 74 #include <netinet/in.h> 75 #include <netinet/in_systm.h> 76 #include <netinet/in_var.h> 77 #include <netinet/ip.h> 78 79 #include <crypto/arc4/arc4.h> 80 81 #include <dev/pci/if_iwireg.h> 82 #include <dev/pci/if_iwivar.h> 83 84 #ifdef IWI_DEBUG 85 #define DPRINTF(x) if (iwi_debug > 0) printf x 86 #define DPRINTFN(n, x) if (iwi_debug >= (n)) printf x 87 int iwi_debug = 4; 88 #else 89 #define DPRINTF(x) 90 #define DPRINTFN(n, x) 91 #endif 92 93 static int iwi_match(device_t, struct cfdata *, void *); 94 static void iwi_attach(device_t, device_t, void *); 95 static int iwi_detach(device_t, int); 96 97 static void iwi_shutdown(void *); 98 static int iwi_suspend(struct iwi_softc *); 99 static int iwi_resume(struct iwi_softc *); 100 static void iwi_powerhook(int, void *); 101 102 static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *, 103 int); 104 static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *); 105 static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *); 106 static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *, 107 int, bus_addr_t, bus_size_t); 108 static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *); 109 static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *); 110 static struct mbuf * 111 iwi_alloc_rx_buf(struct iwi_softc *sc); 112 static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *, 113 int); 114 static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *); 115 static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *); 116 117 static struct ieee80211_node *iwi_node_alloc(struct ieee80211_node_table *); 118 static void iwi_node_free(struct ieee80211_node *); 119 120 static int iwi_cvtrate(int); 121 static int iwi_media_change(struct ifnet *); 122 static void iwi_media_status(struct ifnet *, struct ifmediareq *); 123 static int iwi_wme_update(struct ieee80211com *); 124 static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t); 125 static int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int); 126 static void iwi_fix_channel(struct ieee80211com *, struct mbuf *); 127 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int, 128 struct iwi_frame *); 129 static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *); 130 static void iwi_cmd_intr(struct iwi_softc *); 131 static void iwi_rx_intr(struct iwi_softc *); 132 static void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *); 133 static int iwi_intr(void *); 134 static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t, int); 135 static void iwi_write_ibssnode(struct iwi_softc *, const struct iwi_node *); 136 static int iwi_tx_start(struct ifnet *, struct mbuf *, struct ieee80211_node *, 137 int); 138 static void iwi_start(struct ifnet *); 139 static void iwi_watchdog(struct ifnet *); 140 141 static int iwi_alloc_unr(struct iwi_softc *); 142 static void iwi_free_unr(struct iwi_softc *, int); 143 144 static int iwi_get_table0(struct iwi_softc *, uint32_t *); 145 146 static int iwi_ioctl(struct ifnet *, u_long, void *); 147 static void iwi_stop_master(struct iwi_softc *); 148 static int iwi_reset(struct iwi_softc *); 149 static int iwi_load_ucode(struct iwi_softc *, void *, int); 150 static int iwi_load_firmware(struct iwi_softc *, void *, int); 151 static int iwi_cache_firmware(struct iwi_softc *); 152 static void iwi_free_firmware(struct iwi_softc *); 153 static int iwi_config(struct iwi_softc *); 154 static int iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *); 155 static int iwi_scan(struct iwi_softc *); 156 static int iwi_auth_and_assoc(struct iwi_softc *); 157 static int iwi_init(struct ifnet *); 158 static void iwi_stop(struct ifnet *, int); 159 static int iwi_getrfkill(struct iwi_softc *); 160 static void iwi_led_set(struct iwi_softc *, uint32_t, int); 161 static void iwi_sysctlattach(struct iwi_softc *); 162 163 /* 164 * Supported rates for 802.11a/b/g modes (in 500Kbps unit). 165 */ 166 static const struct ieee80211_rateset iwi_rateset_11a = 167 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } }; 168 169 static const struct ieee80211_rateset iwi_rateset_11b = 170 { 4, { 2, 4, 11, 22 } }; 171 172 static const struct ieee80211_rateset iwi_rateset_11g = 173 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 174 175 static inline uint8_t 176 MEM_READ_1(struct iwi_softc *sc, uint32_t addr) 177 { 178 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr); 179 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA); 180 } 181 182 static inline uint32_t 183 MEM_READ_4(struct iwi_softc *sc, uint32_t addr) 184 { 185 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr); 186 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA); 187 } 188 189 CFATTACH_DECL_NEW(iwi, sizeof (struct iwi_softc), iwi_match, iwi_attach, 190 iwi_detach, NULL); 191 192 static int 193 iwi_match(device_t parent, struct cfdata *match, void *aux) 194 { 195 struct pci_attach_args *pa = aux; 196 197 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL) 198 return 0; 199 200 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2200BG || 201 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2225BG || 202 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 || 203 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2) 204 return 1; 205 206 return 0; 207 } 208 209 /* Base Address Register */ 210 #define IWI_PCI_BAR0 0x10 211 212 static void 213 iwi_attach(device_t parent, device_t self, void *aux) 214 { 215 struct iwi_softc *sc = device_private(self); 216 struct ieee80211com *ic = &sc->sc_ic; 217 struct ifnet *ifp = &sc->sc_if; 218 struct pci_attach_args *pa = aux; 219 const char *intrstr; 220 char devinfo[256]; 221 bus_space_tag_t memt; 222 bus_space_handle_t memh; 223 pci_intr_handle_t ih; 224 pcireg_t data; 225 uint16_t val; 226 int error, revision, i; 227 228 sc->sc_dev = self; 229 sc->sc_pct = pa->pa_pc; 230 sc->sc_pcitag = pa->pa_tag; 231 232 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo); 233 revision = PCI_REVISION(pa->pa_class); 234 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision); 235 236 /* clear device specific PCI configuration register 0x41 */ 237 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40); 238 data &= ~0x0000ff00; 239 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data); 240 241 /* clear unit numbers allocated to IBSS */ 242 sc->sc_unr = 0; 243 244 /* power up chip */ 245 if ((error = pci_activate(pa->pa_pc, pa->pa_tag, sc, 246 NULL)) && error != EOPNOTSUPP) { 247 aprint_error_dev(self, "cannot activate %d\n", error); 248 return; 249 } 250 251 /* enable bus-mastering */ 252 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG); 253 data |= PCI_COMMAND_MASTER_ENABLE; 254 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data); 255 256 /* map the register window */ 257 error = pci_mapreg_map(pa, IWI_PCI_BAR0, PCI_MAPREG_TYPE_MEM | 258 PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, NULL, &sc->sc_sz); 259 if (error != 0) { 260 aprint_error_dev(self, "could not map memory space\n"); 261 return; 262 } 263 264 sc->sc_st = memt; 265 sc->sc_sh = memh; 266 sc->sc_dmat = pa->pa_dmat; 267 268 /* disable interrupts */ 269 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0); 270 271 if (pci_intr_map(pa, &ih) != 0) { 272 aprint_error_dev(self, "could not map interrupt\n"); 273 return; 274 } 275 276 intrstr = pci_intr_string(sc->sc_pct, ih); 277 sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwi_intr, sc); 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 (iwi_reset(sc) != 0) { 288 aprint_error_dev(self, "could not reset adapter\n"); 289 return; 290 } 291 292 /* 293 * Allocate rings. 294 */ 295 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) { 296 aprint_error_dev(self, "could not allocate command ring\n"); 297 goto fail; 298 } 299 300 error = iwi_alloc_tx_ring(sc, &sc->txq[0], IWI_TX_RING_COUNT, 301 IWI_CSR_TX1_RIDX, IWI_CSR_TX1_WIDX); 302 if (error != 0) { 303 aprint_error_dev(self, "could not allocate Tx ring 1\n"); 304 goto fail; 305 } 306 307 error = iwi_alloc_tx_ring(sc, &sc->txq[1], IWI_TX_RING_COUNT, 308 IWI_CSR_TX2_RIDX, IWI_CSR_TX2_WIDX); 309 if (error != 0) { 310 aprint_error_dev(self, "could not allocate Tx ring 2\n"); 311 goto fail; 312 } 313 314 error = iwi_alloc_tx_ring(sc, &sc->txq[2], IWI_TX_RING_COUNT, 315 IWI_CSR_TX3_RIDX, IWI_CSR_TX3_WIDX); 316 if (error != 0) { 317 aprint_error_dev(self, "could not allocate Tx ring 3\n"); 318 goto fail; 319 } 320 321 error = iwi_alloc_tx_ring(sc, &sc->txq[3], IWI_TX_RING_COUNT, 322 IWI_CSR_TX4_RIDX, IWI_CSR_TX4_WIDX); 323 if (error != 0) { 324 aprint_error_dev(self, "could not allocate Tx ring 4\n"); 325 goto fail; 326 } 327 328 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) { 329 aprint_error_dev(self, "could not allocate Rx ring\n"); 330 goto fail; 331 } 332 333 ic->ic_ifp = ifp; 334 ic->ic_wme.wme_update = iwi_wme_update; 335 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 336 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 337 ic->ic_state = IEEE80211_S_INIT; 338 339 sc->sc_fwname = "iwi-bss.fw"; 340 341 /* set device capabilities */ 342 ic->ic_caps = 343 IEEE80211_C_IBSS | /* IBSS mode supported */ 344 IEEE80211_C_MONITOR | /* monitor mode supported */ 345 IEEE80211_C_TXPMGT | /* tx power management */ 346 IEEE80211_C_SHPREAMBLE | /* short preamble supported */ 347 IEEE80211_C_SHSLOT | /* short slot time supported */ 348 IEEE80211_C_WPA | /* 802.11i */ 349 IEEE80211_C_WME; /* 802.11e */ 350 351 /* read MAC address from EEPROM */ 352 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0); 353 ic->ic_myaddr[0] = val & 0xff; 354 ic->ic_myaddr[1] = val >> 8; 355 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1); 356 ic->ic_myaddr[2] = val & 0xff; 357 ic->ic_myaddr[3] = val >> 8; 358 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2); 359 ic->ic_myaddr[4] = val & 0xff; 360 ic->ic_myaddr[5] = val >> 8; 361 362 aprint_verbose_dev(self, "802.11 address %s\n", 363 ether_sprintf(ic->ic_myaddr)); 364 365 /* read the NIC type from EEPROM */ 366 val = iwi_read_prom_word(sc, IWI_EEPROM_NIC_TYPE); 367 sc->nictype = val & 0xff; 368 369 DPRINTF(("%s: NIC type %d\n", device_xname(self), sc->nictype)); 370 371 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 || 372 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2) { 373 /* set supported .11a rates (2915ABG only) */ 374 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a; 375 376 /* set supported .11a channels */ 377 for (i = 36; i <= 64; i += 4) { 378 ic->ic_channels[i].ic_freq = 379 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 380 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 381 } 382 for (i = 149; i <= 165; i += 4) { 383 ic->ic_channels[i].ic_freq = 384 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 385 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 386 } 387 } 388 389 /* set supported .11b and .11g rates */ 390 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b; 391 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g; 392 393 /* set supported .11b and .11g channels (1 through 14) */ 394 for (i = 1; i <= 14; i++) { 395 ic->ic_channels[i].ic_freq = 396 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 397 ic->ic_channels[i].ic_flags = 398 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 399 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 400 } 401 402 ifp->if_softc = sc; 403 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 404 ifp->if_init = iwi_init; 405 ifp->if_stop = iwi_stop; 406 ifp->if_ioctl = iwi_ioctl; 407 ifp->if_start = iwi_start; 408 ifp->if_watchdog = iwi_watchdog; 409 IFQ_SET_READY(&ifp->if_snd); 410 memcpy(ifp->if_xname, device_xname(self), IFNAMSIZ); 411 412 if_attach(ifp); 413 ieee80211_ifattach(ic); 414 /* override default methods */ 415 ic->ic_node_alloc = iwi_node_alloc; 416 sc->sc_node_free = ic->ic_node_free; 417 ic->ic_node_free = iwi_node_free; 418 /* override state transition machine */ 419 sc->sc_newstate = ic->ic_newstate; 420 ic->ic_newstate = iwi_newstate; 421 ieee80211_media_init(ic, iwi_media_change, iwi_media_status); 422 423 #if NBPFILTER > 0 424 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 425 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf); 426 427 sc->sc_rxtap_len = sizeof sc->sc_rxtapu; 428 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 429 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT); 430 431 sc->sc_txtap_len = sizeof sc->sc_txtapu; 432 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 433 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT); 434 #endif 435 436 iwi_sysctlattach(sc); 437 438 /* 439 * Make sure the interface is shutdown during reboot. 440 */ 441 sc->sc_sdhook = shutdownhook_establish(iwi_shutdown, sc); 442 if (sc->sc_sdhook == NULL) 443 aprint_error_dev(self, 444 "WARNING: unable to establish shutdown hook\n"); 445 sc->sc_powerhook = powerhook_establish(device_xname(self), 446 iwi_powerhook, sc); 447 if (sc->sc_powerhook == NULL) 448 aprint_error_dev(self, 449 "WARNING: unable to establish power hook\n"); 450 451 ieee80211_announce(ic); 452 453 return; 454 455 fail: iwi_detach(self, 0); 456 } 457 458 static int 459 iwi_detach(device_t self, int flags) 460 { 461 struct iwi_softc *sc = device_private(self); 462 struct ifnet *ifp = &sc->sc_if; 463 464 if (ifp != NULL) 465 iwi_stop(ifp, 1); 466 467 iwi_free_firmware(sc); 468 469 ieee80211_ifdetach(&sc->sc_ic); 470 if (ifp != NULL) 471 if_detach(ifp); 472 473 iwi_free_cmd_ring(sc, &sc->cmdq); 474 iwi_free_tx_ring(sc, &sc->txq[0]); 475 iwi_free_tx_ring(sc, &sc->txq[1]); 476 iwi_free_tx_ring(sc, &sc->txq[2]); 477 iwi_free_tx_ring(sc, &sc->txq[3]); 478 iwi_free_rx_ring(sc, &sc->rxq); 479 480 if (sc->sc_ih != NULL) { 481 pci_intr_disestablish(sc->sc_pct, sc->sc_ih); 482 sc->sc_ih = NULL; 483 } 484 485 bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz); 486 487 powerhook_disestablish(sc->sc_powerhook); 488 shutdownhook_disestablish(sc->sc_sdhook); 489 490 return 0; 491 } 492 493 static int 494 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, 495 int count) 496 { 497 int error, nsegs; 498 499 ring->count = count; 500 ring->queued = 0; 501 ring->cur = ring->next = 0; 502 503 /* 504 * Allocate and map command ring 505 */ 506 error = bus_dmamap_create(sc->sc_dmat, 507 IWI_CMD_DESC_SIZE * count, 1, 508 IWI_CMD_DESC_SIZE * count, 0, 509 BUS_DMA_NOWAIT, &ring->desc_map); 510 if (error != 0) { 511 aprint_error_dev(sc->sc_dev, 512 "could not create command ring DMA map\n"); 513 goto fail; 514 } 515 516 error = bus_dmamem_alloc(sc->sc_dmat, 517 IWI_CMD_DESC_SIZE * count, PAGE_SIZE, 0, 518 &sc->cmdq.desc_seg, 1, &nsegs, BUS_DMA_NOWAIT); 519 if (error != 0) { 520 aprint_error_dev(sc->sc_dev, 521 "could not allocate command ring DMA memory\n"); 522 goto fail; 523 } 524 525 error = bus_dmamem_map(sc->sc_dmat, &sc->cmdq.desc_seg, nsegs, 526 IWI_CMD_DESC_SIZE * count, 527 (void **)&sc->cmdq.desc, BUS_DMA_NOWAIT); 528 if (error != 0) { 529 aprint_error_dev(sc->sc_dev, 530 "could not map command ring DMA memory\n"); 531 goto fail; 532 } 533 534 error = bus_dmamap_load(sc->sc_dmat, sc->cmdq.desc_map, sc->cmdq.desc, 535 IWI_CMD_DESC_SIZE * count, NULL, 536 BUS_DMA_NOWAIT); 537 if (error != 0) { 538 aprint_error_dev(sc->sc_dev, 539 "could not load command ring DMA map\n"); 540 goto fail; 541 } 542 543 memset(sc->cmdq.desc, 0, 544 IWI_CMD_DESC_SIZE * count); 545 546 return 0; 547 548 fail: iwi_free_cmd_ring(sc, ring); 549 return error; 550 } 551 552 static void 553 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring) 554 { 555 int i; 556 557 for (i = ring->next; i != ring->cur;) { 558 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map, 559 i * IWI_CMD_DESC_SIZE, IWI_CMD_DESC_SIZE, 560 BUS_DMASYNC_POSTWRITE); 561 562 wakeup(&ring->desc[i]); 563 i = (i + 1) % ring->count; 564 } 565 566 ring->queued = 0; 567 ring->cur = ring->next = 0; 568 } 569 570 static void 571 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring) 572 { 573 if (ring->desc_map != NULL) { 574 if (ring->desc != NULL) { 575 bus_dmamap_unload(sc->sc_dmat, ring->desc_map); 576 bus_dmamem_unmap(sc->sc_dmat, (void *)ring->desc, 577 IWI_CMD_DESC_SIZE * ring->count); 578 bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1); 579 } 580 bus_dmamap_destroy(sc->sc_dmat, ring->desc_map); 581 } 582 } 583 584 static int 585 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, 586 int count, bus_size_t csr_ridx, bus_size_t csr_widx) 587 { 588 int i, error, nsegs; 589 590 ring->count = count; 591 ring->queued = 0; 592 ring->cur = ring->next = 0; 593 ring->csr_ridx = csr_ridx; 594 ring->csr_widx = csr_widx; 595 596 /* 597 * Allocate and map Tx ring 598 */ 599 error = bus_dmamap_create(sc->sc_dmat, 600 IWI_TX_DESC_SIZE * count, 1, 601 IWI_TX_DESC_SIZE * count, 0, BUS_DMA_NOWAIT, 602 &ring->desc_map); 603 if (error != 0) { 604 aprint_error_dev(sc->sc_dev, 605 "could not create tx ring DMA map\n"); 606 goto fail; 607 } 608 609 error = bus_dmamem_alloc(sc->sc_dmat, 610 IWI_TX_DESC_SIZE * count, PAGE_SIZE, 0, 611 &ring->desc_seg, 1, &nsegs, BUS_DMA_NOWAIT); 612 if (error != 0) { 613 aprint_error_dev(sc->sc_dev, 614 "could not allocate tx ring DMA memory\n"); 615 goto fail; 616 } 617 618 error = bus_dmamem_map(sc->sc_dmat, &ring->desc_seg, nsegs, 619 IWI_TX_DESC_SIZE * count, 620 (void **)&ring->desc, BUS_DMA_NOWAIT); 621 if (error != 0) { 622 aprint_error_dev(sc->sc_dev, 623 "could not map tx ring DMA memory\n"); 624 goto fail; 625 } 626 627 error = bus_dmamap_load(sc->sc_dmat, ring->desc_map, ring->desc, 628 IWI_TX_DESC_SIZE * count, NULL, 629 BUS_DMA_NOWAIT); 630 if (error != 0) { 631 aprint_error_dev(sc->sc_dev, 632 "could not load tx ring DMA map\n"); 633 goto fail; 634 } 635 636 memset(ring->desc, 0, IWI_TX_DESC_SIZE * count); 637 638 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF, 639 M_NOWAIT | M_ZERO); 640 if (ring->data == NULL) { 641 aprint_error_dev(sc->sc_dev, "could not allocate soft data\n"); 642 error = ENOMEM; 643 goto fail; 644 } 645 646 /* 647 * Allocate Tx buffers DMA maps 648 */ 649 for (i = 0; i < count; i++) { 650 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, IWI_MAX_NSEG, 651 MCLBYTES, 0, BUS_DMA_NOWAIT, &ring->data[i].map); 652 if (error != 0) { 653 aprint_error_dev(sc->sc_dev, 654 "could not create tx buf DMA map"); 655 goto fail; 656 } 657 } 658 return 0; 659 660 fail: iwi_free_tx_ring(sc, ring); 661 return error; 662 } 663 664 static void 665 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring) 666 { 667 struct iwi_tx_data *data; 668 int i; 669 670 for (i = 0; i < ring->count; i++) { 671 data = &ring->data[i]; 672 673 if (data->m != NULL) { 674 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 675 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE); 676 bus_dmamap_unload(sc->sc_dmat, data->map); 677 m_freem(data->m); 678 data->m = NULL; 679 } 680 681 if (data->ni != NULL) { 682 ieee80211_free_node(data->ni); 683 data->ni = NULL; 684 } 685 } 686 687 ring->queued = 0; 688 ring->cur = ring->next = 0; 689 } 690 691 static void 692 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring) 693 { 694 int i; 695 696 if (ring->desc_map != NULL) { 697 if (ring->desc != NULL) { 698 bus_dmamap_unload(sc->sc_dmat, ring->desc_map); 699 bus_dmamem_unmap(sc->sc_dmat, (void *)ring->desc, 700 IWI_TX_DESC_SIZE * ring->count); 701 bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1); 702 } 703 bus_dmamap_destroy(sc->sc_dmat, ring->desc_map); 704 } 705 706 for (i = 0; i < ring->count; i++) { 707 if (ring->data[i].m != NULL) { 708 bus_dmamap_unload(sc->sc_dmat, ring->data[i].map); 709 m_freem(ring->data[i].m); 710 } 711 bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map); 712 } 713 } 714 715 static int 716 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count) 717 { 718 int i, error; 719 720 ring->count = count; 721 ring->cur = 0; 722 723 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF, 724 M_NOWAIT | M_ZERO); 725 if (ring->data == NULL) { 726 aprint_error_dev(sc->sc_dev, "could not allocate soft data\n"); 727 error = ENOMEM; 728 goto fail; 729 } 730 731 /* 732 * Allocate and map Rx buffers 733 */ 734 for (i = 0; i < count; i++) { 735 736 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 737 0, BUS_DMA_WAITOK | BUS_DMA_ALLOCNOW, &ring->data[i].map); 738 if (error != 0) { 739 aprint_error_dev(sc->sc_dev, 740 "could not create rx buf DMA map"); 741 goto fail; 742 } 743 744 if ((ring->data[i].m = iwi_alloc_rx_buf(sc)) == NULL) { 745 error = ENOMEM; 746 goto fail; 747 } 748 749 error = bus_dmamap_load_mbuf(sc->sc_dmat, ring->data[i].map, 750 ring->data[i].m, BUS_DMA_READ | BUS_DMA_NOWAIT); 751 if (error != 0) { 752 aprint_error_dev(sc->sc_dev, 753 "could not load rx buffer DMA map\n"); 754 goto fail; 755 } 756 757 bus_dmamap_sync(sc->sc_dmat, ring->data[i].map, 0, 758 ring->data[i].map->dm_mapsize, BUS_DMASYNC_PREREAD); 759 } 760 761 return 0; 762 763 fail: iwi_free_rx_ring(sc, ring); 764 return error; 765 } 766 767 static void 768 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring) 769 { 770 ring->cur = 0; 771 } 772 773 static void 774 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring) 775 { 776 int i; 777 778 for (i = 0; i < ring->count; i++) { 779 if (ring->data[i].m != NULL) { 780 bus_dmamap_unload(sc->sc_dmat, ring->data[i].map); 781 m_freem(ring->data[i].m); 782 } 783 bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map); 784 } 785 } 786 787 static void 788 iwi_shutdown(void *arg) 789 { 790 struct iwi_softc *sc = (struct iwi_softc *)arg; 791 struct ifnet *ifp = sc->sc_ic.ic_ifp; 792 793 iwi_stop(ifp, 1); 794 } 795 796 static int 797 iwi_suspend(struct iwi_softc *sc) 798 { 799 struct ifnet *ifp = sc->sc_ic.ic_ifp; 800 801 iwi_stop(ifp, 1); 802 803 return 0; 804 } 805 806 static int 807 iwi_resume(struct iwi_softc *sc) 808 { 809 struct ifnet *ifp = sc->sc_ic.ic_ifp; 810 pcireg_t data; 811 812 /* clear device specific PCI configuration register 0x41 */ 813 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40); 814 data &= ~0x0000ff00; 815 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data); 816 817 if (ifp->if_flags & IFF_UP) { 818 iwi_init(ifp); 819 if (ifp->if_flags & IFF_RUNNING) 820 iwi_start(ifp); 821 } 822 823 return 0; 824 } 825 826 static void 827 iwi_powerhook(int why, void *arg) 828 { 829 struct iwi_softc *sc = arg; 830 pci_chipset_tag_t pc = sc->sc_pct; 831 pcitag_t tag = sc->sc_pcitag; 832 int s; 833 834 s = splnet(); 835 switch (why) { 836 case PWR_SUSPEND: 837 case PWR_STANDBY: 838 pci_conf_capture(pc, tag, &sc->sc_pciconf); 839 break; 840 case PWR_RESUME: 841 pci_conf_restore(pc, tag, &sc->sc_pciconf); 842 break; 843 case PWR_SOFTSUSPEND: 844 case PWR_SOFTSTANDBY: 845 iwi_suspend(sc); 846 break; 847 case PWR_SOFTRESUME: 848 iwi_resume(sc); 849 break; 850 } 851 splx(s); 852 } 853 854 static struct ieee80211_node * 855 iwi_node_alloc(struct ieee80211_node_table *nt) 856 { 857 struct iwi_node *in; 858 859 in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO); 860 if (in == NULL) 861 return NULL; 862 863 in->in_station = -1; 864 865 return &in->in_node; 866 } 867 868 static int 869 iwi_alloc_unr(struct iwi_softc *sc) 870 { 871 int i; 872 873 for (i = 0; i < IWI_MAX_IBSSNODE - 1; i++) 874 if ((sc->sc_unr & (1 << i)) == 0) { 875 sc->sc_unr |= 1 << i; 876 return i; 877 } 878 879 return -1; 880 } 881 882 static void 883 iwi_free_unr(struct iwi_softc *sc, int r) 884 { 885 886 sc->sc_unr &= 1 << r; 887 } 888 889 static void 890 iwi_node_free(struct ieee80211_node *ni) 891 { 892 struct ieee80211com *ic = ni->ni_ic; 893 struct iwi_softc *sc = ic->ic_ifp->if_softc; 894 struct iwi_node *in = (struct iwi_node *)ni; 895 896 if (in->in_station != -1) 897 iwi_free_unr(sc, in->in_station); 898 899 sc->sc_node_free(ni); 900 } 901 902 static int 903 iwi_media_change(struct ifnet *ifp) 904 { 905 int error; 906 907 error = ieee80211_media_change(ifp); 908 if (error != ENETRESET) 909 return error; 910 911 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) 912 iwi_init(ifp); 913 914 return 0; 915 } 916 917 /* 918 * Convert h/w rate code to IEEE rate code. 919 */ 920 static int 921 iwi_cvtrate(int iwirate) 922 { 923 switch (iwirate) { 924 case IWI_RATE_DS1: return 2; 925 case IWI_RATE_DS2: return 4; 926 case IWI_RATE_DS5: return 11; 927 case IWI_RATE_DS11: return 22; 928 case IWI_RATE_OFDM6: return 12; 929 case IWI_RATE_OFDM9: return 18; 930 case IWI_RATE_OFDM12: return 24; 931 case IWI_RATE_OFDM18: return 36; 932 case IWI_RATE_OFDM24: return 48; 933 case IWI_RATE_OFDM36: return 72; 934 case IWI_RATE_OFDM48: return 96; 935 case IWI_RATE_OFDM54: return 108; 936 } 937 return 0; 938 } 939 940 /* 941 * The firmware automatically adapts the transmit speed. We report its current 942 * value here. 943 */ 944 static void 945 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 946 { 947 struct iwi_softc *sc = ifp->if_softc; 948 struct ieee80211com *ic = &sc->sc_ic; 949 int rate; 950 951 imr->ifm_status = IFM_AVALID; 952 imr->ifm_active = IFM_IEEE80211; 953 if (ic->ic_state == IEEE80211_S_RUN) 954 imr->ifm_status |= IFM_ACTIVE; 955 956 /* read current transmission rate from adapter */ 957 rate = iwi_cvtrate(CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE)); 958 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode); 959 960 switch (ic->ic_opmode) { 961 case IEEE80211_M_STA: 962 break; 963 964 case IEEE80211_M_IBSS: 965 imr->ifm_active |= IFM_IEEE80211_ADHOC; 966 break; 967 968 case IEEE80211_M_MONITOR: 969 imr->ifm_active |= IFM_IEEE80211_MONITOR; 970 break; 971 972 case IEEE80211_M_AHDEMO: 973 case IEEE80211_M_HOSTAP: 974 /* should not get there */ 975 break; 976 } 977 } 978 979 static int 980 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 981 { 982 struct iwi_softc *sc = ic->ic_ifp->if_softc; 983 984 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__, 985 ieee80211_state_name[ic->ic_state], 986 ieee80211_state_name[nstate], sc->flags)); 987 988 switch (nstate) { 989 case IEEE80211_S_SCAN: 990 if (sc->flags & IWI_FLAG_SCANNING) 991 break; 992 993 ieee80211_node_table_reset(&ic->ic_scan); 994 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN; 995 sc->flags |= IWI_FLAG_SCANNING; 996 /* blink the led while scanning */ 997 iwi_led_set(sc, IWI_LED_ASSOCIATED, 1); 998 iwi_scan(sc); 999 break; 1000 1001 case IEEE80211_S_AUTH: 1002 iwi_auth_and_assoc(sc); 1003 break; 1004 1005 case IEEE80211_S_RUN: 1006 if (ic->ic_opmode == IEEE80211_M_IBSS) 1007 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 1008 else if (ic->ic_opmode == IEEE80211_M_MONITOR) 1009 iwi_set_chan(sc, ic->ic_ibss_chan); 1010 1011 return (*sc->sc_newstate)(ic, nstate, 1012 IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 1013 1014 case IEEE80211_S_ASSOC: 1015 iwi_led_set(sc, IWI_LED_ASSOCIATED, 0); 1016 break; 1017 1018 case IEEE80211_S_INIT: 1019 sc->flags &= ~IWI_FLAG_SCANNING; 1020 return (*sc->sc_newstate)(ic, nstate, arg); 1021 } 1022 1023 ic->ic_state = nstate; 1024 return 0; 1025 } 1026 1027 /* 1028 * WME parameters coming from IEEE 802.11e specification. These values are 1029 * already declared in ieee80211_proto.c, but they are static so they can't 1030 * be reused here. 1031 */ 1032 static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = { 1033 { 0, 3, 5, 7, 0, 0, }, /* WME_AC_BE */ 1034 { 0, 3, 5, 10, 0, 0, }, /* WME_AC_BK */ 1035 { 0, 2, 4, 5, 188, 0, }, /* WME_AC_VI */ 1036 { 0, 2, 3, 4, 102, 0, }, /* WME_AC_VO */ 1037 }; 1038 1039 static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = { 1040 { 0, 3, 4, 6, 0, 0, }, /* WME_AC_BE */ 1041 { 0, 3, 4, 10, 0, 0, }, /* WME_AC_BK */ 1042 { 0, 2, 3, 4, 94, 0, }, /* WME_AC_VI */ 1043 { 0, 2, 2, 3, 47, 0, }, /* WME_AC_VO */ 1044 }; 1045 1046 static int 1047 iwi_wme_update(struct ieee80211com *ic) 1048 { 1049 #define IWI_EXP2(v) htole16((1 << (v)) - 1) 1050 #define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v)) 1051 struct iwi_softc *sc = ic->ic_ifp->if_softc; 1052 struct iwi_wme_params wme[3]; 1053 const struct wmeParams *wmep; 1054 int ac; 1055 1056 /* 1057 * We shall not override firmware default WME values if WME is not 1058 * actually enabled. 1059 */ 1060 if (!(ic->ic_flags & IEEE80211_F_WME)) 1061 return 0; 1062 1063 for (ac = 0; ac < WME_NUM_AC; ac++) { 1064 /* set WME values for current operating mode */ 1065 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac]; 1066 wme[0].aifsn[ac] = wmep->wmep_aifsn; 1067 wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin); 1068 wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax); 1069 wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit); 1070 wme[0].acm[ac] = wmep->wmep_acm; 1071 1072 /* set WME values for CCK modulation */ 1073 wmep = &iwi_wme_cck_params[ac]; 1074 wme[1].aifsn[ac] = wmep->wmep_aifsn; 1075 wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin); 1076 wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax); 1077 wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit); 1078 wme[1].acm[ac] = wmep->wmep_acm; 1079 1080 /* set WME values for OFDM modulation */ 1081 wmep = &iwi_wme_ofdm_params[ac]; 1082 wme[2].aifsn[ac] = wmep->wmep_aifsn; 1083 wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin); 1084 wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax); 1085 wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit); 1086 wme[2].acm[ac] = wmep->wmep_acm; 1087 } 1088 1089 DPRINTF(("Setting WME parameters\n")); 1090 return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, wme, sizeof wme, 1); 1091 #undef IWI_USEC 1092 #undef IWI_EXP2 1093 } 1094 1095 /* 1096 * Read 16 bits at address 'addr' from the serial EEPROM. 1097 */ 1098 static uint16_t 1099 iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr) 1100 { 1101 uint32_t tmp; 1102 uint16_t val; 1103 int n; 1104 1105 /* Clock C once before the first command */ 1106 IWI_EEPROM_CTL(sc, 0); 1107 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1108 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 1109 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1110 1111 /* Write start bit (1) */ 1112 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D); 1113 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C); 1114 1115 /* Write READ opcode (10) */ 1116 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D); 1117 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C); 1118 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1119 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 1120 1121 /* Write address A7-A0 */ 1122 for (n = 7; n >= 0; n--) { 1123 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | 1124 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D)); 1125 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | 1126 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C); 1127 } 1128 1129 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1130 1131 /* Read data Q15-Q0 */ 1132 val = 0; 1133 for (n = 15; n >= 0; n--) { 1134 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 1135 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1136 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL); 1137 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n; 1138 } 1139 1140 IWI_EEPROM_CTL(sc, 0); 1141 1142 /* Clear Chip Select and clock C */ 1143 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1144 IWI_EEPROM_CTL(sc, 0); 1145 IWI_EEPROM_CTL(sc, IWI_EEPROM_C); 1146 1147 return val; 1148 } 1149 1150 /* 1151 * XXX: Hack to set the current channel to the value advertised in beacons or 1152 * probe responses. Only used during AP detection. 1153 */ 1154 static void 1155 iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m) 1156 { 1157 struct ieee80211_frame *wh; 1158 uint8_t subtype; 1159 uint8_t *frm, *efrm; 1160 1161 wh = mtod(m, struct ieee80211_frame *); 1162 1163 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT) 1164 return; 1165 1166 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 1167 1168 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON && 1169 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1170 return; 1171 1172 frm = (uint8_t *)(wh + 1); 1173 efrm = mtod(m, uint8_t *) + m->m_len; 1174 1175 frm += 12; /* skip tstamp, bintval and capinfo fields */ 1176 while (frm < efrm) { 1177 if (*frm == IEEE80211_ELEMID_DSPARMS) 1178 #if IEEE80211_CHAN_MAX < 255 1179 if (frm[2] <= IEEE80211_CHAN_MAX) 1180 #endif 1181 ic->ic_curchan = &ic->ic_channels[frm[2]]; 1182 1183 frm += frm[1] + 2; 1184 } 1185 } 1186 1187 static struct mbuf * 1188 iwi_alloc_rx_buf(struct iwi_softc *sc) 1189 { 1190 struct mbuf *m; 1191 1192 MGETHDR(m, M_DONTWAIT, MT_DATA); 1193 if (m == NULL) { 1194 aprint_error_dev(sc->sc_dev, "could not allocate rx mbuf\n"); 1195 return NULL; 1196 } 1197 1198 MCLGET(m, M_DONTWAIT); 1199 if (!(m->m_flags & M_EXT)) { 1200 aprint_error_dev(sc->sc_dev, 1201 "could not allocate rx mbuf cluster\n"); 1202 m_freem(m); 1203 return NULL; 1204 } 1205 1206 m->m_pkthdr.len = m->m_len = m->m_ext.ext_size; 1207 return m; 1208 } 1209 1210 static void 1211 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i, 1212 struct iwi_frame *frame) 1213 { 1214 struct ieee80211com *ic = &sc->sc_ic; 1215 struct ifnet *ifp = ic->ic_ifp; 1216 struct mbuf *m, *m_new; 1217 struct ieee80211_frame *wh; 1218 struct ieee80211_node *ni; 1219 int error; 1220 1221 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n", 1222 le16toh(frame->len), frame->chan, frame->rssi_dbm)); 1223 1224 if (le16toh(frame->len) < sizeof (struct ieee80211_frame) || 1225 le16toh(frame->len) > MCLBYTES) { 1226 DPRINTF(("%s: bad frame length\n", device_xname(sc->sc_dev))); 1227 ifp->if_ierrors++; 1228 return; 1229 } 1230 1231 /* 1232 * Try to allocate a new mbuf for this ring element and 1233 * load it before processing the current mbuf. If the ring 1234 * element cannot be reloaded, drop the received packet 1235 * and reuse the old mbuf. In the unlikely case that 1236 * the old mbuf can't be reloaded either, explicitly panic. 1237 * 1238 * XXX Reorganize buffer by moving elements from the logical 1239 * end of the ring to the front instead of dropping. 1240 */ 1241 if ((m_new = iwi_alloc_rx_buf(sc)) == NULL) { 1242 ifp->if_ierrors++; 1243 return; 1244 } 1245 1246 bus_dmamap_unload(sc->sc_dmat, data->map); 1247 1248 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m_new, 1249 BUS_DMA_READ | BUS_DMA_NOWAIT); 1250 if (error != 0) { 1251 aprint_error_dev(sc->sc_dev, 1252 "could not load rx buf DMA map\n"); 1253 m_freem(m_new); 1254 ifp->if_ierrors++; 1255 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, 1256 data->m, BUS_DMA_READ | BUS_DMA_NOWAIT); 1257 if (error) 1258 panic("%s: unable to remap rx buf", 1259 device_xname(sc->sc_dev)); 1260 return; 1261 } 1262 1263 /* 1264 * New mbuf successfully loaded, update RX ring and continue 1265 * processing. 1266 */ 1267 m = data->m; 1268 data->m = m_new; 1269 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, data->map->dm_segs[0].ds_addr); 1270 1271 /* Finalize mbuf */ 1272 m->m_pkthdr.rcvif = ifp; 1273 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) + 1274 sizeof (struct iwi_frame) + le16toh(frame->len); 1275 1276 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame)); 1277 1278 if (ic->ic_state == IEEE80211_S_SCAN) 1279 iwi_fix_channel(ic, m); 1280 1281 #if NBPFILTER > 0 1282 if (sc->sc_drvbpf != NULL) { 1283 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap; 1284 1285 tap->wr_flags = 0; 1286 tap->wr_rate = iwi_cvtrate(frame->rate); 1287 tap->wr_chan_freq = 1288 htole16(ic->ic_channels[frame->chan].ic_freq); 1289 tap->wr_chan_flags = 1290 htole16(ic->ic_channels[frame->chan].ic_flags); 1291 tap->wr_antsignal = frame->signal; 1292 tap->wr_antenna = frame->antenna; 1293 1294 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 1295 } 1296 #endif 1297 wh = mtod(m, struct ieee80211_frame *); 1298 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 1299 1300 /* Send the frame to the upper layer */ 1301 ieee80211_input(ic, m, ni, frame->rssi_dbm, 0); 1302 1303 /* node is no longer needed */ 1304 ieee80211_free_node(ni); 1305 } 1306 1307 static void 1308 iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif) 1309 { 1310 struct ieee80211com *ic = &sc->sc_ic; 1311 struct iwi_notif_scan_channel *chan; 1312 struct iwi_notif_scan_complete *scan; 1313 struct iwi_notif_authentication *auth; 1314 struct iwi_notif_association *assoc; 1315 struct iwi_notif_beacon_state *beacon; 1316 1317 switch (notif->type) { 1318 case IWI_NOTIF_TYPE_SCAN_CHANNEL: 1319 chan = (struct iwi_notif_scan_channel *)(notif + 1); 1320 1321 DPRINTFN(2, ("Scan of channel %u complete (%u)\n", 1322 ic->ic_channels[chan->nchan].ic_freq, chan->nchan)); 1323 break; 1324 1325 case IWI_NOTIF_TYPE_SCAN_COMPLETE: 1326 scan = (struct iwi_notif_scan_complete *)(notif + 1); 1327 1328 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan, 1329 scan->status)); 1330 1331 /* monitor mode uses scan to set the channel ... */ 1332 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1333 sc->flags &= ~IWI_FLAG_SCANNING; 1334 ieee80211_end_scan(ic); 1335 } else 1336 iwi_set_chan(sc, ic->ic_ibss_chan); 1337 break; 1338 1339 case IWI_NOTIF_TYPE_AUTHENTICATION: 1340 auth = (struct iwi_notif_authentication *)(notif + 1); 1341 1342 DPRINTFN(2, ("Authentication (%u)\n", auth->state)); 1343 1344 switch (auth->state) { 1345 case IWI_AUTH_SUCCESS: 1346 ieee80211_node_authorize(ic->ic_bss); 1347 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1); 1348 break; 1349 1350 case IWI_AUTH_FAIL: 1351 break; 1352 1353 default: 1354 aprint_error_dev(sc->sc_dev, 1355 "unknown authentication state %u\n", auth->state); 1356 } 1357 break; 1358 1359 case IWI_NOTIF_TYPE_ASSOCIATION: 1360 assoc = (struct iwi_notif_association *)(notif + 1); 1361 1362 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state, 1363 assoc->status)); 1364 1365 switch (assoc->state) { 1366 case IWI_AUTH_SUCCESS: 1367 /* re-association, do nothing */ 1368 break; 1369 1370 case IWI_ASSOC_SUCCESS: 1371 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1372 break; 1373 1374 case IWI_ASSOC_FAIL: 1375 ieee80211_begin_scan(ic, 1); 1376 break; 1377 1378 default: 1379 aprint_error_dev(sc->sc_dev, 1380 "unknown association state %u\n", assoc->state); 1381 } 1382 break; 1383 1384 case IWI_NOTIF_TYPE_BEACON: 1385 beacon = (struct iwi_notif_beacon_state *)(notif + 1); 1386 1387 if (beacon->state == IWI_BEACON_MISS) { 1388 DPRINTFN(5, ("%s: %u beacon(s) missed\n", 1389 device_xname(sc->sc_dev), le32toh(beacon->number))); 1390 } 1391 break; 1392 1393 case IWI_NOTIF_TYPE_FRAG_LENGTH: 1394 case IWI_NOTIF_TYPE_LINK_QUALITY: 1395 case IWI_NOTIF_TYPE_TGI_TX_KEY: 1396 case IWI_NOTIF_TYPE_CALIBRATION: 1397 case IWI_NOTIF_TYPE_NOISE: 1398 DPRINTFN(5, ("Notification (%u)\n", notif->type)); 1399 break; 1400 1401 default: 1402 DPRINTF(("%s: unknown notification type %u flags 0x%x len %d\n", 1403 device_xname(sc->sc_dev), notif->type, notif->flags, 1404 le16toh(notif->len))); 1405 } 1406 } 1407 1408 static void 1409 iwi_cmd_intr(struct iwi_softc *sc) 1410 { 1411 uint32_t hw; 1412 1413 hw = CSR_READ_4(sc, IWI_CSR_CMD_RIDX); 1414 1415 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map, 1416 sc->cmdq.next * IWI_CMD_DESC_SIZE, IWI_CMD_DESC_SIZE, 1417 BUS_DMASYNC_POSTWRITE); 1418 1419 wakeup(&sc->cmdq.desc[sc->cmdq.next]); 1420 1421 sc->cmdq.next = (sc->cmdq.next + 1) % sc->cmdq.count; 1422 1423 if (--sc->cmdq.queued > 0) { 1424 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, (sc->cmdq.next + 1) % sc->cmdq.count); 1425 } 1426 } 1427 1428 static void 1429 iwi_rx_intr(struct iwi_softc *sc) 1430 { 1431 struct iwi_rx_data *data; 1432 struct iwi_hdr *hdr; 1433 uint32_t hw; 1434 1435 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX); 1436 1437 for (; sc->rxq.cur != hw;) { 1438 data = &sc->rxq.data[sc->rxq.cur]; 1439 1440 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 1441 data->map->dm_mapsize, BUS_DMASYNC_POSTREAD); 1442 1443 hdr = mtod(data->m, struct iwi_hdr *); 1444 1445 switch (hdr->type) { 1446 case IWI_HDR_TYPE_FRAME: 1447 iwi_frame_intr(sc, data, sc->rxq.cur, 1448 (struct iwi_frame *)(hdr + 1)); 1449 break; 1450 1451 case IWI_HDR_TYPE_NOTIF: 1452 iwi_notification_intr(sc, 1453 (struct iwi_notif *)(hdr + 1)); 1454 break; 1455 1456 default: 1457 aprint_error_dev(sc->sc_dev, "unknown hdr type %u\n", 1458 hdr->type); 1459 } 1460 1461 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 1462 data->map->dm_mapsize, BUS_DMASYNC_PREREAD); 1463 1464 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur)); 1465 1466 sc->rxq.cur = (sc->rxq.cur + 1) % sc->rxq.count; 1467 } 1468 1469 /* Tell the firmware what we have processed */ 1470 hw = (hw == 0) ? sc->rxq.count - 1 : hw - 1; 1471 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw); 1472 } 1473 1474 static void 1475 iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq) 1476 { 1477 struct ifnet *ifp = &sc->sc_if; 1478 struct iwi_tx_data *data; 1479 uint32_t hw; 1480 1481 hw = CSR_READ_4(sc, txq->csr_ridx); 1482 1483 for (; txq->next != hw;) { 1484 data = &txq->data[txq->next]; 1485 1486 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 1487 data->map->dm_mapsize, BUS_DMASYNC_POSTWRITE); 1488 bus_dmamap_unload(sc->sc_dmat, data->map); 1489 m_freem(data->m); 1490 data->m = NULL; 1491 ieee80211_free_node(data->ni); 1492 data->ni = NULL; 1493 1494 DPRINTFN(15, ("tx done idx=%u\n", txq->next)); 1495 1496 ifp->if_opackets++; 1497 1498 txq->queued--; 1499 txq->next = (txq->next + 1) % txq->count; 1500 } 1501 1502 sc->sc_tx_timer = 0; 1503 ifp->if_flags &= ~IFF_OACTIVE; 1504 1505 /* Call start() since some buffer descriptors have been released */ 1506 (*ifp->if_start)(ifp); 1507 } 1508 1509 static int 1510 iwi_intr(void *arg) 1511 { 1512 struct iwi_softc *sc = arg; 1513 uint32_t r; 1514 1515 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) 1516 return 0; 1517 1518 /* Acknowledge interrupts */ 1519 CSR_WRITE_4(sc, IWI_CSR_INTR, r); 1520 1521 if (r & IWI_INTR_FATAL_ERROR) { 1522 aprint_error_dev(sc->sc_dev, "fatal error\n"); 1523 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP; 1524 iwi_stop(&sc->sc_if, 1); 1525 return (1); 1526 } 1527 1528 if (r & IWI_INTR_FW_INITED) { 1529 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR))) 1530 wakeup(sc); 1531 } 1532 1533 if (r & IWI_INTR_RADIO_OFF) { 1534 DPRINTF(("radio transmitter off\n")); 1535 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP; 1536 iwi_stop(&sc->sc_if, 1); 1537 return (1); 1538 } 1539 1540 if (r & IWI_INTR_CMD_DONE) 1541 iwi_cmd_intr(sc); 1542 1543 if (r & IWI_INTR_TX1_DONE) 1544 iwi_tx_intr(sc, &sc->txq[0]); 1545 1546 if (r & IWI_INTR_TX2_DONE) 1547 iwi_tx_intr(sc, &sc->txq[1]); 1548 1549 if (r & IWI_INTR_TX3_DONE) 1550 iwi_tx_intr(sc, &sc->txq[2]); 1551 1552 if (r & IWI_INTR_TX4_DONE) 1553 iwi_tx_intr(sc, &sc->txq[3]); 1554 1555 if (r & IWI_INTR_RX_DONE) 1556 iwi_rx_intr(sc); 1557 1558 if (r & IWI_INTR_PARITY_ERROR) 1559 aprint_error_dev(sc->sc_dev, "parity error\n"); 1560 1561 return 1; 1562 } 1563 1564 static int 1565 iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len, 1566 int async) 1567 { 1568 struct iwi_cmd_desc *desc; 1569 1570 desc = &sc->cmdq.desc[sc->cmdq.cur]; 1571 1572 desc->hdr.type = IWI_HDR_TYPE_COMMAND; 1573 desc->hdr.flags = IWI_HDR_FLAG_IRQ; 1574 desc->type = type; 1575 desc->len = len; 1576 memcpy(desc->data, data, len); 1577 1578 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map, 1579 sc->cmdq.cur * IWI_CMD_DESC_SIZE, 1580 IWI_CMD_DESC_SIZE, BUS_DMASYNC_PREWRITE); 1581 1582 DPRINTFN(2, ("sending command idx=%u type=%u len=%u async=%d\n", 1583 sc->cmdq.cur, type, len, async)); 1584 1585 sc->cmdq.cur = (sc->cmdq.cur + 1) % sc->cmdq.count; 1586 1587 if (++sc->cmdq.queued == 1) 1588 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur); 1589 1590 return async ? 0 : tsleep(desc, 0, "iwicmd", hz); 1591 } 1592 1593 static void 1594 iwi_write_ibssnode(struct iwi_softc *sc, const struct iwi_node *in) 1595 { 1596 struct iwi_ibssnode node; 1597 1598 /* write node information into NIC memory */ 1599 memset(&node, 0, sizeof node); 1600 IEEE80211_ADDR_COPY(node.bssid, in->in_node.ni_macaddr); 1601 1602 CSR_WRITE_REGION_1(sc, 1603 IWI_CSR_NODE_BASE + in->in_station * sizeof node, 1604 (uint8_t *)&node, sizeof node); 1605 } 1606 1607 static int 1608 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni, 1609 int ac) 1610 { 1611 struct iwi_softc *sc = ifp->if_softc; 1612 struct ieee80211com *ic = &sc->sc_ic; 1613 struct iwi_node *in = (struct iwi_node *)ni; 1614 struct ieee80211_frame *wh; 1615 struct ieee80211_key *k; 1616 const struct chanAccParams *cap; 1617 struct iwi_tx_ring *txq = &sc->txq[ac]; 1618 struct iwi_tx_data *data; 1619 struct iwi_tx_desc *desc; 1620 struct mbuf *mnew; 1621 int error, hdrlen, i, noack = 0; 1622 1623 wh = mtod(m0, struct ieee80211_frame *); 1624 1625 if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) { 1626 hdrlen = sizeof (struct ieee80211_qosframe); 1627 cap = &ic->ic_wme.wme_chanParams; 1628 noack = cap->cap_wmeParams[ac].wmep_noackPolicy; 1629 } else 1630 hdrlen = sizeof (struct ieee80211_frame); 1631 1632 /* 1633 * This is only used in IBSS mode where the firmware expect an index 1634 * in a h/w table instead of a destination address. 1635 */ 1636 if (ic->ic_opmode == IEEE80211_M_IBSS && in->in_station == -1) { 1637 in->in_station = iwi_alloc_unr(sc); 1638 1639 if (in->in_station == -1) { /* h/w table is full */ 1640 m_freem(m0); 1641 ieee80211_free_node(ni); 1642 ifp->if_oerrors++; 1643 return 0; 1644 } 1645 iwi_write_ibssnode(sc, in); 1646 } 1647 1648 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1649 k = ieee80211_crypto_encap(ic, ni, m0); 1650 if (k == NULL) { 1651 m_freem(m0); 1652 return ENOBUFS; 1653 } 1654 1655 /* packet header may have moved, reset our local pointer */ 1656 wh = mtod(m0, struct ieee80211_frame *); 1657 } 1658 1659 #if NBPFILTER > 0 1660 if (sc->sc_drvbpf != NULL) { 1661 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap; 1662 1663 tap->wt_flags = 0; 1664 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq); 1665 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags); 1666 1667 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1668 } 1669 #endif 1670 1671 data = &txq->data[txq->cur]; 1672 desc = &txq->desc[txq->cur]; 1673 1674 /* save and trim IEEE802.11 header */ 1675 m_copydata(m0, 0, hdrlen, (void *)&desc->wh); 1676 m_adj(m0, hdrlen); 1677 1678 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, 1679 BUS_DMA_WRITE | BUS_DMA_NOWAIT); 1680 if (error != 0 && error != EFBIG) { 1681 aprint_error_dev(sc->sc_dev, "could not map mbuf (error %d)\n", 1682 error); 1683 m_freem(m0); 1684 return error; 1685 } 1686 if (error != 0) { 1687 /* too many fragments, linearize */ 1688 1689 MGETHDR(mnew, M_DONTWAIT, MT_DATA); 1690 if (mnew == NULL) { 1691 m_freem(m0); 1692 return ENOMEM; 1693 } 1694 1695 M_COPY_PKTHDR(mnew, m0); 1696 1697 /* If the data won't fit in the header, get a cluster */ 1698 if (m0->m_pkthdr.len > MHLEN) { 1699 MCLGET(mnew, M_DONTWAIT); 1700 if (!(mnew->m_flags & M_EXT)) { 1701 m_freem(m0); 1702 m_freem(mnew); 1703 return ENOMEM; 1704 } 1705 } 1706 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, void *)); 1707 m_freem(m0); 1708 mnew->m_len = mnew->m_pkthdr.len; 1709 m0 = mnew; 1710 1711 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, 1712 BUS_DMA_WRITE | BUS_DMA_NOWAIT); 1713 if (error != 0) { 1714 aprint_error_dev(sc->sc_dev, 1715 "could not map mbuf (error %d)\n", error); 1716 m_freem(m0); 1717 return error; 1718 } 1719 } 1720 1721 data->m = m0; 1722 data->ni = ni; 1723 1724 desc->hdr.type = IWI_HDR_TYPE_DATA; 1725 desc->hdr.flags = IWI_HDR_FLAG_IRQ; 1726 desc->station = 1727 (ic->ic_opmode == IEEE80211_M_IBSS) ? in->in_station : 0; 1728 desc->cmd = IWI_DATA_CMD_TX; 1729 desc->len = htole16(m0->m_pkthdr.len); 1730 desc->flags = 0; 1731 desc->xflags = 0; 1732 1733 if (!noack && !IEEE80211_IS_MULTICAST(desc->wh.i_addr1)) 1734 desc->flags |= IWI_DATA_FLAG_NEED_ACK; 1735 1736 #if 0 1737 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 1738 desc->wh.i_fc[1] |= IEEE80211_FC1_WEP; 1739 desc->wep_txkey = ic->ic_crypto.cs_def_txkey; 1740 } else 1741 #endif 1742 desc->flags |= IWI_DATA_FLAG_NO_WEP; 1743 1744 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1745 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE; 1746 1747 if (desc->wh.i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) 1748 desc->xflags |= IWI_DATA_XFLAG_QOS; 1749 1750 if (ic->ic_curmode == IEEE80211_MODE_11B) 1751 desc->xflags |= IWI_DATA_XFLAG_CCK; 1752 1753 desc->nseg = htole32(data->map->dm_nsegs); 1754 for (i = 0; i < data->map->dm_nsegs; i++) { 1755 desc->seg_addr[i] = htole32(data->map->dm_segs[i].ds_addr); 1756 desc->seg_len[i] = htole16(data->map->dm_segs[i].ds_len); 1757 } 1758 1759 bus_dmamap_sync(sc->sc_dmat, txq->desc_map, 1760 txq->cur * IWI_TX_DESC_SIZE, 1761 IWI_TX_DESC_SIZE, BUS_DMASYNC_PREWRITE); 1762 1763 bus_dmamap_sync(sc->sc_dmat, data->map, 0, data->map->dm_mapsize, 1764 BUS_DMASYNC_PREWRITE); 1765 1766 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n", 1767 ac, txq->cur, le16toh(desc->len), le32toh(desc->nseg))); 1768 1769 /* Inform firmware about this new packet */ 1770 txq->queued++; 1771 txq->cur = (txq->cur + 1) % txq->count; 1772 CSR_WRITE_4(sc, txq->csr_widx, txq->cur); 1773 1774 return 0; 1775 } 1776 1777 static void 1778 iwi_start(struct ifnet *ifp) 1779 { 1780 struct iwi_softc *sc = ifp->if_softc; 1781 struct ieee80211com *ic = &sc->sc_ic; 1782 struct mbuf *m0; 1783 struct ether_header *eh; 1784 struct ieee80211_node *ni; 1785 int ac; 1786 1787 if (ic->ic_state != IEEE80211_S_RUN) 1788 return; 1789 1790 for (;;) { 1791 IF_DEQUEUE(&ifp->if_snd, m0); 1792 if (m0 == NULL) 1793 break; 1794 1795 if (m0->m_len < sizeof (struct ether_header) && 1796 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL) { 1797 ifp->if_oerrors++; 1798 continue; 1799 } 1800 1801 eh = mtod(m0, struct ether_header *); 1802 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 1803 if (ni == NULL) { 1804 m_freem(m0); 1805 ifp->if_oerrors++; 1806 continue; 1807 } 1808 1809 /* classify mbuf so we can find which tx ring to use */ 1810 if (ieee80211_classify(ic, m0, ni) != 0) { 1811 m_freem(m0); 1812 ieee80211_free_node(ni); 1813 ifp->if_oerrors++; 1814 continue; 1815 } 1816 1817 /* no QoS encapsulation for EAPOL frames */ 1818 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ? 1819 M_WME_GETAC(m0) : WME_AC_BE; 1820 1821 if (sc->txq[ac].queued > sc->txq[ac].count - 8) { 1822 /* there is no place left in this ring */ 1823 IF_PREPEND(&ifp->if_snd, m0); 1824 ifp->if_flags |= IFF_OACTIVE; 1825 break; 1826 } 1827 1828 #if NBPFILTER > 0 1829 if (ifp->if_bpf != NULL) 1830 bpf_mtap(ifp->if_bpf, m0); 1831 #endif 1832 1833 m0 = ieee80211_encap(ic, m0, ni); 1834 if (m0 == NULL) { 1835 ieee80211_free_node(ni); 1836 ifp->if_oerrors++; 1837 continue; 1838 } 1839 1840 #if NBPFILTER > 0 1841 if (ic->ic_rawbpf != NULL) 1842 bpf_mtap(ic->ic_rawbpf, m0); 1843 #endif 1844 1845 if (iwi_tx_start(ifp, m0, ni, ac) != 0) { 1846 ieee80211_free_node(ni); 1847 ifp->if_oerrors++; 1848 break; 1849 } 1850 1851 /* start watchdog timer */ 1852 sc->sc_tx_timer = 5; 1853 ifp->if_timer = 1; 1854 } 1855 } 1856 1857 static void 1858 iwi_watchdog(struct ifnet *ifp) 1859 { 1860 struct iwi_softc *sc = ifp->if_softc; 1861 1862 ifp->if_timer = 0; 1863 1864 if (sc->sc_tx_timer > 0) { 1865 if (--sc->sc_tx_timer == 0) { 1866 aprint_error_dev(sc->sc_dev, "device timeout\n"); 1867 ifp->if_oerrors++; 1868 ifp->if_flags &= ~IFF_UP; 1869 iwi_stop(ifp, 1); 1870 return; 1871 } 1872 ifp->if_timer = 1; 1873 } 1874 1875 ieee80211_watchdog(&sc->sc_ic); 1876 } 1877 1878 static int 1879 iwi_get_table0(struct iwi_softc *sc, uint32_t *tbl) 1880 { 1881 uint32_t size, buf[128]; 1882 1883 if (!(sc->flags & IWI_FLAG_FW_INITED)) { 1884 memset(buf, 0, sizeof buf); 1885 return copyout(buf, tbl, sizeof buf); 1886 } 1887 1888 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1); 1889 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size); 1890 1891 return copyout(buf, tbl, sizeof buf); 1892 } 1893 1894 static int 1895 iwi_ioctl(struct ifnet *ifp, u_long cmd, void *data) 1896 { 1897 #define IS_RUNNING(ifp) \ 1898 ((ifp->if_flags & IFF_UP) && (ifp->if_flags & IFF_RUNNING)) 1899 1900 struct iwi_softc *sc = ifp->if_softc; 1901 struct ieee80211com *ic = &sc->sc_ic; 1902 struct ifreq *ifr = (struct ifreq *)data; 1903 int s, error = 0; 1904 int val; 1905 1906 s = splnet(); 1907 1908 switch (cmd) { 1909 case SIOCSIFFLAGS: 1910 if (ifp->if_flags & IFF_UP) { 1911 if (!(ifp->if_flags & IFF_RUNNING)) 1912 iwi_init(ifp); 1913 } else { 1914 if (ifp->if_flags & IFF_RUNNING) 1915 iwi_stop(ifp, 1); 1916 } 1917 break; 1918 1919 case SIOCADDMULTI: 1920 case SIOCDELMULTI: 1921 /* XXX no h/w multicast filter? --dyoung */ 1922 if ((error = ether_ioctl(ifp, cmd, data)) == ENETRESET) { 1923 /* setup multicast filter, etc */ 1924 error = 0; 1925 } 1926 break; 1927 1928 case SIOCGTABLE0: 1929 error = iwi_get_table0(sc, (uint32_t *)ifr->ifr_data); 1930 break; 1931 1932 case SIOCGRADIO: 1933 val = !iwi_getrfkill(sc); 1934 error = copyout(&val, (int *)ifr->ifr_data, sizeof val); 1935 break; 1936 1937 case SIOCSIFMEDIA: 1938 if (ifr->ifr_media & IFM_IEEE80211_ADHOC) { 1939 sc->sc_fwname = "iwi-ibss.fw"; 1940 } else if (ifr->ifr_media & IFM_IEEE80211_MONITOR) { 1941 sc->sc_fwname = "iwi-sniffer.fw"; 1942 } else { 1943 sc->sc_fwname = "iwi-bss.fw"; 1944 } 1945 error = iwi_cache_firmware(sc); 1946 if (error) 1947 break; 1948 /* FALLTRHOUGH */ 1949 1950 default: 1951 error = ieee80211_ioctl(&sc->sc_ic, cmd, data); 1952 1953 if (error == ENETRESET) { 1954 if (IS_RUNNING(ifp) && 1955 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)) 1956 iwi_init(ifp); 1957 error = 0; 1958 } 1959 } 1960 1961 splx(s); 1962 return error; 1963 #undef IS_RUNNING 1964 } 1965 1966 static void 1967 iwi_stop_master(struct iwi_softc *sc) 1968 { 1969 int ntries; 1970 1971 /* Disable interrupts */ 1972 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0); 1973 1974 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER); 1975 for (ntries = 0; ntries < 5; ntries++) { 1976 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED) 1977 break; 1978 DELAY(10); 1979 } 1980 if (ntries == 5) 1981 aprint_error_dev(sc->sc_dev, "timeout waiting for master\n"); 1982 1983 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) | 1984 IWI_RST_PRINCETON_RESET); 1985 1986 sc->flags &= ~IWI_FLAG_FW_INITED; 1987 } 1988 1989 static int 1990 iwi_reset(struct iwi_softc *sc) 1991 { 1992 int i, ntries; 1993 1994 iwi_stop_master(sc); 1995 1996 /* Move adapter to D0 state */ 1997 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) | 1998 IWI_CTL_INIT); 1999 2000 /* Initialize Phase-Locked Level (PLL) */ 2001 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST); 2002 2003 /* Wait for clock stabilization */ 2004 for (ntries = 0; ntries < 1000; ntries++) { 2005 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY) 2006 break; 2007 DELAY(200); 2008 } 2009 if (ntries == 1000) { 2010 aprint_error_dev(sc->sc_dev, 2011 "timeout waiting for clock stabilization\n"); 2012 return ETIMEDOUT; 2013 } 2014 2015 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) | 2016 IWI_RST_SW_RESET); 2017 2018 DELAY(10); 2019 2020 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) | 2021 IWI_CTL_INIT); 2022 2023 /* Clear NIC memory */ 2024 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0); 2025 for (i = 0; i < 0xc000; i++) 2026 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0); 2027 2028 return 0; 2029 } 2030 2031 static int 2032 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size) 2033 { 2034 uint16_t *w; 2035 int ntries, i; 2036 2037 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) | 2038 IWI_RST_STOP_MASTER); 2039 for (ntries = 0; ntries < 5; ntries++) { 2040 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED) 2041 break; 2042 DELAY(10); 2043 } 2044 if (ntries == 5) { 2045 aprint_error_dev(sc->sc_dev, "timeout waiting for master\n"); 2046 return ETIMEDOUT; 2047 } 2048 2049 MEM_WRITE_4(sc, 0x3000e0, 0x80000000); 2050 DELAY(5000); 2051 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) & 2052 ~IWI_RST_PRINCETON_RESET); 2053 DELAY(5000); 2054 MEM_WRITE_4(sc, 0x3000e0, 0); 2055 DELAY(1000); 2056 MEM_WRITE_4(sc, 0x300004, 1); 2057 DELAY(1000); 2058 MEM_WRITE_4(sc, 0x300004, 0); 2059 DELAY(1000); 2060 MEM_WRITE_1(sc, 0x200000, 0x00); 2061 MEM_WRITE_1(sc, 0x200000, 0x40); 2062 DELAY(1000); 2063 2064 /* Adapter is buggy, we must set the address for each word */ 2065 for (w = uc; size > 0; w++, size -= 2) 2066 MEM_WRITE_2(sc, 0x200010, htole16(*w)); 2067 2068 MEM_WRITE_1(sc, 0x200000, 0x00); 2069 MEM_WRITE_1(sc, 0x200000, 0x80); 2070 2071 /* Wait until we get a response in the uc queue */ 2072 for (ntries = 0; ntries < 100; ntries++) { 2073 if (MEM_READ_1(sc, 0x200000) & 1) 2074 break; 2075 DELAY(100); 2076 } 2077 if (ntries == 100) { 2078 aprint_error_dev(sc->sc_dev, 2079 "timeout waiting for ucode to initialize\n"); 2080 return ETIMEDOUT; 2081 } 2082 2083 /* Empty the uc queue or the firmware will not initialize properly */ 2084 for (i = 0; i < 7; i++) 2085 MEM_READ_4(sc, 0x200004); 2086 2087 MEM_WRITE_1(sc, 0x200000, 0x00); 2088 2089 return 0; 2090 } 2091 2092 /* macro to handle unaligned little endian data in firmware image */ 2093 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24) 2094 static int 2095 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size) 2096 { 2097 bus_dmamap_t map; 2098 u_char *p, *end; 2099 uint32_t sentinel, ctl, sum; 2100 uint32_t cs, sl, cd, cl; 2101 int ntries, nsegs, error; 2102 int sn; 2103 2104 nsegs = (size + PAGE_SIZE - 1) / PAGE_SIZE; 2105 2106 /* Create a DMA map for the firmware image */ 2107 error = bus_dmamap_create(sc->sc_dmat, size, nsegs, size, 0, 2108 BUS_DMA_NOWAIT, &map); 2109 if (error != 0) { 2110 aprint_error_dev(sc->sc_dev, 2111 "could not create firmware DMA map\n"); 2112 goto fail1; 2113 } 2114 2115 error = bus_dmamap_load(sc->sc_dmat, map, fw, size, NULL, 2116 BUS_DMA_NOWAIT | BUS_DMA_WRITE); 2117 if (error != 0) { 2118 aprint_error_dev(sc->sc_dev, "could not load fw dma map(%d)\n", 2119 error); 2120 goto fail2; 2121 } 2122 2123 /* Make sure the adapter will get up-to-date values */ 2124 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE); 2125 2126 /* Tell the adapter where the command blocks are stored */ 2127 MEM_WRITE_4(sc, 0x3000a0, 0x27000); 2128 2129 /* 2130 * Store command blocks into adapter's internal memory using register 2131 * indirections. The adapter will read the firmware image through DMA 2132 * using information stored in command blocks. 2133 */ 2134 p = fw; 2135 end = p + size; 2136 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000); 2137 2138 sn = 0; 2139 sl = cl = 0; 2140 cs = cd = 0; 2141 while (p < end) { 2142 if (sl == 0) { 2143 cs = map->dm_segs[sn].ds_addr; 2144 sl = map->dm_segs[sn].ds_len; 2145 sn++; 2146 } 2147 if (cl == 0) { 2148 cd = GETLE32(p); p += 4; cs += 4; sl -= 4; 2149 cl = GETLE32(p); p += 4; cs += 4; sl -= 4; 2150 } 2151 while (sl > 0 && cl > 0) { 2152 int len = min(cl, sl); 2153 2154 sl -= len; 2155 cl -= len; 2156 p += len; 2157 2158 while (len > 0) { 2159 int mlen = min(len, IWI_CB_MAXDATALEN); 2160 2161 ctl = IWI_CB_DEFAULT_CTL | mlen; 2162 sum = ctl ^ cs ^ cd; 2163 2164 /* Write a command block */ 2165 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl); 2166 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, cs); 2167 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, cd); 2168 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum); 2169 2170 cs += mlen; 2171 cd += mlen; 2172 len -= mlen; 2173 } 2174 } 2175 } 2176 2177 /* Write a fictive final command block (sentinel) */ 2178 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR); 2179 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0); 2180 2181 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) & 2182 ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER)); 2183 2184 /* Tell the adapter to start processing command blocks */ 2185 MEM_WRITE_4(sc, 0x3000a4, 0x540100); 2186 2187 /* Wait until the adapter has processed all command blocks */ 2188 for (ntries = 0; ntries < 400; ntries++) { 2189 if (MEM_READ_4(sc, 0x3000d0) >= sentinel) 2190 break; 2191 DELAY(100); 2192 } 2193 if (ntries == 400) { 2194 aprint_error_dev(sc->sc_dev, "timeout processing cb\n"); 2195 error = ETIMEDOUT; 2196 goto fail3; 2197 } 2198 2199 /* We're done with command blocks processing */ 2200 MEM_WRITE_4(sc, 0x3000a4, 0x540c00); 2201 2202 /* Allow interrupts so we know when the firmware is inited */ 2203 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK); 2204 2205 /* Tell the adapter to initialize the firmware */ 2206 CSR_WRITE_4(sc, IWI_CSR_RST, 0); 2207 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) | 2208 IWI_CTL_ALLOW_STANDBY); 2209 2210 /* Wait at most one second for firmware initialization to complete */ 2211 if ((error = tsleep(sc, 0, "iwiinit", hz)) != 0) { 2212 aprint_error_dev(sc->sc_dev, 2213 "timeout waiting for firmware initialization to complete\n"); 2214 goto fail3; 2215 } 2216 2217 fail3: 2218 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE); 2219 bus_dmamap_unload(sc->sc_dmat, map); 2220 fail2: 2221 bus_dmamap_destroy(sc->sc_dmat, map); 2222 2223 fail1: 2224 return error; 2225 } 2226 2227 /* 2228 * Store firmware into kernel memory so we can download it when we need to, 2229 * e.g when the adapter wakes up from suspend mode. 2230 */ 2231 static int 2232 iwi_cache_firmware(struct iwi_softc *sc) 2233 { 2234 struct iwi_firmware *kfw = &sc->fw; 2235 firmware_handle_t fwh; 2236 const struct iwi_firmware_hdr *hdr; 2237 off_t size; 2238 char *fw; 2239 int error; 2240 2241 iwi_free_firmware(sc); 2242 error = firmware_open("if_iwi", sc->sc_fwname, &fwh); 2243 if (error != 0) { 2244 aprint_error_dev(sc->sc_dev, "firmware_open failed\n"); 2245 goto fail1; 2246 } 2247 2248 size = firmware_get_size(fwh); 2249 if (size < sizeof(struct iwi_firmware_hdr)) { 2250 aprint_error_dev(sc->sc_dev, "image '%s' has no header\n", 2251 sc->sc_fwname); 2252 error = EIO; 2253 goto fail1; 2254 } 2255 2256 sc->sc_blob = firmware_malloc(size); 2257 if (sc->sc_blob == NULL) { 2258 error = ENOMEM; 2259 firmware_close(fwh); 2260 goto fail1; 2261 } 2262 2263 error = firmware_read(fwh, 0, sc->sc_blob, size); 2264 firmware_close(fwh); 2265 if (error != 0) 2266 goto fail2; 2267 2268 2269 hdr = (const struct iwi_firmware_hdr *)sc->sc_blob; 2270 if (size < sizeof(struct iwi_firmware_hdr) + hdr->bsize + hdr->usize + hdr->fsize) { 2271 aprint_error_dev(sc->sc_dev, "image '%s' too small\n", 2272 sc->sc_fwname); 2273 error = EIO; 2274 goto fail2; 2275 } 2276 2277 hdr = (const struct iwi_firmware_hdr *)sc->sc_blob; 2278 DPRINTF(("firmware version = %d\n", le32toh(hdr->version))); 2279 if ((IWI_FW_GET_MAJOR(le32toh(hdr->version)) != IWI_FW_REQ_MAJOR) || 2280 (IWI_FW_GET_MINOR(le32toh(hdr->version)) != IWI_FW_REQ_MINOR)) { 2281 aprint_error_dev(sc->sc_dev, 2282 "version for '%s' %d.%d != %d.%d\n", sc->sc_fwname, 2283 IWI_FW_GET_MAJOR(le32toh(hdr->version)), 2284 IWI_FW_GET_MINOR(le32toh(hdr->version)), 2285 IWI_FW_REQ_MAJOR, IWI_FW_REQ_MINOR); 2286 error = EIO; 2287 goto fail2; 2288 } 2289 2290 kfw->boot_size = hdr->bsize; 2291 kfw->ucode_size = hdr->usize; 2292 kfw->main_size = hdr->fsize; 2293 2294 fw = sc->sc_blob + sizeof(struct iwi_firmware_hdr); 2295 kfw->boot = fw; 2296 fw += kfw->boot_size; 2297 kfw->ucode = fw; 2298 fw += kfw->ucode_size; 2299 kfw->main = fw; 2300 2301 DPRINTF(("Firmware cached: boot %p, ucode %p, main %p\n", 2302 kfw->boot, kfw->ucode, kfw->main)); 2303 DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n", 2304 kfw->boot_size, kfw->ucode_size, kfw->main_size)); 2305 2306 sc->flags |= IWI_FLAG_FW_CACHED; 2307 2308 return 0; 2309 2310 2311 fail2: firmware_free(sc->sc_blob, 0); 2312 fail1: 2313 return error; 2314 } 2315 2316 static void 2317 iwi_free_firmware(struct iwi_softc *sc) 2318 { 2319 2320 if (!(sc->flags & IWI_FLAG_FW_CACHED)) 2321 return; 2322 2323 firmware_free(sc->sc_blob, 0); 2324 2325 sc->flags &= ~IWI_FLAG_FW_CACHED; 2326 } 2327 2328 static int 2329 iwi_config(struct iwi_softc *sc) 2330 { 2331 struct ieee80211com *ic = &sc->sc_ic; 2332 struct ifnet *ifp = &sc->sc_if; 2333 struct iwi_configuration config; 2334 struct iwi_rateset rs; 2335 struct iwi_txpower power; 2336 struct ieee80211_key *wk; 2337 struct iwi_wep_key wepkey; 2338 uint32_t data; 2339 int error, nchan, i; 2340 2341 IEEE80211_ADDR_COPY(ic->ic_myaddr, CLLADDR(ifp->if_sadl)); 2342 DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr))); 2343 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr, 2344 IEEE80211_ADDR_LEN, 0); 2345 if (error != 0) 2346 return error; 2347 2348 memset(&config, 0, sizeof config); 2349 config.bluetooth_coexistence = sc->bluetooth; 2350 config.antenna = sc->antenna; 2351 config.silence_threshold = 0x1e; 2352 config.multicast_enabled = 1; 2353 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0; 2354 config.disable_unicast_decryption = 1; 2355 config.disable_multicast_decryption = 1; 2356 DPRINTF(("Configuring adapter\n")); 2357 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config, sizeof config, 2358 0); 2359 if (error != 0) 2360 return error; 2361 2362 data = htole32(IWI_POWER_MODE_CAM); 2363 DPRINTF(("Setting power mode to %u\n", le32toh(data))); 2364 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0); 2365 if (error != 0) 2366 return error; 2367 2368 data = htole32(ic->ic_rtsthreshold); 2369 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data))); 2370 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0); 2371 if (error != 0) 2372 return error; 2373 2374 data = htole32(ic->ic_fragthreshold); 2375 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data))); 2376 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0); 2377 if (error != 0) 2378 return error; 2379 2380 /* 2381 * Set default Tx power for 802.11b/g and 802.11a channels. 2382 */ 2383 nchan = 0; 2384 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 2385 if (!IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i])) 2386 continue; 2387 power.chan[nchan].chan = i; 2388 power.chan[nchan].power = IWI_TXPOWER_MAX; 2389 nchan++; 2390 } 2391 power.nchan = nchan; 2392 2393 power.mode = IWI_MODE_11G; 2394 DPRINTF(("Setting .11g channels tx power\n")); 2395 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 0); 2396 if (error != 0) 2397 return error; 2398 2399 power.mode = IWI_MODE_11B; 2400 DPRINTF(("Setting .11b channels tx power\n")); 2401 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 0); 2402 if (error != 0) 2403 return error; 2404 2405 nchan = 0; 2406 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 2407 if (!IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i])) 2408 continue; 2409 power.chan[nchan].chan = i; 2410 power.chan[nchan].power = IWI_TXPOWER_MAX; 2411 nchan++; 2412 } 2413 power.nchan = nchan; 2414 2415 if (nchan > 0) { /* 2915ABG only */ 2416 power.mode = IWI_MODE_11A; 2417 DPRINTF(("Setting .11a channels tx power\n")); 2418 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 2419 0); 2420 if (error != 0) 2421 return error; 2422 } 2423 2424 rs.mode = IWI_MODE_11G; 2425 rs.type = IWI_RATESET_TYPE_SUPPORTED; 2426 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates; 2427 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates, 2428 rs.nrates); 2429 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates)); 2430 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0); 2431 if (error != 0) 2432 return error; 2433 2434 rs.mode = IWI_MODE_11A; 2435 rs.type = IWI_RATESET_TYPE_SUPPORTED; 2436 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates; 2437 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates, 2438 rs.nrates); 2439 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates)); 2440 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0); 2441 if (error != 0) 2442 return error; 2443 2444 /* if we have a desired ESSID, set it now */ 2445 if (ic->ic_des_esslen != 0) { 2446 #ifdef IWI_DEBUG 2447 if (iwi_debug > 0) { 2448 printf("Setting desired ESSID to "); 2449 ieee80211_print_essid(ic->ic_des_essid, 2450 ic->ic_des_esslen); 2451 printf("\n"); 2452 } 2453 #endif 2454 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ic->ic_des_essid, 2455 ic->ic_des_esslen, 0); 2456 if (error != 0) 2457 return error; 2458 } 2459 2460 data = htole32(arc4random()); 2461 DPRINTF(("Setting initialization vector to %u\n", le32toh(data))); 2462 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0); 2463 if (error != 0) 2464 return error; 2465 2466 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 2467 /* XXX iwi_setwepkeys? */ 2468 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2469 wk = &ic->ic_crypto.cs_nw_keys[i]; 2470 2471 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY; 2472 wepkey.idx = i; 2473 wepkey.len = wk->wk_keylen; 2474 memset(wepkey.key, 0, sizeof wepkey.key); 2475 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen); 2476 DPRINTF(("Setting wep key index %u len %u\n", 2477 wepkey.idx, wepkey.len)); 2478 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey, 2479 sizeof wepkey, 0); 2480 if (error != 0) 2481 return error; 2482 } 2483 } 2484 2485 /* Enable adapter */ 2486 DPRINTF(("Enabling adapter\n")); 2487 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0); 2488 } 2489 2490 static int 2491 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan) 2492 { 2493 struct ieee80211com *ic = &sc->sc_ic; 2494 struct iwi_scan_v2 scan; 2495 2496 (void)memset(&scan, 0, sizeof scan); 2497 2498 scan.dwelltime[IWI_SCAN_TYPE_PASSIVE] = htole16(2000); 2499 scan.channels[0] = 1 | 2500 (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ); 2501 scan.channels[1] = ieee80211_chan2ieee(ic, chan); 2502 iwi_scan_type_set(scan, 1, IWI_SCAN_TYPE_PASSIVE); 2503 2504 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan))); 2505 return iwi_cmd(sc, IWI_CMD_SCAN_V2, &scan, sizeof scan, 1); 2506 } 2507 2508 static int 2509 iwi_scan(struct iwi_softc *sc) 2510 { 2511 struct ieee80211com *ic = &sc->sc_ic; 2512 struct iwi_scan_v2 scan; 2513 uint32_t type; 2514 uint8_t *p; 2515 int i, count, idx; 2516 2517 (void)memset(&scan, 0, sizeof scan); 2518 scan.dwelltime[IWI_SCAN_TYPE_ACTIVE_BROADCAST] = 2519 htole16(sc->dwelltime); 2520 scan.dwelltime[IWI_SCAN_TYPE_ACTIVE_BDIRECT] = 2521 htole16(sc->dwelltime); 2522 2523 /* tell the firmware about the desired essid */ 2524 if (ic->ic_des_esslen) { 2525 int error; 2526 2527 DPRINTF(("%s: Setting adapter desired ESSID to %s\n", 2528 __func__, ic->ic_des_essid)); 2529 2530 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, 2531 ic->ic_des_essid, ic->ic_des_esslen, 1); 2532 if (error) 2533 return error; 2534 2535 type = IWI_SCAN_TYPE_ACTIVE_BDIRECT; 2536 } else { 2537 type = IWI_SCAN_TYPE_ACTIVE_BROADCAST; 2538 } 2539 2540 p = &scan.channels[0]; 2541 count = idx = 0; 2542 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 2543 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) && 2544 isset(ic->ic_chan_active, i)) { 2545 *++p = i; 2546 count++; 2547 idx++; 2548 iwi_scan_type_set(scan, idx, type); 2549 } 2550 } 2551 if (count) { 2552 *(p - count) = IWI_CHAN_5GHZ | count; 2553 p++; 2554 } 2555 2556 count = 0; 2557 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 2558 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) && 2559 isset(ic->ic_chan_active, i)) { 2560 *++p = i; 2561 count++; 2562 idx++; 2563 iwi_scan_type_set(scan, idx, type); 2564 } 2565 } 2566 *(p - count) = IWI_CHAN_2GHZ | count; 2567 2568 DPRINTF(("Start scanning\n")); 2569 return iwi_cmd(sc, IWI_CMD_SCAN_V2, &scan, sizeof scan, 1); 2570 } 2571 2572 static int 2573 iwi_auth_and_assoc(struct iwi_softc *sc) 2574 { 2575 struct ieee80211com *ic = &sc->sc_ic; 2576 struct ieee80211_node *ni = ic->ic_bss; 2577 struct ifnet *ifp = &sc->sc_if; 2578 struct ieee80211_wme_info wme; 2579 struct iwi_configuration config; 2580 struct iwi_associate assoc; 2581 struct iwi_rateset rs; 2582 uint16_t capinfo; 2583 uint32_t data; 2584 int error; 2585 2586 memset(&config, 0, sizeof config); 2587 config.bluetooth_coexistence = sc->bluetooth; 2588 config.antenna = sc->antenna; 2589 config.multicast_enabled = 1; 2590 config.silence_threshold = 0x1e; 2591 if (ic->ic_curmode == IEEE80211_MODE_11G) 2592 config.use_protection = 1; 2593 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0; 2594 config.disable_unicast_decryption = 1; 2595 config.disable_multicast_decryption = 1; 2596 2597 DPRINTF(("Configuring adapter\n")); 2598 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config, 2599 sizeof config, 1); 2600 if (error != 0) 2601 return error; 2602 2603 #ifdef IWI_DEBUG 2604 if (iwi_debug > 0) { 2605 aprint_debug_dev(sc->sc_dev, "Setting ESSID to "); 2606 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 2607 aprint_debug("\n"); 2608 } 2609 #endif 2610 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1); 2611 if (error != 0) 2612 return error; 2613 2614 /* the rate set has already been "negotiated" */ 2615 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A : 2616 IWI_MODE_11G; 2617 rs.type = IWI_RATESET_TYPE_NEGOTIATED; 2618 rs.nrates = ni->ni_rates.rs_nrates; 2619 2620 if (rs.nrates > IWI_RATESET_SIZE) { 2621 DPRINTF(("Truncating negotiated rate set from %u\n", 2622 rs.nrates)); 2623 rs.nrates = IWI_RATESET_SIZE; 2624 } 2625 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates); 2626 DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates)); 2627 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1); 2628 if (error != 0) 2629 return error; 2630 2631 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) { 2632 wme.wme_id = IEEE80211_ELEMID_VENDOR; 2633 wme.wme_len = sizeof (struct ieee80211_wme_info) - 2; 2634 wme.wme_oui[0] = 0x00; 2635 wme.wme_oui[1] = 0x50; 2636 wme.wme_oui[2] = 0xf2; 2637 wme.wme_type = WME_OUI_TYPE; 2638 wme.wme_subtype = WME_INFO_OUI_SUBTYPE; 2639 wme.wme_version = WME_VERSION; 2640 wme.wme_info = 0; 2641 2642 DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len)); 2643 error = iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme, 1); 2644 if (error != 0) 2645 return error; 2646 } 2647 2648 if (ic->ic_opt_ie != NULL) { 2649 DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len)); 2650 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie, 2651 ic->ic_opt_ie_len, 1); 2652 if (error != 0) 2653 return error; 2654 } 2655 data = htole32(ni->ni_rssi); 2656 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi)); 2657 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1); 2658 if (error != 0) 2659 return error; 2660 2661 memset(&assoc, 0, sizeof assoc); 2662 if (IEEE80211_IS_CHAN_A(ni->ni_chan)) 2663 assoc.mode = IWI_MODE_11A; 2664 else if (IEEE80211_IS_CHAN_G(ni->ni_chan)) 2665 assoc.mode = IWI_MODE_11G; 2666 else if (IEEE80211_IS_CHAN_B(ni->ni_chan)) 2667 assoc.mode = IWI_MODE_11B; 2668 2669 assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 2670 2671 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) 2672 assoc.auth = (ic->ic_crypto.cs_def_txkey << 4) | IWI_AUTH_SHARED; 2673 2674 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 2675 assoc.plen = IWI_ASSOC_SHPREAMBLE; 2676 2677 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) 2678 assoc.policy |= htole16(IWI_POLICY_WME); 2679 if (ic->ic_flags & IEEE80211_F_WPA) 2680 assoc.policy |= htole16(IWI_POLICY_WPA); 2681 if (ic->ic_opmode == IEEE80211_M_IBSS && ni->ni_tstamp.tsf == 0) 2682 assoc.type = IWI_HC_IBSS_START; 2683 else 2684 assoc.type = IWI_HC_ASSOC; 2685 memcpy(assoc.tstamp, ni->ni_tstamp.data, 8); 2686 2687 if (ic->ic_opmode == IEEE80211_M_IBSS) 2688 capinfo = IEEE80211_CAPINFO_IBSS; 2689 else 2690 capinfo = IEEE80211_CAPINFO_ESS; 2691 if (ic->ic_flags & IEEE80211_F_PRIVACY) 2692 capinfo |= IEEE80211_CAPINFO_PRIVACY; 2693 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 2694 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 2695 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 2696 if (ic->ic_flags & IEEE80211_F_SHSLOT) 2697 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 2698 assoc.capinfo = htole16(capinfo); 2699 2700 assoc.lintval = htole16(ic->ic_lintval); 2701 assoc.intval = htole16(ni->ni_intval); 2702 IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid); 2703 if (ic->ic_opmode == IEEE80211_M_IBSS) 2704 IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr); 2705 else 2706 IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid); 2707 2708 DPRINTF(("%s bssid %s dst %s channel %u policy 0x%x " 2709 "auth %u capinfo 0x%x lintval %u bintval %u\n", 2710 assoc.type == IWI_HC_IBSS_START ? "Start" : "Join", 2711 ether_sprintf(assoc.bssid), ether_sprintf(assoc.dst), 2712 assoc.chan, le16toh(assoc.policy), assoc.auth, 2713 le16toh(assoc.capinfo), le16toh(assoc.lintval), 2714 le16toh(assoc.intval))); 2715 2716 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1); 2717 } 2718 2719 static int 2720 iwi_init(struct ifnet *ifp) 2721 { 2722 struct iwi_softc *sc = ifp->if_softc; 2723 struct ieee80211com *ic = &sc->sc_ic; 2724 struct iwi_firmware *fw = &sc->fw; 2725 int i, error; 2726 2727 /* exit immediately if firmware has not been ioctl'd */ 2728 if (!(sc->flags & IWI_FLAG_FW_CACHED)) { 2729 if ((error = iwi_cache_firmware(sc)) != 0) { 2730 aprint_error_dev(sc->sc_dev, 2731 "could not cache the firmware\n"); 2732 goto fail; 2733 } 2734 } 2735 2736 iwi_stop(ifp, 0); 2737 2738 if ((error = iwi_reset(sc)) != 0) { 2739 aprint_error_dev(sc->sc_dev, "could not reset adapter\n"); 2740 goto fail; 2741 } 2742 2743 if ((error = iwi_load_firmware(sc, fw->boot, fw->boot_size)) != 0) { 2744 aprint_error_dev(sc->sc_dev, "could not load boot firmware\n"); 2745 goto fail; 2746 } 2747 2748 if ((error = iwi_load_ucode(sc, fw->ucode, fw->ucode_size)) != 0) { 2749 aprint_error_dev(sc->sc_dev, "could not load microcode\n"); 2750 goto fail; 2751 } 2752 2753 iwi_stop_master(sc); 2754 2755 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.desc_map->dm_segs[0].ds_addr); 2756 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count); 2757 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur); 2758 2759 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].desc_map->dm_segs[0].ds_addr); 2760 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count); 2761 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur); 2762 2763 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].desc_map->dm_segs[0].ds_addr); 2764 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count); 2765 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur); 2766 2767 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].desc_map->dm_segs[0].ds_addr); 2768 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count); 2769 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur); 2770 2771 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].desc_map->dm_segs[0].ds_addr); 2772 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count); 2773 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur); 2774 2775 for (i = 0; i < sc->rxq.count; i++) 2776 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, 2777 sc->rxq.data[i].map->dm_segs[0].ds_addr); 2778 2779 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count -1); 2780 2781 if ((error = iwi_load_firmware(sc, fw->main, fw->main_size)) != 0) { 2782 aprint_error_dev(sc->sc_dev, "could not load main firmware\n"); 2783 goto fail; 2784 } 2785 2786 sc->flags |= IWI_FLAG_FW_INITED; 2787 2788 if ((error = iwi_config(sc)) != 0) { 2789 aprint_error_dev(sc->sc_dev, "device configuration failed\n"); 2790 goto fail; 2791 } 2792 2793 ic->ic_state = IEEE80211_S_INIT; 2794 2795 ifp->if_flags &= ~IFF_OACTIVE; 2796 ifp->if_flags |= IFF_RUNNING; 2797 2798 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2799 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL) 2800 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2801 } else 2802 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 2803 2804 return 0; 2805 2806 fail: ifp->if_flags &= ~IFF_UP; 2807 iwi_stop(ifp, 0); 2808 2809 return error; 2810 } 2811 2812 2813 /* 2814 * Return whether or not the radio is enabled in hardware 2815 * (i.e. the rfkill switch is "off"). 2816 */ 2817 static int 2818 iwi_getrfkill(struct iwi_softc *sc) 2819 { 2820 return (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) == 0; 2821 } 2822 2823 static int 2824 iwi_sysctl_radio(SYSCTLFN_ARGS) 2825 { 2826 struct sysctlnode node; 2827 struct iwi_softc *sc; 2828 int val, error; 2829 2830 node = *rnode; 2831 sc = (struct iwi_softc *)node.sysctl_data; 2832 2833 val = !iwi_getrfkill(sc); 2834 2835 node.sysctl_data = &val; 2836 error = sysctl_lookup(SYSCTLFN_CALL(&node)); 2837 2838 if (error || newp == NULL) 2839 return error; 2840 2841 return 0; 2842 } 2843 2844 #ifdef IWI_DEBUG 2845 SYSCTL_SETUP(sysctl_iwi, "sysctl iwi(4) subtree setup") 2846 { 2847 int rc; 2848 const struct sysctlnode *rnode; 2849 const struct sysctlnode *cnode; 2850 2851 if ((rc = sysctl_createv(clog, 0, NULL, &rnode, 2852 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL, 2853 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) 2854 goto err; 2855 2856 if ((rc = sysctl_createv(clog, 0, &rnode, &rnode, 2857 CTLFLAG_PERMANENT, CTLTYPE_NODE, "iwi", 2858 SYSCTL_DESCR("iwi global controls"), 2859 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0) 2860 goto err; 2861 2862 /* control debugging printfs */ 2863 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode, 2864 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 2865 "debug", SYSCTL_DESCR("Enable debugging output"), 2866 NULL, 0, &iwi_debug, 0, CTL_CREATE, CTL_EOL)) != 0) 2867 goto err; 2868 2869 return; 2870 err: 2871 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc); 2872 } 2873 2874 #endif /* IWI_DEBUG */ 2875 2876 /* 2877 * Add sysctl knobs. 2878 */ 2879 static void 2880 iwi_sysctlattach(struct iwi_softc *sc) 2881 { 2882 int rc; 2883 const struct sysctlnode *rnode; 2884 const struct sysctlnode *cnode; 2885 2886 struct sysctllog **clog = &sc->sc_sysctllog; 2887 2888 if ((rc = sysctl_createv(clog, 0, NULL, &rnode, 2889 CTLFLAG_PERMANENT, CTLTYPE_NODE, "hw", NULL, 2890 NULL, 0, NULL, 0, CTL_HW, CTL_EOL)) != 0) 2891 goto err; 2892 2893 if ((rc = sysctl_createv(clog, 0, &rnode, &rnode, 2894 CTLFLAG_PERMANENT, CTLTYPE_NODE, device_xname(sc->sc_dev), 2895 SYSCTL_DESCR("iwi controls and statistics"), 2896 NULL, 0, NULL, 0, CTL_CREATE, CTL_EOL)) != 0) 2897 goto err; 2898 2899 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode, 2900 CTLFLAG_PERMANENT, CTLTYPE_INT, "radio", 2901 SYSCTL_DESCR("radio transmitter switch state (0=off, 1=on)"), 2902 iwi_sysctl_radio, 0, sc, 0, CTL_CREATE, CTL_EOL)) != 0) 2903 goto err; 2904 2905 sc->dwelltime = 100; 2906 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode, 2907 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 2908 "dwell", SYSCTL_DESCR("channel dwell time (ms) for AP/station scanning"), 2909 NULL, 0, &sc->dwelltime, 0, CTL_CREATE, CTL_EOL)) != 0) 2910 goto err; 2911 2912 sc->bluetooth = 0; 2913 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode, 2914 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 2915 "bluetooth", SYSCTL_DESCR("bluetooth coexistence"), 2916 NULL, 0, &sc->bluetooth, 0, CTL_CREATE, CTL_EOL)) != 0) 2917 goto err; 2918 2919 sc->antenna = IWI_ANTENNA_AUTO; 2920 if ((rc = sysctl_createv(clog, 0, &rnode, &cnode, 2921 CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT, 2922 "antenna", SYSCTL_DESCR("antenna (0=auto)"), 2923 NULL, 0, &sc->antenna, 0, CTL_CREATE, CTL_EOL)) != 0) 2924 goto err; 2925 2926 return; 2927 err: 2928 aprint_error("%s: sysctl_createv failed (rc = %d)\n", __func__, rc); 2929 } 2930 2931 static void 2932 iwi_stop(struct ifnet *ifp, int disable) 2933 { 2934 struct iwi_softc *sc = ifp->if_softc; 2935 struct ieee80211com *ic = &sc->sc_ic; 2936 2937 IWI_LED_OFF(sc); 2938 2939 iwi_stop_master(sc); 2940 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET); 2941 2942 /* reset rings */ 2943 iwi_reset_cmd_ring(sc, &sc->cmdq); 2944 iwi_reset_tx_ring(sc, &sc->txq[0]); 2945 iwi_reset_tx_ring(sc, &sc->txq[1]); 2946 iwi_reset_tx_ring(sc, &sc->txq[2]); 2947 iwi_reset_tx_ring(sc, &sc->txq[3]); 2948 iwi_reset_rx_ring(sc, &sc->rxq); 2949 2950 ifp->if_timer = 0; 2951 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2952 2953 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2954 } 2955 2956 static void 2957 iwi_led_set(struct iwi_softc *sc, uint32_t state, int toggle) 2958 { 2959 uint32_t val; 2960 2961 val = MEM_READ_4(sc, IWI_MEM_EVENT_CTL); 2962 2963 switch (sc->nictype) { 2964 case 1: 2965 /* special NIC type: reversed leds */ 2966 if (state == IWI_LED_ACTIVITY) { 2967 state &= ~IWI_LED_ACTIVITY; 2968 state |= IWI_LED_ASSOCIATED; 2969 } else if (state == IWI_LED_ASSOCIATED) { 2970 state &= ~IWI_LED_ASSOCIATED; 2971 state |= IWI_LED_ACTIVITY; 2972 } 2973 /* and ignore toggle effect */ 2974 val |= state; 2975 break; 2976 case 0: 2977 case 2: 2978 case 3: 2979 case 4: 2980 val = (toggle && (val & state)) ? val & ~state : val | state; 2981 break; 2982 default: 2983 aprint_normal_dev(sc->sc_dev, "unknown NIC type %d\n", 2984 sc->nictype); 2985 return; 2986 break; 2987 } 2988 2989 MEM_WRITE_4(sc, IWI_MEM_EVENT_CTL, val); 2990 2991 return; 2992 } 2993