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