1 /* $NetBSD: if_iwi.c,v 1.22 2005/09/12 21:15:04 skrll Exp $ */ 2 3 /*- 4 * Copyright (c) 2004, 2005 5 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __KERNEL_RCSID(0, "$NetBSD: if_iwi.c,v 1.22 2005/09/12 21:15:04 skrll Exp $"); 32 33 /*- 34 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG driver 35 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm 36 */ 37 38 #include "bpfilter.h" 39 40 #include <sys/param.h> 41 #include <sys/sockio.h> 42 #include <sys/sysctl.h> 43 #include <sys/mbuf.h> 44 #include <sys/kernel.h> 45 #include <sys/socket.h> 46 #include <sys/systm.h> 47 #include <sys/malloc.h> 48 #include <sys/conf.h> 49 50 #include <machine/bus.h> 51 #include <machine/endian.h> 52 #include <machine/intr.h> 53 54 #include <dev/pci/pcireg.h> 55 #include <dev/pci/pcivar.h> 56 #include <dev/pci/pcidevs.h> 57 58 #if NBPFILTER > 0 59 #include <net/bpf.h> 60 #endif 61 #include <net/if.h> 62 #include <net/if_arp.h> 63 #include <net/if_dl.h> 64 #include <net/if_ether.h> 65 #include <net/if_media.h> 66 #include <net/if_types.h> 67 68 #include <net80211/ieee80211_var.h> 69 #include <net80211/ieee80211_radiotap.h> 70 71 #include <netinet/in.h> 72 #include <netinet/in_systm.h> 73 #include <netinet/in_var.h> 74 #include <netinet/ip.h> 75 76 #include <crypto/arc4/arc4.h> 77 78 #include <dev/pci/if_iwireg.h> 79 #include <dev/pci/if_iwivar.h> 80 81 #ifdef IWI_DEBUG 82 #define DPRINTF(x) if (iwi_debug > 0) printf x 83 #define DPRINTFN(n, x) if (iwi_debug >= (n)) printf x 84 int iwi_debug = 4; 85 #else 86 #define DPRINTF(x) 87 #define DPRINTFN(n, x) 88 #endif 89 90 static int iwi_match(struct device *, struct cfdata *, void *); 91 static void iwi_attach(struct device *, struct device *, void *); 92 static int iwi_detach(struct device *, int); 93 94 static void iwi_shutdown(void *); 95 static int iwi_suspend(struct iwi_softc *); 96 static int iwi_resume(struct iwi_softc *); 97 static void iwi_powerhook(int, void *); 98 99 static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *, 100 int); 101 static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *); 102 static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *); 103 static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *, 104 int); 105 static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *); 106 static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *); 107 static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *, 108 int); 109 static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *); 110 static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *); 111 112 static int iwi_media_change(struct ifnet *); 113 static void iwi_media_status(struct ifnet *, struct ifmediareq *); 114 static u_int16_t iwi_read_prom_word(struct iwi_softc *, u_int8_t); 115 static int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int); 116 static void iwi_fix_channel(struct ieee80211com *, struct mbuf *); 117 static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int, 118 struct iwi_frame *); 119 static void iwi_notification_intr(struct iwi_softc *, struct iwi_rx_data *, 120 struct iwi_notif *); 121 static void iwi_rx_intr(struct iwi_softc *); 122 static void iwi_tx_intr(struct iwi_softc *); 123 static int iwi_intr(void *); 124 static int iwi_cmd(struct iwi_softc *, u_int8_t, void *, u_int8_t, int); 125 static int iwi_tx_start(struct ifnet *, struct mbuf *, struct ieee80211_node *); 126 static void iwi_start(struct ifnet *); 127 static void iwi_watchdog(struct ifnet *); 128 static int iwi_get_table0(struct iwi_softc *, u_int32_t *); 129 static int iwi_get_radio(struct iwi_softc *, int *); 130 static int iwi_ioctl(struct ifnet *, u_long, caddr_t); 131 static void iwi_stop_master(struct iwi_softc *); 132 static int iwi_reset(struct iwi_softc *); 133 static int iwi_load_ucode(struct iwi_softc *, void *, int); 134 static int iwi_load_firmware(struct iwi_softc *, void *, int); 135 static int iwi_cache_firmware(struct iwi_softc *, void *); 136 static void iwi_free_firmware(struct iwi_softc *); 137 static int iwi_config(struct iwi_softc *); 138 static int iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *); 139 static int iwi_scan(struct iwi_softc *); 140 static int iwi_auth_and_assoc(struct iwi_softc *); 141 static int iwi_init(struct ifnet *); 142 static void iwi_stop(struct ifnet *, int); 143 144 /* 145 * Supported rates for 802.11a/b/g modes (in 500Kbps unit). 146 */ 147 static const struct ieee80211_rateset iwi_rateset_11a = 148 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } }; 149 150 static const struct ieee80211_rateset iwi_rateset_11b = 151 { 4, { 2, 4, 11, 22 } }; 152 153 static const struct ieee80211_rateset iwi_rateset_11g = 154 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 155 156 static __inline u_int8_t 157 MEM_READ_1(struct iwi_softc *sc, u_int32_t addr) 158 { 159 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr); 160 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA); 161 } 162 163 static __inline u_int32_t 164 MEM_READ_4(struct iwi_softc *sc, u_int32_t addr) 165 { 166 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr); 167 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA); 168 } 169 170 CFATTACH_DECL(iwi, sizeof (struct iwi_softc), iwi_match, iwi_attach, 171 iwi_detach, NULL); 172 173 static int 174 iwi_match(struct device *parent, struct cfdata *match, void *aux) 175 { 176 struct pci_attach_args *pa = aux; 177 178 if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_INTEL) 179 return 0; 180 181 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2200BG || 182 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2225BG || 183 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 || 184 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2) 185 return 1; 186 187 return 0; 188 } 189 190 /* Base Address Register */ 191 #define IWI_PCI_BAR0 0x10 192 193 static void 194 iwi_attach(struct device *parent, struct device *self, void *aux) 195 { 196 struct iwi_softc *sc = (struct iwi_softc *)self; 197 struct ieee80211com *ic = &sc->sc_ic; 198 struct ifnet *ifp = &sc->sc_if; 199 struct pci_attach_args *pa = aux; 200 const char *intrstr; 201 char devinfo[256]; 202 bus_space_tag_t memt; 203 bus_space_handle_t memh; 204 bus_addr_t base; 205 pci_intr_handle_t ih; 206 pcireg_t data; 207 u_int16_t val; 208 int error, revision, i; 209 210 sc->sc_pct = pa->pa_pc; 211 sc->sc_pcitag = pa->pa_tag; 212 213 pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof devinfo); 214 revision = PCI_REVISION(pa->pa_class); 215 aprint_normal(": %s (rev. 0x%02x)\n", devinfo, revision); 216 217 /* clear device specific PCI configuration register 0x41 */ 218 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40); 219 data &= ~0x0000ff00; 220 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data); 221 222 /* enable bus-mastering */ 223 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG); 224 data |= PCI_COMMAND_MASTER_ENABLE; 225 pci_conf_write(sc->sc_pct, sc->sc_pcitag, PCI_COMMAND_STATUS_REG, data); 226 227 /* map the register window */ 228 error = pci_mapreg_map(pa, IWI_PCI_BAR0, PCI_MAPREG_TYPE_MEM | 229 PCI_MAPREG_MEM_TYPE_32BIT, 0, &memt, &memh, &base, &sc->sc_sz); 230 if (error != 0) { 231 aprint_error("%s: could not map memory space\n", 232 sc->sc_dev.dv_xname); 233 return; 234 } 235 236 sc->sc_st = memt; 237 sc->sc_sh = memh; 238 sc->sc_dmat = pa->pa_dmat; 239 240 /* disable interrupts */ 241 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0); 242 243 if (pci_intr_map(pa, &ih) != 0) { 244 aprint_error("%s: could not map interrupt\n", 245 sc->sc_dev.dv_xname); 246 return; 247 } 248 249 intrstr = pci_intr_string(sc->sc_pct, ih); 250 sc->sc_ih = pci_intr_establish(sc->sc_pct, ih, IPL_NET, iwi_intr, sc); 251 if (sc->sc_ih == NULL) { 252 aprint_error("%s: could not establish interrupt", 253 sc->sc_dev.dv_xname); 254 if (intrstr != NULL) 255 aprint_error(" at %s", intrstr); 256 aprint_error("\n"); 257 return; 258 } 259 aprint_normal("%s: interrupting at %s\n", sc->sc_dev.dv_xname, intrstr); 260 261 if (iwi_reset(sc) != 0) { 262 aprint_error("%s: could not reset adapter\n", 263 sc->sc_dev.dv_xname); 264 return; 265 } 266 267 /* 268 * Allocate rings. 269 */ 270 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) { 271 aprint_error("%s: could not allocate command ring\n", 272 sc->sc_dev.dv_xname); 273 goto fail; 274 } 275 276 if (iwi_alloc_tx_ring(sc, &sc->txq, IWI_TX_RING_COUNT) != 0) { 277 aprint_error("%s: could not allocate Tx ring\n", 278 sc->sc_dev.dv_xname); 279 goto fail; 280 } 281 282 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) { 283 aprint_error("%s: could not allocate Rx ring\n", 284 sc->sc_dev.dv_xname); 285 goto fail; 286 } 287 288 ic->ic_ifp = ifp; 289 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 290 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 291 ic->ic_state = IEEE80211_S_INIT; 292 293 /* set device capabilities */ 294 ic->ic_caps = IEEE80211_C_WPA | IEEE80211_C_PMGT | IEEE80211_C_TXPMGT | 295 IEEE80211_C_SHPREAMBLE | IEEE80211_C_MONITOR; 296 297 /* read MAC address from EEPROM */ 298 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0); 299 ic->ic_myaddr[0] = val >> 8; 300 ic->ic_myaddr[1] = val & 0xff; 301 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1); 302 ic->ic_myaddr[2] = val >> 8; 303 ic->ic_myaddr[3] = val & 0xff; 304 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2); 305 ic->ic_myaddr[4] = val >> 8; 306 ic->ic_myaddr[5] = val & 0xff; 307 308 aprint_normal("%s: 802.11 address %s\n", sc->sc_dev.dv_xname, 309 ether_sprintf(ic->ic_myaddr)); 310 311 312 if (PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_1 || 313 PCI_PRODUCT(pa->pa_id) == PCI_PRODUCT_INTEL_PRO_WL_2915ABG_2) { 314 /* set supported .11a rates (2915ABG only) */ 315 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a; 316 317 /* set supported .11a channels */ 318 for (i = 36; i <= 64; i += 4) { 319 ic->ic_channels[i].ic_freq = 320 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 321 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 322 } 323 for (i = 149; i <= 165; i += 4) { 324 ic->ic_channels[i].ic_freq = 325 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 326 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 327 } 328 } 329 330 /* set supported .11b and .11g rates */ 331 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b; 332 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g; 333 334 /* set supported .11b and .11g channels (1 through 14) */ 335 for (i = 1; i <= 14; i++) { 336 ic->ic_channels[i].ic_freq = 337 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 338 ic->ic_channels[i].ic_flags = 339 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 340 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 341 } 342 343 ifp->if_softc = sc; 344 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 345 ifp->if_init = iwi_init; 346 ifp->if_stop = iwi_stop; 347 ifp->if_ioctl = iwi_ioctl; 348 ifp->if_start = iwi_start; 349 ifp->if_watchdog = iwi_watchdog; 350 IFQ_SET_READY(&ifp->if_snd); 351 memcpy(ifp->if_xname, sc->sc_dev.dv_xname, IFNAMSIZ); 352 353 if_attach(ifp); 354 ieee80211_ifattach(ic); 355 /* override state transition machine */ 356 sc->sc_newstate = ic->ic_newstate; 357 ic->ic_newstate = iwi_newstate; 358 ieee80211_media_init(ic, iwi_media_change, iwi_media_status); 359 360 #if NBPFILTER > 0 361 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 362 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf); 363 364 sc->sc_rxtap_len = sizeof sc->sc_rxtapu; 365 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 366 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT); 367 368 sc->sc_txtap_len = sizeof sc->sc_txtapu; 369 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 370 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT); 371 #endif 372 373 /* 374 * Make sure the interface is shutdown during reboot. 375 */ 376 sc->sc_sdhook = shutdownhook_establish(iwi_shutdown, sc); 377 if (sc->sc_sdhook == NULL) 378 aprint_error("%s: WARNING: unable to establish shutdown hook\n", 379 sc->sc_dev.dv_xname); 380 sc->sc_powerhook = powerhook_establish(iwi_powerhook, sc); 381 if (sc->sc_powerhook == NULL) 382 printf("%s: WARNING: unable to establish power hook\n", 383 sc->sc_dev.dv_xname); 384 385 ieee80211_announce(ic); 386 /* 387 * Add a few sysctl knobs. 388 * XXX: Not yet. 389 */ 390 sc->dwelltime = 100; 391 sc->bluetooth = 1; 392 sc->antenna = 0; 393 394 return; 395 396 fail: iwi_detach(self, 0); 397 } 398 399 static int 400 iwi_detach(struct device* self, int flags) 401 { 402 struct iwi_softc *sc = (struct iwi_softc *)self; 403 struct ifnet *ifp = &sc->sc_if; 404 405 iwi_stop(ifp, 1); 406 iwi_free_firmware(sc); 407 408 #if NBPFILTER > 0 409 bpfdetach(ifp); 410 #endif 411 ieee80211_ifdetach(&sc->sc_ic); 412 if_detach(ifp); 413 414 iwi_free_cmd_ring(sc, &sc->cmdq); 415 iwi_free_tx_ring(sc, &sc->txq); 416 iwi_free_rx_ring(sc, &sc->rxq); 417 418 if (sc->sc_ih != NULL) { 419 pci_intr_disestablish(sc->sc_pct, sc->sc_ih); 420 sc->sc_ih = NULL; 421 } 422 423 bus_space_unmap(sc->sc_st, sc->sc_sh, sc->sc_sz); 424 425 powerhook_disestablish(sc->sc_powerhook); 426 shutdownhook_disestablish(sc->sc_sdhook); 427 428 return 0; 429 } 430 431 static int 432 iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, 433 int count) 434 { 435 int error, nsegs; 436 437 ring->count = count; 438 ring->queued = 0; 439 ring->cur = ring->next = 0; 440 441 /* 442 * Allocate and map command ring 443 */ 444 error = bus_dmamap_create(sc->sc_dmat, 445 sizeof (struct iwi_cmd_desc) * count, 1, 446 sizeof (struct iwi_cmd_desc) * count, 0, 447 BUS_DMA_NOWAIT, &ring->desc_map); 448 if (error != 0) { 449 aprint_error("%s: could not create command ring DMA map\n", 450 sc->sc_dev.dv_xname); 451 goto fail; 452 } 453 454 error = bus_dmamem_alloc(sc->sc_dmat, 455 sizeof (struct iwi_cmd_desc) * count, PAGE_SIZE, 0, 456 &sc->cmdq.desc_seg, 1, &nsegs, BUS_DMA_NOWAIT); 457 if (error != 0) { 458 aprint_error("%s: could not allocate command ring DMA memory\n", 459 sc->sc_dev.dv_xname); 460 goto fail; 461 } 462 463 error = bus_dmamem_map(sc->sc_dmat, &sc->cmdq.desc_seg, nsegs, 464 sizeof (struct iwi_cmd_desc) * count, 465 (caddr_t *)&sc->cmdq.desc, BUS_DMA_NOWAIT); 466 if (error != 0) { 467 aprint_error("%s: could not map command ring DMA memory\n", 468 sc->sc_dev.dv_xname); 469 goto fail; 470 } 471 472 error = bus_dmamap_load(sc->sc_dmat, sc->cmdq.desc_map, sc->cmdq.desc, 473 sizeof (struct iwi_cmd_desc) * count, NULL, 474 BUS_DMA_NOWAIT); 475 if (error != 0) { 476 aprint_error("%s: could not load command ring DMA map\n", 477 sc->sc_dev.dv_xname); 478 goto fail; 479 } 480 481 memset(sc->cmdq.desc, 0, 482 sizeof (struct iwi_cmd_desc) * count); 483 484 return 0; 485 486 fail: iwi_free_cmd_ring(sc, ring); 487 return error; 488 } 489 490 static void 491 iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring) 492 { 493 ring->queued = 0; 494 ring->cur = ring->next = 0; 495 } 496 497 static void 498 iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring) 499 { 500 if (ring->desc_map != NULL) { 501 if (ring->desc != NULL) { 502 bus_dmamap_unload(sc->sc_dmat, ring->desc_map); 503 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc, 504 sizeof (struct iwi_cmd_desc) * ring->count); 505 bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1); 506 } 507 bus_dmamap_destroy(sc->sc_dmat, ring->desc_map); 508 } 509 } 510 511 static int 512 iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, 513 int count) 514 { 515 int i, error, nsegs; 516 517 ring->count = count; 518 ring->queued = 0; 519 ring->cur = ring->next = 0; 520 521 /* 522 * Allocate and map Tx ring 523 */ 524 error = bus_dmamap_create(sc->sc_dmat, 525 sizeof (struct iwi_tx_desc) * count, 1, 526 sizeof (struct iwi_tx_desc) * count, 0, BUS_DMA_NOWAIT, 527 &ring->desc_map); 528 if (error != 0) { 529 aprint_error("%s: could not create tx ring DMA map\n", 530 sc->sc_dev.dv_xname); 531 goto fail; 532 } 533 534 error = bus_dmamem_alloc(sc->sc_dmat, 535 sizeof (struct iwi_tx_desc) * count, PAGE_SIZE, 0, 536 &ring->desc_seg, 1, &nsegs, BUS_DMA_NOWAIT); 537 if (error != 0) { 538 aprint_error("%s: could not allocate tx ring DMA memory\n", 539 sc->sc_dev.dv_xname); 540 goto fail; 541 } 542 543 error = bus_dmamem_map(sc->sc_dmat, &ring->desc_seg, nsegs, 544 sizeof (struct iwi_tx_desc) * count, 545 (caddr_t *)&ring->desc, BUS_DMA_NOWAIT); 546 if (error != 0) { 547 aprint_error("%s: could not map tx ring DMA memory\n", 548 sc->sc_dev.dv_xname); 549 goto fail; 550 } 551 552 error = bus_dmamap_load(sc->sc_dmat, ring->desc_map, ring->desc, 553 sizeof (struct iwi_tx_desc) * count, NULL, 554 BUS_DMA_NOWAIT); 555 if (error != 0) { 556 aprint_error("%s: could not load tx ring DMA map\n", 557 sc->sc_dev.dv_xname); 558 goto fail; 559 } 560 561 memset(ring->desc, 0, sizeof (struct iwi_tx_desc) * count); 562 563 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF, 564 M_NOWAIT | M_ZERO); 565 if (ring->data == NULL) { 566 aprint_error("%s: could not allocate soft data\n", 567 sc->sc_dev.dv_xname); 568 error = ENOMEM; 569 goto fail; 570 } 571 572 /* 573 * Allocate Tx buffers DMA maps 574 */ 575 for (i = 0; i < count; i++) { 576 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, IWI_MAX_NSEG, 577 MCLBYTES, 0, BUS_DMA_NOWAIT, &ring->data[i].map); 578 if (error != 0) { 579 aprint_error("%s: could not create tx buf DMA map", 580 sc->sc_dev.dv_xname); 581 goto fail; 582 } 583 } 584 return 0; 585 586 fail: iwi_free_tx_ring(sc, ring); 587 return error; 588 } 589 590 static void 591 iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring) 592 { 593 struct iwi_tx_data *data; 594 int i; 595 596 for (i = 0; i < ring->count; i++) { 597 data = &ring->data[i]; 598 599 if (data->m != NULL) { 600 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 601 MCLBYTES, BUS_DMASYNC_POSTWRITE); 602 bus_dmamap_unload(sc->sc_dmat, data->map); 603 m_freem(data->m); 604 data->m = NULL; 605 } 606 607 if (data->ni != NULL) { 608 ieee80211_free_node(data->ni); 609 data->ni = NULL; 610 } 611 } 612 613 ring->queued = 0; 614 ring->cur = ring->next = 0; 615 } 616 617 static void 618 iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring) 619 { 620 int i; 621 622 if (ring->desc_map != NULL) { 623 if (ring->desc != NULL) { 624 bus_dmamap_unload(sc->sc_dmat, ring->desc_map); 625 bus_dmamem_unmap(sc->sc_dmat, (caddr_t)ring->desc, 626 sizeof (struct iwi_tx_desc) * ring->count); 627 bus_dmamem_free(sc->sc_dmat, &ring->desc_seg, 1); 628 } 629 bus_dmamap_destroy(sc->sc_dmat, ring->desc_map); 630 } 631 632 for (i = 0; i < ring->count; i++) { 633 if (ring->data[i].m != NULL) { 634 bus_dmamap_unload(sc->sc_dmat, ring->data[i].map); 635 m_freem(ring->data[i].m); 636 } 637 bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map); 638 } 639 } 640 641 static int 642 iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, 643 int count) 644 { 645 int i, error; 646 647 ring->count = count; 648 ring->cur = 0; 649 650 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF, 651 M_NOWAIT | M_ZERO); 652 if (ring->data == NULL) { 653 aprint_error("%s: could not allocate soft data\n", 654 sc->sc_dev.dv_xname); 655 error = ENOMEM; 656 goto fail; 657 } 658 659 /* 660 * Allocate and map Rx buffers 661 */ 662 for (i = 0; i < count; i++) { 663 664 error = bus_dmamap_create(sc->sc_dmat, MCLBYTES, 1, MCLBYTES, 665 0, BUS_DMA_NOWAIT, &ring->data[i].map); 666 if (error != 0) { 667 aprint_error("%s: could not create rx buf DMA map", 668 sc->sc_dev.dv_xname); 669 goto fail; 670 } 671 672 MGETHDR(ring->data[i].m, M_DONTWAIT, MT_DATA); 673 if (ring->data[i].m == NULL) { 674 aprint_error("%s: could not allocate rx mbuf\n", 675 sc->sc_dev.dv_xname); 676 error = ENOMEM; 677 goto fail; 678 } 679 680 MCLGET(ring->data[i].m, M_DONTWAIT); 681 if (!(ring->data[i].m->m_flags & M_EXT)) { 682 m_freem(ring->data[i].m); 683 aprint_error("%s: could not allocate rx mbuf cluster\n", 684 sc->sc_dev.dv_xname); 685 error = ENOMEM; 686 goto fail; 687 } 688 689 error = bus_dmamap_load(sc->sc_dmat, ring->data[i].map, 690 mtod(ring->data[i].m, void *), MCLBYTES, NULL, 691 BUS_DMA_NOWAIT); 692 if (error != 0) { 693 aprint_error("%s: could not load rx buffer DMA map\n", 694 sc->sc_dev.dv_xname); 695 goto fail; 696 } 697 } 698 699 return 0; 700 701 fail: iwi_free_rx_ring(sc, ring); 702 return error; 703 } 704 705 static void 706 iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring) 707 { 708 ring->cur = 0; 709 } 710 711 static void 712 iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring) 713 { 714 int i; 715 716 for (i = 0; i < ring->count; i++) { 717 if (ring->data[i].m != NULL) { 718 bus_dmamap_unload(sc->sc_dmat, ring->data[i].map); 719 m_freem(ring->data[i].m); 720 } 721 bus_dmamap_destroy(sc->sc_dmat, ring->data[i].map); 722 } 723 } 724 725 static void 726 iwi_shutdown(void *arg) 727 { 728 struct iwi_softc *sc = (struct iwi_softc *)arg; 729 struct ifnet *ifp = sc->sc_ic.ic_ifp; 730 731 iwi_stop(ifp, 1); 732 } 733 734 static int 735 iwi_suspend(struct iwi_softc *sc) 736 { 737 struct ifnet *ifp = sc->sc_ic.ic_ifp; 738 739 iwi_stop(ifp, 1); 740 741 return 0; 742 } 743 744 static int 745 iwi_resume(struct iwi_softc *sc) 746 { 747 struct ifnet *ifp = sc->sc_ic.ic_ifp; 748 pcireg_t data; 749 750 /* clear device specific PCI configuration register 0x41 */ 751 data = pci_conf_read(sc->sc_pct, sc->sc_pcitag, 0x40); 752 data &= ~0x0000ff00; 753 pci_conf_write(sc->sc_pct, sc->sc_pcitag, 0x40, data); 754 755 if (ifp->if_flags & IFF_UP) { 756 iwi_init(ifp); 757 if (ifp->if_flags & IFF_RUNNING) 758 iwi_start(ifp); 759 } 760 761 return 0; 762 } 763 764 static void 765 iwi_powerhook(int why, void *arg) 766 { 767 struct iwi_softc *sc = arg; 768 int s; 769 770 s = splnet(); 771 switch (why) { 772 case PWR_SUSPEND: 773 case PWR_STANDBY: 774 iwi_suspend(sc); 775 break; 776 case PWR_RESUME: 777 iwi_resume(sc); 778 break; 779 case PWR_SOFTSUSPEND: 780 case PWR_SOFTSTANDBY: 781 case PWR_SOFTRESUME: 782 break; 783 } 784 splx(s); 785 } 786 787 static int 788 iwi_media_change(struct ifnet *ifp) 789 { 790 int error; 791 792 error = ieee80211_media_change(ifp); 793 if (error != ENETRESET) 794 return error; 795 796 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING)) 797 iwi_init(ifp); 798 799 return 0; 800 } 801 802 /* 803 * The firmware automaticly adapt the transmit speed. We report the current 804 * transmit speed here. 805 */ 806 static void 807 iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 808 { 809 struct iwi_softc *sc = ifp->if_softc; 810 struct ieee80211com *ic = &sc->sc_ic; 811 #define N(a) (sizeof (a) / sizeof (a[0])) 812 static const struct { 813 u_int32_t val; 814 int rate; 815 } rates[] = { 816 { IWI_RATE_DS1, 2 }, 817 { IWI_RATE_DS2, 4 }, 818 { IWI_RATE_DS5, 11 }, 819 { IWI_RATE_DS11, 22 }, 820 { IWI_RATE_OFDM6, 12 }, 821 { IWI_RATE_OFDM9, 18 }, 822 { IWI_RATE_OFDM12, 24 }, 823 { IWI_RATE_OFDM18, 36 }, 824 { IWI_RATE_OFDM24, 48 }, 825 { IWI_RATE_OFDM36, 72 }, 826 { IWI_RATE_OFDM48, 96 }, 827 { IWI_RATE_OFDM54, 108 }, 828 }; 829 u_int32_t val; 830 int rate, i; 831 832 imr->ifm_status = IFM_AVALID; 833 imr->ifm_active = IFM_IEEE80211; 834 if (ic->ic_state == IEEE80211_S_RUN) 835 imr->ifm_status |= IFM_ACTIVE; 836 837 /* read current transmission rate from adapter */ 838 val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE); 839 840 /* convert rate to 802.11 rate */ 841 for (i = 0; i < N(rates) && rates[i].val != val; i++); 842 rate = (i < N(rates)) ? rates[i].rate : 0; 843 844 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode); 845 switch (ic->ic_opmode) { 846 case IEEE80211_M_STA: 847 break; 848 849 case IEEE80211_M_IBSS: 850 imr->ifm_active |= IFM_IEEE80211_ADHOC; 851 break; 852 853 case IEEE80211_M_MONITOR: 854 imr->ifm_active |= IFM_IEEE80211_MONITOR; 855 break; 856 857 case IEEE80211_M_AHDEMO: 858 case IEEE80211_M_HOSTAP: 859 /* should not get there */ 860 break; 861 } 862 #undef N 863 } 864 865 static int 866 iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 867 { 868 struct iwi_softc *sc = ic->ic_ifp->if_softc; 869 870 switch (nstate) { 871 case IEEE80211_S_SCAN: 872 if (sc->flags & IWI_FLAG_SCANNING) 873 break; 874 875 ieee80211_node_table_reset(&ic->ic_scan); 876 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN; 877 sc->flags |= IWI_FLAG_SCANNING; 878 iwi_scan(sc); 879 break; 880 881 case IEEE80211_S_AUTH: 882 iwi_auth_and_assoc(sc); 883 break; 884 885 case IEEE80211_S_RUN: 886 if (ic->ic_opmode == IEEE80211_M_IBSS) 887 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 888 else if (ic->ic_opmode == IEEE80211_M_MONITOR) 889 iwi_set_chan(sc, ic->ic_ibss_chan); 890 891 return (*sc->sc_newstate)(ic, nstate, 892 IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 893 894 case IEEE80211_S_ASSOC: 895 break; 896 897 case IEEE80211_S_INIT: 898 sc->flags &= ~IWI_FLAG_SCANNING; 899 return (*sc->sc_newstate)(ic, nstate, arg); 900 } 901 902 ic->ic_state = nstate; 903 return 0; 904 } 905 906 /* 907 * Read 16 bits at address 'addr' from the serial EEPROM. 908 */ 909 static u_int16_t 910 iwi_read_prom_word(struct iwi_softc *sc, u_int8_t addr) 911 { 912 u_int32_t tmp; 913 u_int16_t val; 914 int n; 915 916 /* Clock C once before the first command */ 917 IWI_EEPROM_CTL(sc, 0); 918 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 919 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 920 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 921 922 /* Write start bit (1) */ 923 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D); 924 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C); 925 926 /* Write READ opcode (10) */ 927 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D); 928 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C); 929 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 930 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 931 932 /* Write address A7-A0 */ 933 for (n = 7; n >= 0; n--) { 934 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | 935 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D)); 936 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | 937 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C); 938 } 939 940 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 941 942 /* Read data Q15-Q0 */ 943 val = 0; 944 for (n = 15; n >= 0; n--) { 945 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 946 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 947 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL); 948 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n; 949 } 950 951 IWI_EEPROM_CTL(sc, 0); 952 953 /* Clear Chip Select and clock C */ 954 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 955 IWI_EEPROM_CTL(sc, 0); 956 IWI_EEPROM_CTL(sc, IWI_EEPROM_C); 957 958 return be16toh(val); 959 } 960 961 /* 962 * XXX: Hack to set the current channel to the value advertised in beacons or 963 * probe responses. Only used during AP detection. 964 */ 965 static void 966 iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m) 967 { 968 struct ieee80211_frame *wh; 969 u_int8_t subtype; 970 u_int8_t *frm, *efrm; 971 972 wh = mtod(m, struct ieee80211_frame *); 973 974 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT) 975 return; 976 977 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 978 979 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON && 980 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP) 981 return; 982 983 frm = (u_int8_t *)(wh + 1); 984 efrm = mtod(m, u_int8_t *) + m->m_len; 985 986 frm += 12; /* skip tstamp, bintval and capinfo fields */ 987 while (frm < efrm) { 988 if (*frm == IEEE80211_ELEMID_DSPARMS) 989 #if IEEE80211_CHAN_MAX < 255 990 if (frm[2] <= IEEE80211_CHAN_MAX) 991 #endif 992 ic->ic_bss->ni_chan = &ic->ic_channels[frm[2]]; 993 994 frm += frm[1] + 2; 995 } 996 } 997 998 static void 999 iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i, 1000 struct iwi_frame *frame) 1001 { 1002 struct ieee80211com *ic = &sc->sc_ic; 1003 struct ifnet *ifp = ic->ic_ifp; 1004 struct mbuf *m; 1005 struct ieee80211_frame *wh; 1006 struct ieee80211_node *ni; 1007 int error; 1008 1009 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n", 1010 le16toh(frame->len), frame->chan, frame->rssi_dbm)); 1011 1012 bus_dmamap_sync(sc->sc_dmat, data->map, sizeof (struct iwi_hdr), 1013 sizeof (struct iwi_frame) + le16toh(frame->len), 1014 BUS_DMASYNC_POSTREAD); 1015 1016 if (le16toh(frame->len) < sizeof (struct ieee80211_frame) || 1017 le16toh(frame->len) > MCLBYTES) { 1018 DPRINTF(("%s: bad frame length\n", sc->sc_dev.dv_xname)); 1019 ifp->if_ierrors++; 1020 return; 1021 } 1022 1023 bus_dmamap_unload(sc->sc_dmat, data->map); 1024 1025 /* Finalize mbuf */ 1026 m = data->m; 1027 m->m_pkthdr.rcvif = ifp; 1028 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) + 1029 sizeof (struct iwi_frame) + le16toh(frame->len); 1030 1031 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame)); 1032 1033 if (ic->ic_state == IEEE80211_S_SCAN) 1034 iwi_fix_channel(ic, m); 1035 1036 #if NBPFILTER > 0 1037 if (sc->sc_drvbpf != NULL) { 1038 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap; 1039 1040 tap->wr_flags = 0; 1041 tap->wr_rate = frame->rate; 1042 tap->wr_chan_freq = 1043 htole16(ic->ic_channels[frame->chan].ic_freq); 1044 tap->wr_chan_flags = 1045 htole16(ic->ic_channels[frame->chan].ic_flags); 1046 tap->wr_antsignal = frame->signal; 1047 tap->wr_antenna = frame->antenna; 1048 1049 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 1050 } 1051 #endif 1052 1053 wh = mtod(m, struct ieee80211_frame *); 1054 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 1055 1056 /* Send the frame to the upper layer */ 1057 ieee80211_input(ic, m, ni, frame->rssi_dbm, 0); 1058 1059 /* node is no longer needed */ 1060 ieee80211_free_node(ni); 1061 1062 MGETHDR(data->m, M_DONTWAIT, MT_DATA); 1063 if (data->m == NULL) { 1064 aprint_error("%s: could not allocate rx mbuf\n", 1065 sc->sc_dev.dv_xname); 1066 return; 1067 } 1068 1069 MCLGET(data->m, M_DONTWAIT); 1070 if (!(data->m->m_flags & M_EXT)) { 1071 aprint_error("%s: could not allocate rx mbuf cluster\n", 1072 sc->sc_dev.dv_xname); 1073 m_freem(data->m); 1074 data->m = NULL; 1075 return; 1076 } 1077 1078 error = bus_dmamap_load(sc->sc_dmat, data->map, mtod(data->m, void *), 1079 MCLBYTES, NULL, BUS_DMA_NOWAIT); 1080 if (error != 0) { 1081 aprint_error("%s: could not load rx buf DMA map\n", 1082 sc->sc_dev.dv_xname); 1083 m_freem(data->m); 1084 data->m = NULL; 1085 return; 1086 } 1087 1088 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, data->map->dm_segs[0].ds_addr); 1089 } 1090 1091 static void 1092 iwi_notification_intr(struct iwi_softc *sc, struct iwi_rx_data *buf, 1093 struct iwi_notif *notif) 1094 { 1095 struct ieee80211com *ic = &sc->sc_ic; 1096 struct iwi_notif_scan_channel *chan; 1097 struct iwi_notif_scan_complete *scan; 1098 struct iwi_notif_authentication *auth; 1099 struct iwi_notif_association *assoc; 1100 1101 bus_dmamap_sync(sc->sc_dmat, buf->map, sizeof (struct iwi_hdr), 1102 sizeof (struct iwi_notif) + le16toh(notif->len), 1103 BUS_DMASYNC_POSTREAD); 1104 1105 switch (notif->type) { 1106 case IWI_NOTIF_TYPE_SCAN_CHANNEL: 1107 chan = (struct iwi_notif_scan_channel *)(notif + 1); 1108 1109 DPRINTFN(2, ("Scanning channel (%u)\n", chan->nchan)); 1110 break; 1111 1112 case IWI_NOTIF_TYPE_SCAN_COMPLETE: 1113 scan = (struct iwi_notif_scan_complete *)(notif + 1); 1114 1115 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan, 1116 scan->status)); 1117 1118 /* monitor mode uses scan to set the channel ... */ 1119 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1120 sc->flags &= ~IWI_FLAG_SCANNING; 1121 ieee80211_end_scan(ic); 1122 } else 1123 iwi_set_chan(sc, ic->ic_ibss_chan); 1124 break; 1125 1126 case IWI_NOTIF_TYPE_AUTHENTICATION: 1127 auth = (struct iwi_notif_authentication *)(notif + 1); 1128 1129 DPRINTFN(2, ("Authentication (%u)\n", auth->state)); 1130 1131 switch (auth->state) { 1132 case IWI_AUTHENTICATED: 1133 ieee80211_node_authorize(ic, ic->ic_bss); 1134 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1); 1135 break; 1136 1137 case IWI_DEAUTHENTICATED: 1138 break; 1139 1140 default: 1141 aprint_error("%s: unknown authentication state %u\n", 1142 sc->sc_dev.dv_xname, auth->state); 1143 } 1144 break; 1145 1146 case IWI_NOTIF_TYPE_ASSOCIATION: 1147 assoc = (struct iwi_notif_association *)(notif + 1); 1148 1149 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state, 1150 assoc->status)); 1151 1152 switch (assoc->state) { 1153 case IWI_AUTHENTICATED: 1154 /* re-association, do nothing */ 1155 break; 1156 1157 case IWI_ASSOCIATED: 1158 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1159 break; 1160 1161 case IWI_DEASSOCIATED: 1162 ieee80211_begin_scan(ic, 1); 1163 break; 1164 1165 default: 1166 aprint_error("%s: unknown association state %u\n", 1167 sc->sc_dev.dv_xname, assoc->state); 1168 } 1169 break; 1170 1171 case IWI_NOTIF_TYPE_CALIBRATION: 1172 case IWI_NOTIF_TYPE_BEACON: 1173 case IWI_NOTIF_TYPE_NOISE: 1174 DPRINTFN(5, ("Notification (%u)\n", notif->type)); 1175 break; 1176 1177 default: 1178 aprint_error("%s: unknown notification type %u\n", 1179 sc->sc_dev.dv_xname, notif->type); 1180 } 1181 } 1182 1183 static void 1184 iwi_rx_intr(struct iwi_softc *sc) 1185 { 1186 struct iwi_rx_data *data; 1187 struct iwi_hdr *hdr; 1188 uint32_t hw; 1189 1190 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX); 1191 1192 for (; sc->rxq.cur != hw;) { 1193 data = &sc->rxq.data[sc->rxq.cur]; 1194 1195 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 1196 sizeof (struct iwi_hdr), BUS_DMASYNC_POSTREAD); 1197 1198 hdr = mtod(data->m, struct iwi_hdr *); 1199 1200 switch (hdr->type) { 1201 case IWI_HDR_TYPE_FRAME: 1202 iwi_frame_intr(sc, data, sc->rxq.cur, 1203 (struct iwi_frame *)(hdr + 1)); 1204 break; 1205 1206 case IWI_HDR_TYPE_NOTIF: 1207 iwi_notification_intr(sc, data, 1208 (struct iwi_notif *)(hdr + 1)); 1209 break; 1210 1211 default: 1212 aprint_error("%s: unknown hdr type %u\n", 1213 sc->sc_dev.dv_xname, hdr->type); 1214 } 1215 1216 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur)); 1217 1218 sc->rxq.cur = (sc->rxq.cur + 1) % sc->rxq.count; 1219 } 1220 1221 1222 /* Tell the firmware what we have processed */ 1223 hw = (hw == 0) ? sc->rxq.count - 1 : hw - 1; 1224 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw); 1225 } 1226 1227 static void 1228 iwi_tx_intr(struct iwi_softc *sc) 1229 { 1230 struct ifnet *ifp = &sc->sc_if; 1231 struct iwi_tx_data *data; 1232 u_int32_t hw; 1233 1234 hw = CSR_READ_4(sc, IWI_CSR_TX1_RIDX); 1235 1236 for (; sc->txq.next != hw;) { 1237 data = &sc->txq.data[sc->txq.next]; 1238 1239 bus_dmamap_sync(sc->sc_dmat, data->map, 0, 1240 MCLBYTES, BUS_DMASYNC_POSTWRITE); 1241 bus_dmamap_unload(sc->sc_dmat, data->map); 1242 m_freem(data->m); 1243 data->m = NULL; 1244 ieee80211_free_node(data->ni); 1245 data->ni = NULL; 1246 1247 DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next)); 1248 1249 ifp->if_opackets++; 1250 1251 sc->txq.queued--; 1252 sc->txq.next = (sc->txq.next + 1) % sc->txq.count; 1253 } 1254 1255 sc->sc_tx_timer = 0; 1256 ifp->if_flags &= ~IFF_OACTIVE; 1257 1258 /* Call start() since some buffer descriptors have been released */ 1259 (*ifp->if_start)(ifp); 1260 } 1261 1262 static int 1263 iwi_intr(void *arg) 1264 { 1265 struct iwi_softc *sc = arg; 1266 u_int32_t r; 1267 1268 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) 1269 return 0; 1270 1271 /* Disable interrupts */ 1272 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0); 1273 1274 if (r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)) { 1275 aprint_error("%s: fatal error\n", sc->sc_dev.dv_xname); 1276 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP; 1277 iwi_stop(&sc->sc_if, 1); 1278 } 1279 1280 if (r & IWI_INTR_FW_INITED) { 1281 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR))) 1282 wakeup(sc); 1283 } 1284 1285 if (r & IWI_INTR_RADIO_OFF) { 1286 DPRINTF(("radio transmitter off\n")); 1287 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP; 1288 iwi_stop(&sc->sc_if, 1); 1289 } 1290 1291 if (r & IWI_INTR_RX_DONE) 1292 iwi_rx_intr(sc); 1293 1294 if (r & IWI_INTR_CMD_DONE) 1295 wakeup(sc); 1296 1297 if (r & IWI_INTR_TX1_DONE) 1298 iwi_tx_intr(sc); 1299 1300 /* Acknowledge interrupts */ 1301 CSR_WRITE_4(sc, IWI_CSR_INTR, r); 1302 1303 /* Re-enable interrupts */ 1304 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK); 1305 1306 return 1; 1307 } 1308 1309 static int 1310 iwi_cmd(struct iwi_softc *sc, u_int8_t type, void *data, u_int8_t len, 1311 int async) 1312 { 1313 struct iwi_cmd_desc *desc; 1314 1315 desc = &sc->cmdq.desc[sc->cmdq.cur]; 1316 1317 desc->hdr.type = IWI_HDR_TYPE_COMMAND; 1318 desc->hdr.flags = IWI_HDR_FLAG_IRQ; 1319 desc->type = type; 1320 desc->len = len; 1321 memcpy(desc->data, data, len); 1322 1323 bus_dmamap_sync(sc->sc_dmat, sc->cmdq.desc_map, 1324 sc->cmdq.cur * sizeof (struct iwi_cmd_desc), 1325 sizeof (struct iwi_cmd_desc), BUS_DMASYNC_PREWRITE); 1326 1327 DPRINTFN(2, ("sending command type=%u len=%u\n", 1328 type, len)); 1329 1330 sc->cmdq.cur = (sc->cmdq.cur + 1) % sc->cmdq.count; 1331 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur); 1332 1333 return async ? 0 : tsleep(sc, 0, "iwicmd", hz); 1334 } 1335 1336 static int 1337 iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni) 1338 { 1339 struct iwi_softc *sc = ifp->if_softc; 1340 struct ieee80211com *ic = &sc->sc_ic; 1341 struct ieee80211_frame wh; 1342 struct ieee80211_key *k; 1343 struct iwi_tx_data *data; 1344 struct iwi_tx_desc *desc; 1345 struct mbuf *mnew; 1346 int error, i; 1347 1348 (void)memcpy(&wh, mtod(m0, struct ieee80211_frame *), sizeof(wh)); 1349 if (wh.i_fc[1] & IEEE80211_FC1_WEP) { 1350 k = ieee80211_crypto_encap(ic, ni, m0); 1351 if (k == NULL) { 1352 m_freem(m0); 1353 return ENOBUFS; 1354 } 1355 } 1356 1357 #if NBPFILTER > 0 1358 if (sc->sc_drvbpf != NULL) { 1359 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap; 1360 1361 tap->wt_flags = 0; 1362 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq); 1363 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags); 1364 1365 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1366 } 1367 #endif 1368 1369 data = &sc->txq.data[sc->txq.cur]; 1370 desc = &sc->txq.desc[sc->txq.cur]; 1371 1372 /* trim IEEE802.11 header */ 1373 m_adj(m0, sizeof (struct ieee80211_frame)); 1374 1375 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, BUS_DMA_NOWAIT); 1376 if (error != 0 && error != EFBIG) { 1377 aprint_error("%s: could not map mbuf (error %d)\n", 1378 sc->sc_dev.dv_xname, error); 1379 m_freem(m0); 1380 return error; 1381 } 1382 if (error != 0) { 1383 /* too many fragments, linearize */ 1384 1385 MGETHDR(mnew, M_DONTWAIT, MT_DATA); 1386 if (mnew == NULL) { 1387 m_freem(m0); 1388 return ENOMEM; 1389 } 1390 1391 M_COPY_PKTHDR(mnew, m0); 1392 MCLGET(mnew, M_DONTWAIT); 1393 if (!(mnew->m_flags & M_EXT)) { 1394 m_freem(m0); 1395 m_freem(mnew); 1396 return ENOMEM; 1397 } 1398 1399 m_copydata(m0, 0, m0->m_pkthdr.len, mtod(mnew, caddr_t)); 1400 m_freem(m0); 1401 mnew->m_len = mnew->m_pkthdr.len; 1402 m0 = mnew; 1403 1404 error = bus_dmamap_load_mbuf(sc->sc_dmat, data->map, m0, 1405 BUS_DMA_NOWAIT); 1406 if (error != 0) { 1407 aprint_error("%s: could not map mbuf (error %d)\n", 1408 sc->sc_dev.dv_xname, error); 1409 m_freem(m0); 1410 return error; 1411 } 1412 } 1413 1414 data->m = m0; 1415 data->ni = ni; 1416 1417 desc->hdr.type = IWI_HDR_TYPE_DATA; 1418 desc->hdr.flags = IWI_HDR_FLAG_IRQ; 1419 desc->cmd = IWI_DATA_CMD_TX; 1420 desc->len = htole16(m0->m_pkthdr.len); 1421 (void)memcpy(&desc->wh, &wh, sizeof (struct ieee80211_frame)); 1422 desc->flags = 0; 1423 if (!IEEE80211_IS_MULTICAST(wh.i_addr1)) 1424 desc->flags |= IWI_DATA_FLAG_NEED_ACK; 1425 1426 #if 0 1427 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 1428 wh.i_fc[1] |= IEEE80211_FC1_WEP; 1429 desc->wep_txkey = ic->ic_crypto.cs_def_txkey; 1430 } else 1431 #endif 1432 desc->flags |= IWI_DATA_FLAG_NO_WEP; 1433 1434 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1435 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE; 1436 1437 desc->nseg = htole32(data->map->dm_nsegs); 1438 for (i = 0; i < data->map->dm_nsegs; i++) { 1439 desc->seg_addr[i] = htole32(data->map->dm_segs[i].ds_addr); 1440 desc->seg_len[i] = htole32(data->map->dm_segs[i].ds_len); 1441 } 1442 1443 bus_dmamap_sync(sc->sc_dmat, sc->txq.desc_map, 1444 sc->txq.cur * sizeof (struct iwi_tx_desc), 1445 sizeof (struct iwi_tx_desc), BUS_DMASYNC_PREWRITE); 1446 1447 bus_dmamap_sync(sc->sc_dmat, data->map, 0, MCLBYTES, 1448 BUS_DMASYNC_PREWRITE); 1449 1450 DPRINTFN(5, ("sending data frame len=%u nseg=%u\n", 1451 desc->len, desc->nseg)); 1452 1453 /* Inform firmware about this new packet */ 1454 sc->txq.queued++; 1455 sc->txq.cur = (sc->txq.cur + 1) % sc->txq.count; 1456 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq.cur); 1457 1458 return 0; 1459 } 1460 1461 static void 1462 iwi_start(struct ifnet *ifp) 1463 { 1464 struct iwi_softc *sc = ifp->if_softc; 1465 struct ieee80211com *ic = &sc->sc_ic; 1466 struct mbuf *m0; 1467 struct ether_header *eh; 1468 struct ieee80211_node *ni; 1469 1470 if (ic->ic_state != IEEE80211_S_RUN) 1471 return; 1472 1473 for (;;) { 1474 IF_DEQUEUE(&ifp->if_snd, m0); 1475 if (m0 == NULL) 1476 break; 1477 1478 if (sc->txq.queued >= sc->txq.count - 4) { 1479 IF_PREPEND(&ifp->if_snd, m0); 1480 ifp->if_flags |= IFF_OACTIVE; 1481 break; 1482 } 1483 1484 if (m0->m_len < sizeof (struct ether_header) && 1485 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL) 1486 continue; 1487 1488 #if NBPFILTER > 0 1489 if (ifp->if_bpf != NULL) 1490 bpf_mtap(ifp->if_bpf, m0); 1491 #endif 1492 1493 eh = mtod(m0, struct ether_header *); 1494 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 1495 if (ni == NULL) { 1496 m_freem(m0); 1497 continue; 1498 } 1499 1500 #if NBPFILTER > 0 1501 if (ic->ic_rawbpf != NULL) 1502 bpf_mtap(ic->ic_rawbpf, m0); 1503 #endif 1504 m0 = ieee80211_encap(ic, m0, ni); 1505 if (m0 == NULL) { 1506 ieee80211_free_node(ni); 1507 continue; 1508 } 1509 1510 if (iwi_tx_start(ifp, m0, ni) != 0) { 1511 ieee80211_free_node(ni); 1512 ifp->if_oerrors++; 1513 break; 1514 } 1515 1516 /* start watchdog timer */ 1517 sc->sc_tx_timer = 5; 1518 ifp->if_timer = 1; 1519 } 1520 } 1521 1522 static void 1523 iwi_watchdog(struct ifnet *ifp) 1524 { 1525 struct iwi_softc *sc = ifp->if_softc; 1526 1527 ifp->if_timer = 0; 1528 1529 if (sc->sc_tx_timer > 0) { 1530 if (--sc->sc_tx_timer == 0) { 1531 aprint_error("%s: device timeout\n", 1532 sc->sc_dev.dv_xname); 1533 ifp->if_oerrors++; 1534 ifp->if_flags &= ~IFF_UP; 1535 iwi_stop(ifp, 1); 1536 return; 1537 } 1538 ifp->if_timer = 1; 1539 } 1540 1541 ieee80211_watchdog(&sc->sc_ic); 1542 } 1543 1544 static int 1545 iwi_get_table0(struct iwi_softc *sc, u_int32_t *tbl) 1546 { 1547 u_int32_t size, buf[128]; 1548 1549 if (!(sc->flags & IWI_FLAG_FW_INITED)) { 1550 memset(buf, 0, sizeof buf); 1551 return copyout(buf, tbl, sizeof buf); 1552 } 1553 1554 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1); 1555 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size); 1556 1557 return copyout(buf, tbl, sizeof buf); 1558 } 1559 1560 static int 1561 iwi_get_radio(struct iwi_softc *sc, int *ret) 1562 { 1563 int val; 1564 1565 val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0; 1566 return copyout(&val, ret, sizeof val); 1567 } 1568 1569 static int 1570 iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1571 { 1572 struct iwi_softc *sc = ifp->if_softc; 1573 struct ifreq *ifr; 1574 int s, error = 0; 1575 1576 s = splnet(); 1577 1578 switch (cmd) { 1579 case SIOCSIFFLAGS: 1580 if (ifp->if_flags & IFF_UP) { 1581 if (!(ifp->if_flags & IFF_RUNNING)) 1582 iwi_init(ifp); 1583 } else { 1584 if (ifp->if_flags & IFF_RUNNING) 1585 iwi_stop(ifp, 1); 1586 } 1587 break; 1588 1589 case SIOCGTABLE0: 1590 ifr = (struct ifreq *)data; 1591 error = iwi_get_table0(sc, (u_int32_t *)ifr->ifr_data); 1592 break; 1593 1594 case SIOCGRADIO: 1595 ifr = (struct ifreq *)data; 1596 error = iwi_get_radio(sc, (int *)ifr->ifr_data); 1597 break; 1598 1599 case SIOCSLOADFW: 1600 /* only super-user can do that! */ 1601 if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0) 1602 break; 1603 1604 ifr = (struct ifreq *)data; 1605 error = iwi_cache_firmware(sc, ifr->ifr_data); 1606 break; 1607 1608 case SIOCSKILLFW: 1609 /* only super-user can do that! */ 1610 if ((error = suser(curproc->p_ucred, &curproc->p_acflag)) != 0) 1611 break; 1612 1613 ifp->if_flags &= ~IFF_UP; 1614 iwi_stop(ifp, 1); 1615 iwi_free_firmware(sc); 1616 break; 1617 1618 default: 1619 error = ieee80211_ioctl(&sc->sc_ic, cmd, data); 1620 } 1621 1622 if (error == ENETRESET && cmd != SIOCADDMULTI) { 1623 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == 1624 (IFF_UP | IFF_RUNNING)) 1625 iwi_init(ifp); 1626 error = 0; 1627 } 1628 1629 splx(s); 1630 return error; 1631 } 1632 1633 static void 1634 iwi_stop_master(struct iwi_softc *sc) 1635 { 1636 int ntries; 1637 1638 /* Disable interrupts */ 1639 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0); 1640 1641 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER); 1642 for (ntries = 0; ntries < 5; ntries++) { 1643 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED) 1644 break; 1645 DELAY(10); 1646 } 1647 if (ntries == 5) 1648 aprint_error("%s: timeout waiting for master\n", 1649 sc->sc_dev.dv_xname); 1650 1651 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) | 1652 IWI_RST_PRINCETON_RESET); 1653 1654 sc->flags &= ~IWI_FLAG_FW_INITED; 1655 } 1656 1657 static int 1658 iwi_reset(struct iwi_softc *sc) 1659 { 1660 int i, ntries; 1661 1662 iwi_stop_master(sc); 1663 1664 /* Move adapter to D0 state */ 1665 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) | 1666 IWI_CTL_INIT); 1667 1668 /* Initialize Phase-Locked Level (PLL) */ 1669 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST); 1670 1671 /* Wait for clock stabilization */ 1672 for (ntries = 0; ntries < 1000; ntries++) { 1673 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY) 1674 break; 1675 DELAY(200); 1676 } 1677 if (ntries == 1000) 1678 return EIO; 1679 1680 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) | 1681 IWI_RST_SW_RESET); 1682 1683 DELAY(10); 1684 1685 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) | 1686 IWI_CTL_INIT); 1687 1688 /* Clear NIC memory */ 1689 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0); 1690 for (i = 0; i < 0xc000; i++) 1691 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0); 1692 1693 return 0; 1694 } 1695 1696 static int 1697 iwi_load_ucode(struct iwi_softc *sc, void *uc, int size) 1698 { 1699 u_int16_t *w; 1700 int ntries, i; 1701 1702 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) | 1703 IWI_RST_STOP_MASTER); 1704 for (ntries = 0; ntries < 5; ntries++) { 1705 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED) 1706 break; 1707 DELAY(10); 1708 } 1709 if (ntries == 5) { 1710 aprint_error("%s: timeout waiting for master\n", 1711 sc->sc_dev.dv_xname); 1712 return EIO; 1713 } 1714 1715 MEM_WRITE_4(sc, 0x3000e0, 0x80000000); 1716 DELAY(5000); 1717 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) & 1718 ~IWI_RST_PRINCETON_RESET); 1719 DELAY(5000); 1720 MEM_WRITE_4(sc, 0x3000e0, 0); 1721 DELAY(1000); 1722 MEM_WRITE_4(sc, 0x300004, 1); 1723 DELAY(1000); 1724 MEM_WRITE_4(sc, 0x300004, 0); 1725 DELAY(1000); 1726 MEM_WRITE_1(sc, 0x200000, 0x00); 1727 MEM_WRITE_1(sc, 0x200000, 0x40); 1728 DELAY(1000); 1729 1730 /* Adapter is buggy, we must set the address for each word */ 1731 for (w = uc; size > 0; w++, size -= 2) 1732 MEM_WRITE_2(sc, 0x200010, *w); 1733 1734 MEM_WRITE_1(sc, 0x200000, 0x00); 1735 MEM_WRITE_1(sc, 0x200000, 0x80); 1736 1737 /* Wait until we get a response in the uc queue */ 1738 for (ntries = 0; ntries < 100; ntries++) { 1739 if (MEM_READ_1(sc, 0x200000) & 1) 1740 break; 1741 DELAY(100); 1742 } 1743 if (ntries == 100) { 1744 aprint_error("%s: timeout waiting for ucode to initialize\n", 1745 sc->sc_dev.dv_xname); 1746 return EIO; 1747 } 1748 1749 /* Empty the uc queue or the firmware will not initialize properly */ 1750 for (i = 0; i < 7; i++) 1751 MEM_READ_4(sc, 0x200004); 1752 1753 MEM_WRITE_1(sc, 0x200000, 0x00); 1754 1755 return 0; 1756 } 1757 1758 /* macro to handle unaligned little endian data in firmware image */ 1759 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24) 1760 static int 1761 iwi_load_firmware(struct iwi_softc *sc, void *fw, int size) 1762 { 1763 bus_dmamap_t map; 1764 bus_dma_segment_t seg; 1765 caddr_t virtaddr; 1766 u_char *p, *end; 1767 u_int32_t sentinel, ctl, src, dst, sum, len, mlen; 1768 int ntries, nsegs, error; 1769 1770 /* Allocate DMA memory for storing firmware image */ 1771 error = bus_dmamap_create(sc->sc_dmat, size, 1, size, 0, 1772 BUS_DMA_NOWAIT, &map); 1773 if (error != 0) { 1774 aprint_error("%s: could not create firmware DMA map\n", 1775 sc->sc_dev.dv_xname); 1776 goto fail1; 1777 } 1778 1779 /* 1780 * We cannot map fw directly because of some hardware constraints on 1781 * the mapping address. 1782 */ 1783 error = bus_dmamem_alloc(sc->sc_dmat, size, PAGE_SIZE, 0, &seg, 1, 1784 &nsegs, BUS_DMA_NOWAIT); 1785 if (error != 0) { 1786 aprint_error("%s: could not allocate firmware DMA memory\n", 1787 sc->sc_dev.dv_xname); 1788 goto fail2; 1789 } 1790 1791 error = bus_dmamem_map(sc->sc_dmat, &seg, nsegs, size, &virtaddr, 1792 BUS_DMA_NOWAIT); 1793 if (error != 0) { 1794 aprint_error("%s: could not load firmware DMA map\n", 1795 sc->sc_dev.dv_xname); 1796 goto fail3; 1797 } 1798 1799 error = bus_dmamap_load(sc->sc_dmat, map, virtaddr, size, NULL, 1800 BUS_DMA_NOWAIT); 1801 if (error != 0) { 1802 aprint_error("%s: could not load fw dma map\n", 1803 sc->sc_dev.dv_xname); 1804 goto fail4; 1805 } 1806 1807 /* Copy firmware image to DMA memory */ 1808 memcpy(virtaddr, fw, size); 1809 1810 /* Make sure the adapter will get up-to-date values */ 1811 bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_PREWRITE); 1812 1813 /* Tell the adapter where the command blocks are stored */ 1814 MEM_WRITE_4(sc, 0x3000a0, 0x27000); 1815 1816 /* 1817 * Store command blocks into adapter's internal memory using register 1818 * indirections. The adapter will read the firmware image through DMA 1819 * using information stored in command blocks. 1820 */ 1821 src = map->dm_segs[0].ds_addr; 1822 p = virtaddr; 1823 end = p + size; 1824 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000); 1825 1826 while (p < end) { 1827 dst = GETLE32(p); p += 4; src += 4; 1828 len = GETLE32(p); p += 4; src += 4; 1829 p += len; 1830 1831 while (len > 0) { 1832 mlen = min(len, IWI_CB_MAXDATALEN); 1833 1834 ctl = IWI_CB_DEFAULT_CTL | mlen; 1835 sum = ctl ^ src ^ dst; 1836 1837 /* Write a command block */ 1838 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl); 1839 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src); 1840 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst); 1841 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum); 1842 1843 src += mlen; 1844 dst += mlen; 1845 len -= mlen; 1846 } 1847 } 1848 1849 /* Write a fictive final command block (sentinel) */ 1850 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR); 1851 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0); 1852 1853 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) & 1854 ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER)); 1855 1856 /* Tell the adapter to start processing command blocks */ 1857 MEM_WRITE_4(sc, 0x3000a4, 0x540100); 1858 1859 /* Wait until the adapter has processed all command blocks */ 1860 for (ntries = 0; ntries < 400; ntries++) { 1861 if (MEM_READ_4(sc, 0x3000d0) >= sentinel) 1862 break; 1863 DELAY(100); 1864 } 1865 if (ntries == 400) { 1866 aprint_error("%s: timeout processing cb\n", 1867 sc->sc_dev.dv_xname); 1868 error = EIO; 1869 goto fail5; 1870 } 1871 1872 /* We're done with command blocks processing */ 1873 MEM_WRITE_4(sc, 0x3000a4, 0x540c00); 1874 1875 /* Allow interrupts so we know when the firmware is inited */ 1876 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK); 1877 1878 /* Tell the adapter to initialize the firmware */ 1879 CSR_WRITE_4(sc, IWI_CSR_RST, 0); 1880 CSR_WRITE_4(sc, IWI_CSR_CTL, CSR_READ_4(sc, IWI_CSR_CTL) | 1881 IWI_CTL_ALLOW_STANDBY); 1882 1883 /* Wait at most one second for firmware initialization to complete */ 1884 if ((error = tsleep(sc, 0, "iwiinit", hz)) != 0) { 1885 aprint_error("%s: timeout waiting for firmware initialization " 1886 "to complete\n", sc->sc_dev.dv_xname); 1887 goto fail5; 1888 } 1889 1890 fail5: bus_dmamap_sync(sc->sc_dmat, map, 0, size, BUS_DMASYNC_POSTWRITE); 1891 bus_dmamap_unload(sc->sc_dmat, map); 1892 fail4: bus_dmamem_unmap(sc->sc_dmat, virtaddr, size); 1893 fail3: bus_dmamem_free(sc->sc_dmat, &seg, 1); 1894 fail2: bus_dmamap_destroy(sc->sc_dmat, map); 1895 1896 fail1: return error; 1897 } 1898 1899 /* 1900 * Store firmware into kernel memory so we can download it when we need to, 1901 * e.g when the adapter wakes up from suspend mode. 1902 */ 1903 static int 1904 iwi_cache_firmware(struct iwi_softc *sc, void *data) 1905 { 1906 struct iwi_firmware *kfw = &sc->fw; 1907 struct iwi_firmware ufw; 1908 int error; 1909 1910 iwi_free_firmware(sc); 1911 1912 if ((error = copyin(data, &ufw, sizeof ufw)) != 0) 1913 goto fail1; 1914 1915 kfw->boot_size = ufw.boot_size; 1916 kfw->ucode_size = ufw.ucode_size; 1917 kfw->main_size = ufw.main_size; 1918 1919 kfw->boot = malloc(kfw->boot_size, M_DEVBUF, M_NOWAIT); 1920 if (kfw->boot == NULL) { 1921 error = ENOMEM; 1922 goto fail1; 1923 } 1924 1925 kfw->ucode = malloc(kfw->ucode_size, M_DEVBUF, M_NOWAIT); 1926 if (kfw->ucode == NULL) { 1927 error = ENOMEM; 1928 goto fail2; 1929 } 1930 1931 kfw->main = malloc(kfw->main_size, M_DEVBUF, M_NOWAIT); 1932 if (kfw->main == NULL) { 1933 error = ENOMEM; 1934 goto fail3; 1935 } 1936 1937 if ((error = copyin(ufw.boot, kfw->boot, kfw->boot_size)) != 0) 1938 goto fail4; 1939 1940 if ((error = copyin(ufw.ucode, kfw->ucode, kfw->ucode_size)) != 0) 1941 goto fail4; 1942 1943 if ((error = copyin(ufw.main, kfw->main, kfw->main_size)) != 0) 1944 goto fail4; 1945 1946 DPRINTF(("Firmware cached: boot %u, ucode %u, main %u\n", 1947 kfw->boot_size, kfw->ucode_size, kfw->main_size)); 1948 1949 sc->flags |= IWI_FLAG_FW_CACHED; 1950 1951 return 0; 1952 1953 fail4: free(kfw->boot, M_DEVBUF); 1954 fail3: free(kfw->ucode, M_DEVBUF); 1955 fail2: free(kfw->main, M_DEVBUF); 1956 fail1: 1957 return error; 1958 } 1959 1960 static void 1961 iwi_free_firmware(struct iwi_softc *sc) 1962 { 1963 if (!(sc->flags & IWI_FLAG_FW_CACHED)) 1964 return; 1965 1966 free(sc->fw.boot, M_DEVBUF); 1967 free(sc->fw.ucode, M_DEVBUF); 1968 free(sc->fw.main, M_DEVBUF); 1969 1970 sc->flags &= ~IWI_FLAG_FW_CACHED; 1971 } 1972 1973 static int 1974 iwi_config(struct iwi_softc *sc) 1975 { 1976 struct ieee80211com *ic = &sc->sc_ic; 1977 struct ifnet *ifp = &sc->sc_if; 1978 struct iwi_configuration config; 1979 struct iwi_rateset rs; 1980 struct iwi_txpower power; 1981 struct ieee80211_key *wk; 1982 struct iwi_wep_key wepkey; 1983 u_int32_t data; 1984 int error, i; 1985 1986 IEEE80211_ADDR_COPY(ic->ic_myaddr, LLADDR(ifp->if_sadl)); 1987 DPRINTF(("Setting MAC address to %s\n", ether_sprintf(ic->ic_myaddr))); 1988 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr, 1989 IEEE80211_ADDR_LEN, 0); 1990 if (error != 0) 1991 return error; 1992 1993 memset(&config, 0, sizeof config); 1994 config.bluetooth_coexistence = sc->bluetooth; 1995 config.antenna = sc->antenna; 1996 config.multicast_enabled = 1; 1997 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0; 1998 config.disable_unicast_decryption = 1; 1999 config.disable_multicast_decryption = 1; 2000 DPRINTF(("Configuring adapter\n")); 2001 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config, sizeof config, 2002 0); 2003 if (error != 0) 2004 return error; 2005 2006 data = htole32(IWI_POWER_MODE_CAM); 2007 DPRINTF(("Setting power mode to %u\n", le32toh(data))); 2008 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0); 2009 if (error != 0) 2010 return error; 2011 2012 data = htole32(ic->ic_rtsthreshold); 2013 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data))); 2014 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0); 2015 if (error != 0) 2016 return error; 2017 2018 data = htole32(ic->ic_fragthreshold); 2019 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data))); 2020 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0); 2021 if (error != 0) 2022 return error; 2023 2024 if (ic->ic_opmode == IEEE80211_M_IBSS) { 2025 power.mode = IWI_MODE_11B; 2026 power.nchan = 11; 2027 for (i = 0; i < 11; i++) { 2028 power.chan[i].chan = i + 1; 2029 power.chan[i].power = IWI_TXPOWER_MAX; 2030 } 2031 DPRINTF(("Setting .11b channels tx power\n")); 2032 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 2033 0); 2034 if (error != 0) 2035 return error; 2036 2037 power.mode = IWI_MODE_11G; 2038 DPRINTF(("Setting .11g channels tx power\n")); 2039 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 2040 0); 2041 if (error != 0) 2042 return error; 2043 } 2044 2045 rs.mode = IWI_MODE_11G; 2046 rs.type = IWI_RATESET_TYPE_SUPPORTED; 2047 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates; 2048 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates, 2049 rs.nrates); 2050 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates)); 2051 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0); 2052 if (error != 0) 2053 return error; 2054 2055 rs.mode = IWI_MODE_11A; 2056 rs.type = IWI_RATESET_TYPE_SUPPORTED; 2057 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates; 2058 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates, 2059 rs.nrates); 2060 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates)); 2061 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0); 2062 if (error != 0) 2063 return error; 2064 2065 data = htole32(arc4random()); 2066 DPRINTF(("Setting initialization vector to %u\n", le32toh(data))); 2067 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0); 2068 if (error != 0) 2069 return error; 2070 2071 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2072 wk = &ic->ic_crypto.cs_nw_keys[i]; 2073 2074 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY; 2075 wepkey.idx = i; 2076 wepkey.len = wk->wk_keylen; 2077 memset(wepkey.key, 0, sizeof wepkey.key); 2078 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen); 2079 DPRINTF(("Setting wep key index %u len %u\n", 2080 wepkey.idx, wepkey.len)); 2081 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey, 2082 sizeof wepkey, 0); 2083 if (error != 0) 2084 return error; 2085 } 2086 2087 /* Enable adapter */ 2088 DPRINTF(("Enabling adapter\n")); 2089 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0); 2090 } 2091 2092 static int 2093 iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan) 2094 { 2095 struct ieee80211com *ic = &sc->sc_ic; 2096 struct iwi_scan scan; 2097 2098 (void)memset(&scan, 0, sizeof scan); 2099 scan.type = IWI_SCAN_TYPE_PASSIVE; 2100 scan.dwelltime = htole16(2000); 2101 scan.channels[0] = 1 | (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : 2102 IWI_CHAN_2GHZ); 2103 scan.channels[1] = ieee80211_chan2ieee(ic, chan); 2104 2105 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan))); 2106 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1); 2107 } 2108 2109 static int 2110 iwi_scan(struct iwi_softc *sc) 2111 { 2112 struct ieee80211com *ic = &sc->sc_ic; 2113 struct iwi_scan scan; 2114 u_int8_t *p; 2115 int i, count; 2116 2117 (void)memset(&scan, 0, sizeof scan); 2118 scan.type = IWI_SCAN_TYPE_BROADCAST; 2119 scan.dwelltime = htole16(sc->dwelltime); 2120 2121 p = scan.channels; 2122 count = 0; 2123 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 2124 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) && 2125 isset(ic->ic_chan_active, i)) { 2126 *++p = i; 2127 count++; 2128 } 2129 } 2130 *(p - count) = IWI_CHAN_5GHZ | count; 2131 2132 count = 0; 2133 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 2134 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) && 2135 isset(ic->ic_chan_active, i)) { 2136 *++p = i; 2137 count++; 2138 } 2139 } 2140 *(p - count) = IWI_CHAN_2GHZ | count; 2141 2142 DPRINTF(("Start scanning\n")); 2143 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1); 2144 } 2145 2146 static int 2147 iwi_auth_and_assoc(struct iwi_softc *sc) 2148 { 2149 struct ieee80211com *ic = &sc->sc_ic; 2150 struct ieee80211_node *ni = ic->ic_bss; 2151 struct ifnet *ifp = &sc->sc_if; 2152 struct iwi_configuration config; 2153 struct iwi_associate assoc; 2154 struct iwi_rateset rs; 2155 u_int16_t capinfo; 2156 u_int32_t data; 2157 int error; 2158 2159 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) { 2160 memset(&config, 0, sizeof config); 2161 config.bluetooth_coexistence = sc->bluetooth; 2162 config.antenna = sc->antenna; 2163 config.multicast_enabled = 1; 2164 config.use_protection = 1; 2165 config.answer_pbreq = 2166 (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0; 2167 config.disable_unicast_decryption = 1; 2168 config.disable_multicast_decryption = 1; 2169 DPRINTF(("Configuring adapter\n")); 2170 error = iwi_cmd(sc, IWI_CMD_SET_CONFIGURATION, &config, 2171 sizeof config, 1); 2172 if (error != 0) 2173 return error; 2174 } 2175 2176 #ifdef IWI_DEBUG 2177 if (iwi_debug > 0) { 2178 printf("Setting ESSID to "); 2179 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 2180 printf("\n"); 2181 } 2182 #endif 2183 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1); 2184 if (error != 0) 2185 return error; 2186 2187 /* the rate set has already been "negotiated" */ 2188 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A : 2189 IWI_MODE_11G; 2190 rs.type = IWI_RATESET_TYPE_NEGOTIATED; 2191 rs.nrates = ni->ni_rates.rs_nrates; 2192 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates); 2193 DPRINTF(("Setting negotiated rates (%u)\n", rs.nrates)); 2194 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1); 2195 if (error != 0) 2196 return error; 2197 2198 if (ic->ic_opt_ie != NULL) { 2199 DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len)); 2200 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie, 2201 ic->ic_opt_ie_len, 1); 2202 if (error != 0) 2203 return error; 2204 } 2205 data = htole32(ni->ni_rssi); 2206 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi)); 2207 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1); 2208 if (error != 0) 2209 return error; 2210 2211 memset(&assoc, 0, sizeof assoc); 2212 assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A : 2213 IWI_MODE_11G; 2214 assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 2215 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) 2216 assoc.auth = (ic->ic_crypto.cs_def_txkey << 4) | IWI_AUTH_SHARED; 2217 if (ic->ic_opt_ie != NULL) 2218 assoc.policy |= htole16(IWI_POLICY_OPTIE); 2219 memcpy(assoc.tstamp, ni->ni_tstamp.data, 8); 2220 2221 if (ic->ic_opmode == IEEE80211_M_IBSS) 2222 capinfo = IEEE80211_CAPINFO_IBSS; 2223 else 2224 capinfo = IEEE80211_CAPINFO_ESS; 2225 if (ic->ic_flags & IEEE80211_F_PRIVACY) 2226 capinfo |= IEEE80211_CAPINFO_PRIVACY; 2227 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 2228 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 2229 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 2230 if (ic->ic_flags & IEEE80211_F_SHSLOT) 2231 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 2232 assoc.capinfo = htole16(capinfo); 2233 2234 assoc.lintval = htole16(ic->ic_lintval); 2235 assoc.intval = htole16(ni->ni_intval); 2236 IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid); 2237 if (ic->ic_opmode == IEEE80211_M_IBSS) 2238 IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr); 2239 else 2240 IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid); 2241 DPRINTF(("Trying to associate to %s channel %u auth %u\n", 2242 ether_sprintf(assoc.bssid), assoc.chan, assoc.auth)); 2243 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1); 2244 } 2245 2246 static int 2247 iwi_init(struct ifnet *ifp) 2248 { 2249 struct iwi_softc *sc = ifp->if_softc; 2250 struct ieee80211com *ic = &sc->sc_ic; 2251 struct iwi_firmware *fw = &sc->fw; 2252 int i, error; 2253 2254 /* exit immediately if firmware has not been ioctl'd */ 2255 if (!(sc->flags & IWI_FLAG_FW_CACHED)) { 2256 if (!(sc->flags & IWI_FLAG_FW_WARNED)) 2257 aprint_error("%s: Firmware not loaded\n", 2258 sc->sc_dev.dv_xname); 2259 sc->flags |= IWI_FLAG_FW_WARNED; 2260 ifp->if_flags &= ~IFF_UP; 2261 return EIO; 2262 } 2263 2264 iwi_stop(ifp, 0); 2265 2266 if ((error = iwi_reset(sc)) != 0) { 2267 aprint_error("%s: could not reset adapter\n", 2268 sc->sc_dev.dv_xname); 2269 goto fail; 2270 } 2271 2272 if ((error = iwi_load_firmware(sc, fw->boot, fw->boot_size)) != 0) { 2273 aprint_error("%s: could not load boot firmware\n", 2274 sc->sc_dev.dv_xname); 2275 goto fail; 2276 } 2277 2278 if ((error = iwi_load_ucode(sc, fw->ucode, fw->ucode_size)) != 0) { 2279 aprint_error("%s: could not load microcode\n", 2280 sc->sc_dev.dv_xname); 2281 goto fail; 2282 } 2283 2284 iwi_stop_master(sc); 2285 2286 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.desc_map->dm_segs[0].ds_addr); 2287 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count); 2288 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur); 2289 2290 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq.desc_map->dm_segs[0].ds_addr); 2291 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq.count); 2292 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq.cur); 2293 2294 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq.desc_map->dm_segs[0].ds_addr); 2295 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq.count); 2296 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq.cur); 2297 2298 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq.desc_map->dm_segs[0].ds_addr); 2299 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq.count); 2300 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq.cur); 2301 2302 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq.desc_map->dm_segs[0].ds_addr); 2303 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq.count); 2304 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq.cur); 2305 2306 for (i = 0; i < sc->rxq.count; i++) 2307 CSR_WRITE_4(sc, IWI_CSR_RX_BASE + i * 4, 2308 sc->rxq.data[i].map->dm_segs[0].ds_addr); 2309 2310 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count -1); 2311 2312 if ((error = iwi_load_firmware(sc, fw->main, fw->main_size)) != 0) { 2313 aprint_error("%s: could not load main firmware\n", 2314 sc->sc_dev.dv_xname); 2315 goto fail; 2316 } 2317 2318 sc->flags |= IWI_FLAG_FW_INITED; 2319 2320 if ((error = iwi_config(sc)) != 0) { 2321 aprint_error("%s: device configuration failed\n", 2322 sc->sc_dev.dv_xname); 2323 goto fail; 2324 } 2325 2326 if (ic->ic_opmode == IEEE80211_M_MONITOR) 2327 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 2328 else 2329 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2330 2331 ifp->if_flags &= ~IFF_OACTIVE; 2332 ifp->if_flags |= IFF_RUNNING; 2333 2334 return 0; 2335 2336 fail: ifp->if_flags &= ~IFF_UP; 2337 iwi_stop(ifp, 0); 2338 2339 return error; 2340 } 2341 2342 static void 2343 iwi_stop(struct ifnet *ifp, int disable) 2344 { 2345 struct iwi_softc *sc = ifp->if_softc; 2346 struct ieee80211com *ic = &sc->sc_ic; 2347 2348 iwi_stop_master(sc); 2349 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SW_RESET); 2350 2351 /* reset rings */ 2352 iwi_reset_cmd_ring(sc, &sc->cmdq); 2353 iwi_reset_tx_ring(sc, &sc->txq); 2354 iwi_reset_rx_ring(sc, &sc->rxq); 2355 2356 ifp->if_timer = 0; 2357 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2358 2359 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2360 } 2361