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