1 /*- 2 * Copyright (c) 2004-2006 3 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved. 4 * Copyright (c) 2006 Sam Leffler, Errno Consulting 5 * Copyright (c) 2007 Andrew Thompson <thompsa@FreeBSD.org> 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 __FBSDID("$FreeBSD$"); 32 33 /*- 34 * Intel(R) PRO/Wireless 2100 MiniPCI driver 35 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm 36 */ 37 38 #include <sys/param.h> 39 #include <sys/sysctl.h> 40 #include <sys/sockio.h> 41 #include <sys/mbuf.h> 42 #include <sys/kernel.h> 43 #include <sys/socket.h> 44 #include <sys/systm.h> 45 #include <sys/malloc.h> 46 #include <sys/queue.h> 47 #include <sys/taskqueue.h> 48 #include <sys/module.h> 49 #include <sys/bus.h> 50 #include <sys/endian.h> 51 #include <sys/linker.h> 52 #include <sys/firmware.h> 53 54 #include <machine/bus.h> 55 #include <machine/resource.h> 56 #include <sys/rman.h> 57 58 #include <dev/pci/pcireg.h> 59 #include <dev/pci/pcivar.h> 60 61 #include <net/bpf.h> 62 #include <net/if.h> 63 #include <net/if_var.h> 64 #include <net/if_arp.h> 65 #include <net/ethernet.h> 66 #include <net/if_dl.h> 67 #include <net/if_media.h> 68 #include <net/if_types.h> 69 70 #include <net80211/ieee80211_var.h> 71 #include <net80211/ieee80211_radiotap.h> 72 73 #include <netinet/in.h> 74 #include <netinet/in_systm.h> 75 #include <netinet/in_var.h> 76 #include <netinet/ip.h> 77 #include <netinet/if_ether.h> 78 79 #include <dev/ipw/if_ipwreg.h> 80 #include <dev/ipw/if_ipwvar.h> 81 82 #define IPW_DEBUG 83 #ifdef IPW_DEBUG 84 #define DPRINTF(x) do { if (ipw_debug > 0) printf x; } while (0) 85 #define DPRINTFN(n, x) do { if (ipw_debug >= (n)) printf x; } while (0) 86 int ipw_debug = 0; 87 SYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level"); 88 #else 89 #define DPRINTF(x) 90 #define DPRINTFN(n, x) 91 #endif 92 93 MODULE_DEPEND(ipw, pci, 1, 1, 1); 94 MODULE_DEPEND(ipw, wlan, 1, 1, 1); 95 MODULE_DEPEND(ipw, firmware, 1, 1, 1); 96 97 struct ipw_ident { 98 uint16_t vendor; 99 uint16_t device; 100 const char *name; 101 }; 102 103 static const struct ipw_ident ipw_ident_table[] = { 104 { 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" }, 105 106 { 0, 0, NULL } 107 }; 108 109 static struct ieee80211vap *ipw_vap_create(struct ieee80211com *, 110 const char [IFNAMSIZ], int, enum ieee80211_opmode, int, 111 const uint8_t [IEEE80211_ADDR_LEN], 112 const uint8_t [IEEE80211_ADDR_LEN]); 113 static void ipw_vap_delete(struct ieee80211vap *); 114 static int ipw_dma_alloc(struct ipw_softc *); 115 static void ipw_release(struct ipw_softc *); 116 static void ipw_media_status(struct ifnet *, struct ifmediareq *); 117 static int ipw_newstate(struct ieee80211vap *, enum ieee80211_state, int); 118 static uint16_t ipw_read_prom_word(struct ipw_softc *, uint8_t); 119 static void ipw_rx_cmd_intr(struct ipw_softc *, struct ipw_soft_buf *); 120 static void ipw_rx_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *); 121 static void ipw_rx_data_intr(struct ipw_softc *, struct ipw_status *, 122 struct ipw_soft_bd *, struct ipw_soft_buf *); 123 static void ipw_rx_intr(struct ipw_softc *); 124 static void ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *); 125 static void ipw_tx_intr(struct ipw_softc *); 126 static void ipw_intr(void *); 127 static void ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int); 128 static const char * ipw_cmdname(int); 129 static int ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t); 130 static int ipw_tx_start(struct ipw_softc *, struct mbuf *, 131 struct ieee80211_node *); 132 static int ipw_raw_xmit(struct ieee80211_node *, struct mbuf *, 133 const struct ieee80211_bpf_params *); 134 static int ipw_transmit(struct ieee80211com *, struct mbuf *); 135 static void ipw_start(struct ipw_softc *); 136 static void ipw_watchdog(void *); 137 static void ipw_parent(struct ieee80211com *); 138 static void ipw_stop_master(struct ipw_softc *); 139 static int ipw_enable(struct ipw_softc *); 140 static int ipw_disable(struct ipw_softc *); 141 static int ipw_reset(struct ipw_softc *); 142 static int ipw_load_ucode(struct ipw_softc *, const char *, int); 143 static int ipw_load_firmware(struct ipw_softc *, const char *, int); 144 static int ipw_config(struct ipw_softc *); 145 static void ipw_assoc(struct ieee80211com *, struct ieee80211vap *); 146 static void ipw_disassoc(struct ieee80211com *, struct ieee80211vap *); 147 static void ipw_init_task(void *, int); 148 static void ipw_init(void *); 149 static void ipw_init_locked(struct ipw_softc *); 150 static void ipw_stop(void *); 151 static void ipw_stop_locked(struct ipw_softc *); 152 static int ipw_sysctl_stats(SYSCTL_HANDLER_ARGS); 153 static int ipw_sysctl_radio(SYSCTL_HANDLER_ARGS); 154 static uint32_t ipw_read_table1(struct ipw_softc *, uint32_t); 155 static void ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t); 156 #if 0 157 static int ipw_read_table2(struct ipw_softc *, uint32_t, void *, 158 uint32_t *); 159 static void ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *, 160 bus_size_t); 161 #endif 162 static void ipw_write_mem_1(struct ipw_softc *, bus_size_t, 163 const uint8_t *, bus_size_t); 164 static int ipw_scan(struct ipw_softc *); 165 static void ipw_scan_start(struct ieee80211com *); 166 static void ipw_scan_end(struct ieee80211com *); 167 static void ipw_set_channel(struct ieee80211com *); 168 static void ipw_scan_curchan(struct ieee80211_scan_state *, 169 unsigned long maxdwell); 170 static void ipw_scan_mindwell(struct ieee80211_scan_state *); 171 172 static int ipw_probe(device_t); 173 static int ipw_attach(device_t); 174 static int ipw_detach(device_t); 175 static int ipw_shutdown(device_t); 176 static int ipw_suspend(device_t); 177 static int ipw_resume(device_t); 178 179 static device_method_t ipw_methods[] = { 180 /* Device interface */ 181 DEVMETHOD(device_probe, ipw_probe), 182 DEVMETHOD(device_attach, ipw_attach), 183 DEVMETHOD(device_detach, ipw_detach), 184 DEVMETHOD(device_shutdown, ipw_shutdown), 185 DEVMETHOD(device_suspend, ipw_suspend), 186 DEVMETHOD(device_resume, ipw_resume), 187 188 DEVMETHOD_END 189 }; 190 191 static driver_t ipw_driver = { 192 "ipw", 193 ipw_methods, 194 sizeof (struct ipw_softc) 195 }; 196 197 static devclass_t ipw_devclass; 198 199 DRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, NULL, NULL); 200 201 MODULE_VERSION(ipw, 1); 202 203 static int 204 ipw_probe(device_t dev) 205 { 206 const struct ipw_ident *ident; 207 208 for (ident = ipw_ident_table; ident->name != NULL; ident++) { 209 if (pci_get_vendor(dev) == ident->vendor && 210 pci_get_device(dev) == ident->device) { 211 device_set_desc(dev, ident->name); 212 return (BUS_PROBE_DEFAULT); 213 } 214 } 215 return ENXIO; 216 } 217 218 /* Base Address Register */ 219 static int 220 ipw_attach(device_t dev) 221 { 222 struct ipw_softc *sc = device_get_softc(dev); 223 struct ieee80211com *ic = &sc->sc_ic; 224 struct ieee80211_channel *c; 225 uint16_t val; 226 int error, i; 227 228 sc->sc_dev = dev; 229 230 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 231 MTX_DEF | MTX_RECURSE); 232 mbufq_init(&sc->sc_snd, ifqmaxlen); 233 TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc); 234 callout_init_mtx(&sc->sc_wdtimer, &sc->sc_mtx, 0); 235 236 pci_write_config(dev, 0x41, 0, 1); 237 238 /* enable bus-mastering */ 239 pci_enable_busmaster(dev); 240 241 i = PCIR_BAR(0); 242 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &i, RF_ACTIVE); 243 if (sc->mem == NULL) { 244 device_printf(dev, "could not allocate memory resource\n"); 245 goto fail; 246 } 247 248 sc->sc_st = rman_get_bustag(sc->mem); 249 sc->sc_sh = rman_get_bushandle(sc->mem); 250 251 i = 0; 252 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &i, 253 RF_ACTIVE | RF_SHAREABLE); 254 if (sc->irq == NULL) { 255 device_printf(dev, "could not allocate interrupt resource\n"); 256 goto fail1; 257 } 258 259 if (ipw_reset(sc) != 0) { 260 device_printf(dev, "could not reset adapter\n"); 261 goto fail2; 262 } 263 264 if (ipw_dma_alloc(sc) != 0) { 265 device_printf(dev, "could not allocate DMA resources\n"); 266 goto fail2; 267 } 268 269 ic->ic_softc = sc; 270 ic->ic_name = device_get_nameunit(dev); 271 ic->ic_opmode = IEEE80211_M_STA; 272 ic->ic_phytype = IEEE80211_T_DS; 273 274 /* set device capabilities */ 275 ic->ic_caps = 276 IEEE80211_C_STA /* station mode supported */ 277 | IEEE80211_C_IBSS /* IBSS mode supported */ 278 | IEEE80211_C_MONITOR /* monitor mode supported */ 279 | IEEE80211_C_PMGT /* power save supported */ 280 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 281 | IEEE80211_C_WPA /* 802.11i supported */ 282 ; 283 284 /* read MAC address from EEPROM */ 285 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0); 286 ic->ic_macaddr[0] = val >> 8; 287 ic->ic_macaddr[1] = val & 0xff; 288 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1); 289 ic->ic_macaddr[2] = val >> 8; 290 ic->ic_macaddr[3] = val & 0xff; 291 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2); 292 ic->ic_macaddr[4] = val >> 8; 293 ic->ic_macaddr[5] = val & 0xff; 294 295 /* set supported .11b channels (read from EEPROM) */ 296 if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0) 297 val = 0x7ff; /* default to channels 1-11 */ 298 val <<= 1; 299 for (i = 1; i < 16; i++) { 300 if (val & (1 << i)) { 301 c = &ic->ic_channels[ic->ic_nchans++]; 302 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 303 c->ic_flags = IEEE80211_CHAN_B; 304 c->ic_ieee = i; 305 } 306 } 307 308 /* check support for radio transmitter switch in EEPROM */ 309 if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8)) 310 sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH; 311 312 ieee80211_ifattach(ic); 313 ic->ic_scan_start = ipw_scan_start; 314 ic->ic_scan_end = ipw_scan_end; 315 ic->ic_set_channel = ipw_set_channel; 316 ic->ic_scan_curchan = ipw_scan_curchan; 317 ic->ic_scan_mindwell = ipw_scan_mindwell; 318 ic->ic_raw_xmit = ipw_raw_xmit; 319 ic->ic_vap_create = ipw_vap_create; 320 ic->ic_vap_delete = ipw_vap_delete; 321 ic->ic_transmit = ipw_transmit; 322 ic->ic_parent = ipw_parent; 323 324 ieee80211_radiotap_attach(ic, 325 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 326 IPW_TX_RADIOTAP_PRESENT, 327 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 328 IPW_RX_RADIOTAP_PRESENT); 329 330 /* 331 * Add a few sysctl knobs. 332 */ 333 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 334 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio", 335 CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I", 336 "radio transmitter switch state (0=off, 1=on)"); 337 338 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 339 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats", 340 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S", 341 "statistics"); 342 343 /* 344 * Hook our interrupt after all initialization is complete. 345 */ 346 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 347 NULL, ipw_intr, sc, &sc->sc_ih); 348 if (error != 0) { 349 device_printf(dev, "could not set up interrupt\n"); 350 goto fail3; 351 } 352 353 if (bootverbose) 354 ieee80211_announce(ic); 355 356 return 0; 357 fail3: 358 ipw_release(sc); 359 fail2: 360 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq); 361 fail1: 362 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem), 363 sc->mem); 364 fail: 365 mtx_destroy(&sc->sc_mtx); 366 return ENXIO; 367 } 368 369 static int 370 ipw_detach(device_t dev) 371 { 372 struct ipw_softc *sc = device_get_softc(dev); 373 struct ieee80211com *ic = &sc->sc_ic; 374 375 bus_teardown_intr(dev, sc->irq, sc->sc_ih); 376 377 ieee80211_draintask(ic, &sc->sc_init_task); 378 ipw_stop(sc); 379 380 ieee80211_ifdetach(ic); 381 382 callout_drain(&sc->sc_wdtimer); 383 mbufq_drain(&sc->sc_snd); 384 385 ipw_release(sc); 386 387 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->irq), sc->irq); 388 389 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->mem), 390 sc->mem); 391 392 if (sc->sc_firmware != NULL) { 393 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD); 394 sc->sc_firmware = NULL; 395 } 396 397 mtx_destroy(&sc->sc_mtx); 398 399 return 0; 400 } 401 402 static struct ieee80211vap * 403 ipw_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 404 enum ieee80211_opmode opmode, int flags, 405 const uint8_t bssid[IEEE80211_ADDR_LEN], 406 const uint8_t mac[IEEE80211_ADDR_LEN]) 407 { 408 struct ipw_softc *sc = ic->ic_softc; 409 struct ipw_vap *ivp; 410 struct ieee80211vap *vap; 411 const struct firmware *fp; 412 const struct ipw_firmware_hdr *hdr; 413 const char *imagename; 414 415 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 416 return NULL; 417 418 switch (opmode) { 419 case IEEE80211_M_STA: 420 imagename = "ipw_bss"; 421 break; 422 case IEEE80211_M_IBSS: 423 imagename = "ipw_ibss"; 424 break; 425 case IEEE80211_M_MONITOR: 426 imagename = "ipw_monitor"; 427 break; 428 default: 429 return NULL; 430 } 431 432 /* 433 * Load firmware image using the firmware(9) subsystem. Doing 434 * this unlocked is ok since we're single-threaded by the 435 * 802.11 layer. 436 */ 437 if (sc->sc_firmware == NULL || 438 strcmp(sc->sc_firmware->name, imagename) != 0) { 439 if (sc->sc_firmware != NULL) 440 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD); 441 sc->sc_firmware = firmware_get(imagename); 442 } 443 if (sc->sc_firmware == NULL) { 444 device_printf(sc->sc_dev, 445 "could not load firmware image '%s'\n", imagename); 446 return NULL; 447 } 448 fp = sc->sc_firmware; 449 if (fp->datasize < sizeof *hdr) { 450 device_printf(sc->sc_dev, 451 "firmware image too short %zu\n", fp->datasize); 452 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD); 453 sc->sc_firmware = NULL; 454 return NULL; 455 } 456 hdr = (const struct ipw_firmware_hdr *)fp->data; 457 if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) + 458 le32toh(hdr->ucodesz)) { 459 device_printf(sc->sc_dev, 460 "firmware image too short %zu\n", fp->datasize); 461 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD); 462 sc->sc_firmware = NULL; 463 return NULL; 464 } 465 466 ivp = malloc(sizeof(struct ipw_vap), M_80211_VAP, M_WAITOK | M_ZERO); 467 vap = &ivp->vap; 468 469 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid); 470 /* override with driver methods */ 471 ivp->newstate = vap->iv_newstate; 472 vap->iv_newstate = ipw_newstate; 473 474 /* complete setup */ 475 ieee80211_vap_attach(vap, ieee80211_media_change, ipw_media_status, 476 mac); 477 ic->ic_opmode = opmode; 478 return vap; 479 } 480 481 static void 482 ipw_vap_delete(struct ieee80211vap *vap) 483 { 484 struct ipw_vap *ivp = IPW_VAP(vap); 485 486 ieee80211_vap_detach(vap); 487 free(ivp, M_80211_VAP); 488 } 489 490 static int 491 ipw_dma_alloc(struct ipw_softc *sc) 492 { 493 struct ipw_soft_bd *sbd; 494 struct ipw_soft_hdr *shdr; 495 struct ipw_soft_buf *sbuf; 496 bus_addr_t physaddr; 497 int error, i; 498 499 /* 500 * Allocate parent DMA tag for subsequent allocations. 501 */ 502 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 503 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 504 BUS_SPACE_MAXSIZE_32BIT, BUS_SPACE_UNRESTRICTED, 505 BUS_SPACE_MAXSIZE_32BIT, 0, NULL, NULL, &sc->parent_dmat); 506 if (error != 0) { 507 device_printf(sc->sc_dev, "could not create parent DMA tag\n"); 508 goto fail; 509 } 510 511 /* 512 * Allocate and map tx ring. 513 */ 514 error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT, 515 BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL, 516 NULL, &sc->tbd_dmat); 517 if (error != 0) { 518 device_printf(sc->sc_dev, "could not create tx ring DMA tag\n"); 519 goto fail; 520 } 521 522 error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list, 523 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map); 524 if (error != 0) { 525 device_printf(sc->sc_dev, 526 "could not allocate tx ring DMA memory\n"); 527 goto fail; 528 } 529 530 error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list, 531 IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0); 532 if (error != 0) { 533 device_printf(sc->sc_dev, "could not map tx ring DMA memory\n"); 534 goto fail; 535 } 536 537 /* 538 * Allocate and map rx ring. 539 */ 540 error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT, 541 BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL, 542 NULL, &sc->rbd_dmat); 543 if (error != 0) { 544 device_printf(sc->sc_dev, "could not create rx ring DMA tag\n"); 545 goto fail; 546 } 547 548 error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list, 549 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map); 550 if (error != 0) { 551 device_printf(sc->sc_dev, 552 "could not allocate rx ring DMA memory\n"); 553 goto fail; 554 } 555 556 error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list, 557 IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0); 558 if (error != 0) { 559 device_printf(sc->sc_dev, "could not map rx ring DMA memory\n"); 560 goto fail; 561 } 562 563 /* 564 * Allocate and map status ring. 565 */ 566 error = bus_dma_tag_create(sc->parent_dmat, 4, 0, BUS_SPACE_MAXADDR_32BIT, 567 BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0, 568 NULL, NULL, &sc->status_dmat); 569 if (error != 0) { 570 device_printf(sc->sc_dev, 571 "could not create status ring DMA tag\n"); 572 goto fail; 573 } 574 575 error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list, 576 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map); 577 if (error != 0) { 578 device_printf(sc->sc_dev, 579 "could not allocate status ring DMA memory\n"); 580 goto fail; 581 } 582 583 error = bus_dmamap_load(sc->status_dmat, sc->status_map, 584 sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys, 585 0); 586 if (error != 0) { 587 device_printf(sc->sc_dev, 588 "could not map status ring DMA memory\n"); 589 goto fail; 590 } 591 592 /* 593 * Allocate command DMA map. 594 */ 595 error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT, 596 BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1, 597 sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat); 598 if (error != 0) { 599 device_printf(sc->sc_dev, "could not create command DMA tag\n"); 600 goto fail; 601 } 602 603 error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map); 604 if (error != 0) { 605 device_printf(sc->sc_dev, 606 "could not create command DMA map\n"); 607 goto fail; 608 } 609 610 /* 611 * Allocate headers DMA maps. 612 */ 613 error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT, 614 BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1, 615 sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat); 616 if (error != 0) { 617 device_printf(sc->sc_dev, "could not create header DMA tag\n"); 618 goto fail; 619 } 620 621 SLIST_INIT(&sc->free_shdr); 622 for (i = 0; i < IPW_NDATA; i++) { 623 shdr = &sc->shdr_list[i]; 624 error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map); 625 if (error != 0) { 626 device_printf(sc->sc_dev, 627 "could not create header DMA map\n"); 628 goto fail; 629 } 630 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next); 631 } 632 633 /* 634 * Allocate tx buffers DMA maps. 635 */ 636 error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT, 637 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0, 638 NULL, NULL, &sc->txbuf_dmat); 639 if (error != 0) { 640 device_printf(sc->sc_dev, "could not create tx DMA tag\n"); 641 goto fail; 642 } 643 644 SLIST_INIT(&sc->free_sbuf); 645 for (i = 0; i < IPW_NDATA; i++) { 646 sbuf = &sc->tx_sbuf_list[i]; 647 error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map); 648 if (error != 0) { 649 device_printf(sc->sc_dev, 650 "could not create tx DMA map\n"); 651 goto fail; 652 } 653 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next); 654 } 655 656 /* 657 * Initialize tx ring. 658 */ 659 for (i = 0; i < IPW_NTBD; i++) { 660 sbd = &sc->stbd_list[i]; 661 sbd->bd = &sc->tbd_list[i]; 662 sbd->type = IPW_SBD_TYPE_NOASSOC; 663 } 664 665 /* 666 * Pre-allocate rx buffers and DMA maps. 667 */ 668 error = bus_dma_tag_create(sc->parent_dmat, 1, 0, BUS_SPACE_MAXADDR_32BIT, 669 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL, 670 NULL, &sc->rxbuf_dmat); 671 if (error != 0) { 672 device_printf(sc->sc_dev, "could not create rx DMA tag\n"); 673 goto fail; 674 } 675 676 for (i = 0; i < IPW_NRBD; i++) { 677 sbd = &sc->srbd_list[i]; 678 sbuf = &sc->rx_sbuf_list[i]; 679 sbd->bd = &sc->rbd_list[i]; 680 681 sbuf->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 682 if (sbuf->m == NULL) { 683 device_printf(sc->sc_dev, 684 "could not allocate rx mbuf\n"); 685 error = ENOMEM; 686 goto fail; 687 } 688 689 error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map); 690 if (error != 0) { 691 device_printf(sc->sc_dev, 692 "could not create rx DMA map\n"); 693 goto fail; 694 } 695 696 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, 697 mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr, 698 &physaddr, 0); 699 if (error != 0) { 700 device_printf(sc->sc_dev, 701 "could not map rx DMA memory\n"); 702 goto fail; 703 } 704 705 sbd->type = IPW_SBD_TYPE_DATA; 706 sbd->priv = sbuf; 707 sbd->bd->physaddr = htole32(physaddr); 708 sbd->bd->len = htole32(MCLBYTES); 709 } 710 711 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE); 712 713 return 0; 714 715 fail: ipw_release(sc); 716 return error; 717 } 718 719 static void 720 ipw_release(struct ipw_softc *sc) 721 { 722 struct ipw_soft_buf *sbuf; 723 int i; 724 725 if (sc->parent_dmat != NULL) { 726 bus_dma_tag_destroy(sc->parent_dmat); 727 } 728 729 if (sc->tbd_dmat != NULL) { 730 bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map); 731 bus_dmamem_free(sc->tbd_dmat, sc->tbd_list, sc->tbd_map); 732 bus_dma_tag_destroy(sc->tbd_dmat); 733 } 734 735 if (sc->rbd_dmat != NULL) { 736 if (sc->rbd_list != NULL) { 737 bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map); 738 bus_dmamem_free(sc->rbd_dmat, sc->rbd_list, 739 sc->rbd_map); 740 } 741 bus_dma_tag_destroy(sc->rbd_dmat); 742 } 743 744 if (sc->status_dmat != NULL) { 745 if (sc->status_list != NULL) { 746 bus_dmamap_unload(sc->status_dmat, sc->status_map); 747 bus_dmamem_free(sc->status_dmat, sc->status_list, 748 sc->status_map); 749 } 750 bus_dma_tag_destroy(sc->status_dmat); 751 } 752 753 for (i = 0; i < IPW_NTBD; i++) 754 ipw_release_sbd(sc, &sc->stbd_list[i]); 755 756 if (sc->cmd_dmat != NULL) { 757 bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map); 758 bus_dma_tag_destroy(sc->cmd_dmat); 759 } 760 761 if (sc->hdr_dmat != NULL) { 762 for (i = 0; i < IPW_NDATA; i++) 763 bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map); 764 bus_dma_tag_destroy(sc->hdr_dmat); 765 } 766 767 if (sc->txbuf_dmat != NULL) { 768 for (i = 0; i < IPW_NDATA; i++) { 769 bus_dmamap_destroy(sc->txbuf_dmat, 770 sc->tx_sbuf_list[i].map); 771 } 772 bus_dma_tag_destroy(sc->txbuf_dmat); 773 } 774 775 if (sc->rxbuf_dmat != NULL) { 776 for (i = 0; i < IPW_NRBD; i++) { 777 sbuf = &sc->rx_sbuf_list[i]; 778 if (sbuf->m != NULL) { 779 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, 780 BUS_DMASYNC_POSTREAD); 781 bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map); 782 m_freem(sbuf->m); 783 } 784 bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map); 785 } 786 bus_dma_tag_destroy(sc->rxbuf_dmat); 787 } 788 } 789 790 static int 791 ipw_shutdown(device_t dev) 792 { 793 struct ipw_softc *sc = device_get_softc(dev); 794 795 ipw_stop(sc); 796 797 return 0; 798 } 799 800 static int 801 ipw_suspend(device_t dev) 802 { 803 struct ipw_softc *sc = device_get_softc(dev); 804 struct ieee80211com *ic = &sc->sc_ic; 805 806 ieee80211_suspend_all(ic); 807 return 0; 808 } 809 810 static int 811 ipw_resume(device_t dev) 812 { 813 struct ipw_softc *sc = device_get_softc(dev); 814 struct ieee80211com *ic = &sc->sc_ic; 815 816 pci_write_config(dev, 0x41, 0, 1); 817 818 ieee80211_resume_all(ic); 819 return 0; 820 } 821 822 static int 823 ipw_cvtrate(int ipwrate) 824 { 825 switch (ipwrate) { 826 case IPW_RATE_DS1: return 2; 827 case IPW_RATE_DS2: return 4; 828 case IPW_RATE_DS5: return 11; 829 case IPW_RATE_DS11: return 22; 830 } 831 return 0; 832 } 833 834 /* 835 * The firmware automatically adapts the transmit speed. We report its current 836 * value here. 837 */ 838 static void 839 ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr) 840 { 841 struct ieee80211vap *vap = ifp->if_softc; 842 struct ieee80211com *ic = vap->iv_ic; 843 struct ipw_softc *sc = ic->ic_softc; 844 845 /* read current transmission rate from adapter */ 846 vap->iv_bss->ni_txrate = ipw_cvtrate( 847 ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf); 848 ieee80211_media_status(ifp, imr); 849 } 850 851 static int 852 ipw_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 853 { 854 struct ipw_vap *ivp = IPW_VAP(vap); 855 struct ieee80211com *ic = vap->iv_ic; 856 struct ipw_softc *sc = ic->ic_softc; 857 enum ieee80211_state ostate; 858 859 DPRINTF(("%s: %s -> %s flags 0x%x\n", __func__, 860 ieee80211_state_name[vap->iv_state], 861 ieee80211_state_name[nstate], sc->flags)); 862 863 ostate = vap->iv_state; 864 IEEE80211_UNLOCK(ic); 865 866 switch (nstate) { 867 case IEEE80211_S_RUN: 868 if (ic->ic_opmode == IEEE80211_M_IBSS) { 869 /* 870 * XXX when joining an ibss network we are called 871 * with a SCAN -> RUN transition on scan complete. 872 * Use that to call ipw_assoc. On completing the 873 * join we are then called again with an AUTH -> RUN 874 * transition and we want to do nothing. This is 875 * all totally bogus and needs to be redone. 876 */ 877 if (ostate == IEEE80211_S_SCAN) 878 ipw_assoc(ic, vap); 879 } 880 break; 881 882 case IEEE80211_S_INIT: 883 if (sc->flags & IPW_FLAG_ASSOCIATED) 884 ipw_disassoc(ic, vap); 885 break; 886 887 case IEEE80211_S_AUTH: 888 /* 889 * Move to ASSOC state after the ipw_assoc() call. Firmware 890 * takes care of authentication, after the call we'll receive 891 * only an assoc response which would otherwise be discared 892 * if we are still in AUTH state. 893 */ 894 nstate = IEEE80211_S_ASSOC; 895 ipw_assoc(ic, vap); 896 break; 897 898 case IEEE80211_S_ASSOC: 899 /* 900 * If we are not transitioning from AUTH then resend the 901 * association request. 902 */ 903 if (ostate != IEEE80211_S_AUTH) 904 ipw_assoc(ic, vap); 905 break; 906 907 default: 908 break; 909 } 910 IEEE80211_LOCK(ic); 911 return ivp->newstate(vap, nstate, arg); 912 } 913 914 /* 915 * Read 16 bits at address 'addr' from the serial EEPROM. 916 */ 917 static uint16_t 918 ipw_read_prom_word(struct ipw_softc *sc, uint8_t addr) 919 { 920 uint32_t tmp; 921 uint16_t val; 922 int n; 923 924 /* clock C once before the first command */ 925 IPW_EEPROM_CTL(sc, 0); 926 IPW_EEPROM_CTL(sc, IPW_EEPROM_S); 927 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C); 928 IPW_EEPROM_CTL(sc, IPW_EEPROM_S); 929 930 /* write start bit (1) */ 931 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D); 932 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C); 933 934 /* write READ opcode (10) */ 935 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D); 936 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C); 937 IPW_EEPROM_CTL(sc, IPW_EEPROM_S); 938 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C); 939 940 /* write address A7-A0 */ 941 for (n = 7; n >= 0; n--) { 942 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | 943 (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D)); 944 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | 945 (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C); 946 } 947 948 IPW_EEPROM_CTL(sc, IPW_EEPROM_S); 949 950 /* read data Q15-Q0 */ 951 val = 0; 952 for (n = 15; n >= 0; n--) { 953 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C); 954 IPW_EEPROM_CTL(sc, IPW_EEPROM_S); 955 tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL); 956 val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n; 957 } 958 959 IPW_EEPROM_CTL(sc, 0); 960 961 /* clear Chip Select and clock C */ 962 IPW_EEPROM_CTL(sc, IPW_EEPROM_S); 963 IPW_EEPROM_CTL(sc, 0); 964 IPW_EEPROM_CTL(sc, IPW_EEPROM_C); 965 966 return le16toh(val); 967 } 968 969 static void 970 ipw_rx_cmd_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf) 971 { 972 struct ipw_cmd *cmd; 973 974 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD); 975 976 cmd = mtod(sbuf->m, struct ipw_cmd *); 977 978 DPRINTFN(9, ("cmd ack'ed %s(%u, %u, %u, %u, %u)\n", 979 ipw_cmdname(le32toh(cmd->type)), le32toh(cmd->type), 980 le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len), 981 le32toh(cmd->status))); 982 983 sc->flags &= ~IPW_FLAG_BUSY; 984 wakeup(sc); 985 } 986 987 static void 988 ipw_rx_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf) 989 { 990 #define IEEESTATE(vap) ieee80211_state_name[vap->iv_state] 991 struct ieee80211com *ic = &sc->sc_ic; 992 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 993 uint32_t state; 994 995 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD); 996 997 state = le32toh(*mtod(sbuf->m, uint32_t *)); 998 999 switch (state) { 1000 case IPW_STATE_ASSOCIATED: 1001 DPRINTFN(2, ("Association succeeded (%s flags 0x%x)\n", 1002 IEEESTATE(vap), sc->flags)); 1003 /* XXX suppress state change in case the fw auto-associates */ 1004 if ((sc->flags & IPW_FLAG_ASSOCIATING) == 0) { 1005 DPRINTF(("Unexpected association (%s, flags 0x%x)\n", 1006 IEEESTATE(vap), sc->flags)); 1007 break; 1008 } 1009 sc->flags &= ~IPW_FLAG_ASSOCIATING; 1010 sc->flags |= IPW_FLAG_ASSOCIATED; 1011 break; 1012 1013 case IPW_STATE_SCANNING: 1014 DPRINTFN(3, ("Scanning (%s flags 0x%x)\n", 1015 IEEESTATE(vap), sc->flags)); 1016 /* 1017 * NB: Check driver state for association on assoc 1018 * loss as the firmware will immediately start to 1019 * scan and we would treat it as a beacon miss if 1020 * we checked the 802.11 layer state. 1021 */ 1022 if (sc->flags & IPW_FLAG_ASSOCIATED) { 1023 IPW_UNLOCK(sc); 1024 /* XXX probably need to issue disassoc to fw */ 1025 ieee80211_beacon_miss(ic); 1026 IPW_LOCK(sc); 1027 } 1028 break; 1029 1030 case IPW_STATE_SCAN_COMPLETE: 1031 /* 1032 * XXX For some reason scan requests generate scan 1033 * started + scan done events before any traffic is 1034 * received (e.g. probe response frames). We work 1035 * around this by marking the HACK flag and skipping 1036 * the first scan complete event. 1037 */ 1038 DPRINTFN(3, ("Scan complete (%s flags 0x%x)\n", 1039 IEEESTATE(vap), sc->flags)); 1040 if (sc->flags & IPW_FLAG_HACK) { 1041 sc->flags &= ~IPW_FLAG_HACK; 1042 break; 1043 } 1044 if (sc->flags & IPW_FLAG_SCANNING) { 1045 IPW_UNLOCK(sc); 1046 ieee80211_scan_done(vap); 1047 IPW_LOCK(sc); 1048 sc->flags &= ~IPW_FLAG_SCANNING; 1049 sc->sc_scan_timer = 0; 1050 } 1051 break; 1052 1053 case IPW_STATE_ASSOCIATION_LOST: 1054 DPRINTFN(2, ("Association lost (%s flags 0x%x)\n", 1055 IEEESTATE(vap), sc->flags)); 1056 sc->flags &= ~(IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED); 1057 if (vap->iv_state == IEEE80211_S_RUN) { 1058 IPW_UNLOCK(sc); 1059 ieee80211_new_state(vap, IEEE80211_S_SCAN, -1); 1060 IPW_LOCK(sc); 1061 } 1062 break; 1063 1064 case IPW_STATE_DISABLED: 1065 /* XXX? is this right? */ 1066 sc->flags &= ~(IPW_FLAG_HACK | IPW_FLAG_SCANNING | 1067 IPW_FLAG_ASSOCIATING | IPW_FLAG_ASSOCIATED); 1068 DPRINTFN(2, ("Firmware disabled (%s flags 0x%x)\n", 1069 IEEESTATE(vap), sc->flags)); 1070 break; 1071 1072 case IPW_STATE_RADIO_DISABLED: 1073 device_printf(sc->sc_dev, "radio turned off\n"); 1074 ieee80211_notify_radio(ic, 0); 1075 ipw_stop_locked(sc); 1076 /* XXX start polling thread to detect radio on */ 1077 break; 1078 1079 default: 1080 DPRINTFN(2, ("%s: unhandled state %u %s flags 0x%x\n", 1081 __func__, state, IEEESTATE(vap), sc->flags)); 1082 break; 1083 } 1084 #undef IEEESTATE 1085 } 1086 1087 /* 1088 * Set driver state for current channel. 1089 */ 1090 static void 1091 ipw_setcurchan(struct ipw_softc *sc, struct ieee80211_channel *chan) 1092 { 1093 struct ieee80211com *ic = &sc->sc_ic; 1094 1095 ic->ic_curchan = chan; 1096 ieee80211_radiotap_chan_change(ic); 1097 } 1098 1099 /* 1100 * XXX: Hack to set the current channel to the value advertised in beacons or 1101 * probe responses. Only used during AP detection. 1102 */ 1103 static void 1104 ipw_fix_channel(struct ipw_softc *sc, struct mbuf *m) 1105 { 1106 struct ieee80211com *ic = &sc->sc_ic; 1107 struct ieee80211_channel *c; 1108 struct ieee80211_frame *wh; 1109 uint8_t subtype; 1110 uint8_t *frm, *efrm; 1111 1112 wh = mtod(m, struct ieee80211_frame *); 1113 1114 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT) 1115 return; 1116 1117 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 1118 1119 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON && 1120 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1121 return; 1122 1123 /* XXX use ieee80211_parse_beacon */ 1124 frm = (uint8_t *)(wh + 1); 1125 efrm = mtod(m, uint8_t *) + m->m_len; 1126 1127 frm += 12; /* skip tstamp, bintval and capinfo fields */ 1128 while (frm < efrm) { 1129 if (*frm == IEEE80211_ELEMID_DSPARMS) 1130 #if IEEE80211_CHAN_MAX < 255 1131 if (frm[2] <= IEEE80211_CHAN_MAX) 1132 #endif 1133 { 1134 DPRINTF(("Fixing channel to %d\n", frm[2])); 1135 c = ieee80211_find_channel(ic, 1136 ieee80211_ieee2mhz(frm[2], 0), 1137 IEEE80211_CHAN_B); 1138 if (c == NULL) 1139 c = &ic->ic_channels[0]; 1140 ipw_setcurchan(sc, c); 1141 } 1142 1143 frm += frm[1] + 2; 1144 } 1145 } 1146 1147 static void 1148 ipw_rx_data_intr(struct ipw_softc *sc, struct ipw_status *status, 1149 struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf) 1150 { 1151 struct ieee80211com *ic = &sc->sc_ic; 1152 struct mbuf *mnew, *m; 1153 struct ieee80211_node *ni; 1154 bus_addr_t physaddr; 1155 int error; 1156 int8_t rssi, nf; 1157 1158 DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len), 1159 status->rssi)); 1160 1161 if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) || 1162 le32toh(status->len) > MCLBYTES) 1163 return; 1164 1165 /* 1166 * Try to allocate a new mbuf for this ring element and load it before 1167 * processing the current mbuf. If the ring element cannot be loaded, 1168 * drop the received packet and reuse the old mbuf. In the unlikely 1169 * case that the old mbuf can't be reloaded either, explicitly panic. 1170 */ 1171 mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 1172 if (mnew == NULL) { 1173 counter_u64_add(ic->ic_ierrors, 1); 1174 return; 1175 } 1176 1177 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD); 1178 bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map); 1179 1180 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *), 1181 MCLBYTES, ipw_dma_map_addr, &physaddr, 0); 1182 if (error != 0) { 1183 m_freem(mnew); 1184 1185 /* try to reload the old mbuf */ 1186 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, 1187 mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr, 1188 &physaddr, 0); 1189 if (error != 0) { 1190 /* very unlikely that it will fail... */ 1191 panic("%s: could not load old rx mbuf", 1192 device_get_name(sc->sc_dev)); 1193 } 1194 counter_u64_add(ic->ic_ierrors, 1); 1195 return; 1196 } 1197 1198 /* 1199 * New mbuf successfully loaded, update Rx ring and continue 1200 * processing. 1201 */ 1202 m = sbuf->m; 1203 sbuf->m = mnew; 1204 sbd->bd->physaddr = htole32(physaddr); 1205 m->m_pkthdr.len = m->m_len = le32toh(status->len); 1206 1207 rssi = status->rssi + IPW_RSSI_TO_DBM; 1208 nf = -95; 1209 if (ieee80211_radiotap_active(ic)) { 1210 struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap; 1211 1212 tap->wr_flags = 0; 1213 tap->wr_antsignal = rssi; 1214 tap->wr_antnoise = nf; 1215 } 1216 1217 if (sc->flags & IPW_FLAG_SCANNING) 1218 ipw_fix_channel(sc, m); 1219 1220 IPW_UNLOCK(sc); 1221 ni = ieee80211_find_rxnode(ic, mtod(m, struct ieee80211_frame_min *)); 1222 if (ni != NULL) { 1223 (void) ieee80211_input(ni, m, rssi - nf, nf); 1224 ieee80211_free_node(ni); 1225 } else 1226 (void) ieee80211_input_all(ic, m, rssi - nf, nf); 1227 IPW_LOCK(sc); 1228 1229 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE); 1230 } 1231 1232 static void 1233 ipw_rx_intr(struct ipw_softc *sc) 1234 { 1235 struct ipw_status *status; 1236 struct ipw_soft_bd *sbd; 1237 struct ipw_soft_buf *sbuf; 1238 uint32_t r, i; 1239 1240 if (!(sc->flags & IPW_FLAG_FW_INITED)) 1241 return; 1242 1243 r = CSR_READ_4(sc, IPW_CSR_RX_READ); 1244 1245 bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD); 1246 1247 for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) { 1248 status = &sc->status_list[i]; 1249 sbd = &sc->srbd_list[i]; 1250 sbuf = sbd->priv; 1251 1252 switch (le16toh(status->code) & 0xf) { 1253 case IPW_STATUS_CODE_COMMAND: 1254 ipw_rx_cmd_intr(sc, sbuf); 1255 break; 1256 1257 case IPW_STATUS_CODE_NEWSTATE: 1258 ipw_rx_newstate_intr(sc, sbuf); 1259 break; 1260 1261 case IPW_STATUS_CODE_DATA_802_3: 1262 case IPW_STATUS_CODE_DATA_802_11: 1263 ipw_rx_data_intr(sc, status, sbd, sbuf); 1264 break; 1265 1266 case IPW_STATUS_CODE_NOTIFICATION: 1267 DPRINTFN(2, ("notification status, len %u flags 0x%x\n", 1268 le32toh(status->len), status->flags)); 1269 /* XXX maybe drive state machine AUTH->ASSOC? */ 1270 break; 1271 1272 default: 1273 device_printf(sc->sc_dev, "unexpected status code %u\n", 1274 le16toh(status->code)); 1275 } 1276 1277 /* firmware was killed, stop processing received frames */ 1278 if (!(sc->flags & IPW_FLAG_FW_INITED)) 1279 return; 1280 1281 sbd->bd->flags = 0; 1282 } 1283 1284 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE); 1285 1286 /* kick the firmware */ 1287 sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1; 1288 CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur); 1289 } 1290 1291 static void 1292 ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd) 1293 { 1294 struct ipw_soft_hdr *shdr; 1295 struct ipw_soft_buf *sbuf; 1296 1297 switch (sbd->type) { 1298 case IPW_SBD_TYPE_COMMAND: 1299 bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, 1300 BUS_DMASYNC_POSTWRITE); 1301 bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map); 1302 break; 1303 1304 case IPW_SBD_TYPE_HEADER: 1305 shdr = sbd->priv; 1306 bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE); 1307 bus_dmamap_unload(sc->hdr_dmat, shdr->map); 1308 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next); 1309 break; 1310 1311 case IPW_SBD_TYPE_DATA: 1312 sbuf = sbd->priv; 1313 bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, 1314 BUS_DMASYNC_POSTWRITE); 1315 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map); 1316 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next); 1317 1318 if (sbuf->m->m_flags & M_TXCB) 1319 ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/); 1320 m_freem(sbuf->m); 1321 ieee80211_free_node(sbuf->ni); 1322 1323 sc->sc_tx_timer = 0; 1324 break; 1325 } 1326 1327 sbd->type = IPW_SBD_TYPE_NOASSOC; 1328 } 1329 1330 static void 1331 ipw_tx_intr(struct ipw_softc *sc) 1332 { 1333 struct ipw_soft_bd *sbd; 1334 uint32_t r, i; 1335 1336 if (!(sc->flags & IPW_FLAG_FW_INITED)) 1337 return; 1338 1339 r = CSR_READ_4(sc, IPW_CSR_TX_READ); 1340 1341 for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) { 1342 sbd = &sc->stbd_list[i]; 1343 ipw_release_sbd(sc, sbd); 1344 sc->txfree++; 1345 } 1346 1347 /* remember what the firmware has processed */ 1348 sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1; 1349 1350 ipw_start(sc); 1351 } 1352 1353 static void 1354 ipw_fatal_error_intr(struct ipw_softc *sc) 1355 { 1356 struct ieee80211com *ic = &sc->sc_ic; 1357 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1358 1359 device_printf(sc->sc_dev, "firmware error\n"); 1360 if (vap != NULL) { 1361 IPW_UNLOCK(sc); 1362 ieee80211_cancel_scan(vap); 1363 IPW_LOCK(sc); 1364 } 1365 ieee80211_runtask(ic, &sc->sc_init_task); 1366 } 1367 1368 static void 1369 ipw_intr(void *arg) 1370 { 1371 struct ipw_softc *sc = arg; 1372 uint32_t r; 1373 1374 IPW_LOCK(sc); 1375 1376 r = CSR_READ_4(sc, IPW_CSR_INTR); 1377 if (r == 0 || r == 0xffffffff) 1378 goto done; 1379 1380 /* disable interrupts */ 1381 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0); 1382 1383 /* acknowledge all interrupts */ 1384 CSR_WRITE_4(sc, IPW_CSR_INTR, r); 1385 1386 if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) { 1387 ipw_fatal_error_intr(sc); 1388 goto done; 1389 } 1390 1391 if (r & IPW_INTR_FW_INIT_DONE) 1392 wakeup(sc); 1393 1394 if (r & IPW_INTR_RX_TRANSFER) 1395 ipw_rx_intr(sc); 1396 1397 if (r & IPW_INTR_TX_TRANSFER) 1398 ipw_tx_intr(sc); 1399 1400 /* re-enable interrupts */ 1401 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK); 1402 done: 1403 IPW_UNLOCK(sc); 1404 } 1405 1406 static void 1407 ipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1408 { 1409 if (error != 0) 1410 return; 1411 1412 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 1413 1414 *(bus_addr_t *)arg = segs[0].ds_addr; 1415 } 1416 1417 static const char * 1418 ipw_cmdname(int cmd) 1419 { 1420 #define N(a) (sizeof(a) / sizeof(a[0])) 1421 static const struct { 1422 int cmd; 1423 const char *name; 1424 } cmds[] = { 1425 { IPW_CMD_ADD_MULTICAST, "ADD_MULTICAST" }, 1426 { IPW_CMD_BROADCAST_SCAN, "BROADCAST_SCAN" }, 1427 { IPW_CMD_DISABLE, "DISABLE" }, 1428 { IPW_CMD_DISABLE_PHY, "DISABLE_PHY" }, 1429 { IPW_CMD_ENABLE, "ENABLE" }, 1430 { IPW_CMD_PREPARE_POWER_DOWN, "PREPARE_POWER_DOWN" }, 1431 { IPW_CMD_SET_BASIC_TX_RATES, "SET_BASIC_TX_RATES" }, 1432 { IPW_CMD_SET_BEACON_INTERVAL, "SET_BEACON_INTERVAL" }, 1433 { IPW_CMD_SET_CHANNEL, "SET_CHANNEL" }, 1434 { IPW_CMD_SET_CONFIGURATION, "SET_CONFIGURATION" }, 1435 { IPW_CMD_SET_DESIRED_BSSID, "SET_DESIRED_BSSID" }, 1436 { IPW_CMD_SET_ESSID, "SET_ESSID" }, 1437 { IPW_CMD_SET_FRAG_THRESHOLD, "SET_FRAG_THRESHOLD" }, 1438 { IPW_CMD_SET_MAC_ADDRESS, "SET_MAC_ADDRESS" }, 1439 { IPW_CMD_SET_MANDATORY_BSSID, "SET_MANDATORY_BSSID" }, 1440 { IPW_CMD_SET_MODE, "SET_MODE" }, 1441 { IPW_CMD_SET_MSDU_TX_RATES, "SET_MSDU_TX_RATES" }, 1442 { IPW_CMD_SET_POWER_MODE, "SET_POWER_MODE" }, 1443 { IPW_CMD_SET_RTS_THRESHOLD, "SET_RTS_THRESHOLD" }, 1444 { IPW_CMD_SET_SCAN_OPTIONS, "SET_SCAN_OPTIONS" }, 1445 { IPW_CMD_SET_SECURITY_INFO, "SET_SECURITY_INFO" }, 1446 { IPW_CMD_SET_TX_POWER_INDEX, "SET_TX_POWER_INDEX" }, 1447 { IPW_CMD_SET_TX_RATES, "SET_TX_RATES" }, 1448 { IPW_CMD_SET_WEP_FLAGS, "SET_WEP_FLAGS" }, 1449 { IPW_CMD_SET_WEP_KEY, "SET_WEP_KEY" }, 1450 { IPW_CMD_SET_WEP_KEY_INDEX, "SET_WEP_KEY_INDEX" }, 1451 { IPW_CMD_SET_WPA_IE, "SET_WPA_IE" }, 1452 1453 }; 1454 static char buf[12]; 1455 int i; 1456 1457 for (i = 0; i < N(cmds); i++) 1458 if (cmds[i].cmd == cmd) 1459 return cmds[i].name; 1460 snprintf(buf, sizeof(buf), "%u", cmd); 1461 return buf; 1462 #undef N 1463 } 1464 1465 /* 1466 * Send a command to the firmware and wait for the acknowledgement. 1467 */ 1468 static int 1469 ipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len) 1470 { 1471 struct ipw_soft_bd *sbd; 1472 bus_addr_t physaddr; 1473 int error; 1474 1475 IPW_LOCK_ASSERT(sc); 1476 1477 if (sc->flags & IPW_FLAG_BUSY) { 1478 device_printf(sc->sc_dev, "%s: %s not sent, busy\n", 1479 __func__, ipw_cmdname(type)); 1480 return EAGAIN; 1481 } 1482 sc->flags |= IPW_FLAG_BUSY; 1483 1484 sbd = &sc->stbd_list[sc->txcur]; 1485 1486 error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd, 1487 sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0); 1488 if (error != 0) { 1489 device_printf(sc->sc_dev, "could not map command DMA memory\n"); 1490 sc->flags &= ~IPW_FLAG_BUSY; 1491 return error; 1492 } 1493 1494 sc->cmd.type = htole32(type); 1495 sc->cmd.subtype = 0; 1496 sc->cmd.len = htole32(len); 1497 sc->cmd.seq = 0; 1498 memcpy(sc->cmd.data, data, len); 1499 1500 sbd->type = IPW_SBD_TYPE_COMMAND; 1501 sbd->bd->physaddr = htole32(physaddr); 1502 sbd->bd->len = htole32(sizeof (struct ipw_cmd)); 1503 sbd->bd->nfrag = 1; 1504 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND | 1505 IPW_BD_FLAG_TX_LAST_FRAGMENT; 1506 1507 bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE); 1508 bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE); 1509 1510 #ifdef IPW_DEBUG 1511 if (ipw_debug >= 4) { 1512 printf("sending %s(%u, %u, %u, %u)", ipw_cmdname(type), type, 1513 0, 0, len); 1514 /* Print the data buffer in the higher debug level */ 1515 if (ipw_debug >= 9 && len > 0) { 1516 printf(" data: 0x"); 1517 for (int i = 1; i <= len; i++) 1518 printf("%1D", (u_char *)data + len - i, ""); 1519 } 1520 printf("\n"); 1521 } 1522 #endif 1523 1524 /* kick firmware */ 1525 sc->txfree--; 1526 sc->txcur = (sc->txcur + 1) % IPW_NTBD; 1527 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur); 1528 1529 /* wait at most one second for command to complete */ 1530 error = msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz); 1531 if (error != 0) { 1532 device_printf(sc->sc_dev, "%s: %s failed, timeout (error %u)\n", 1533 __func__, ipw_cmdname(type), error); 1534 sc->flags &= ~IPW_FLAG_BUSY; 1535 return (error); 1536 } 1537 return (0); 1538 } 1539 1540 static int 1541 ipw_tx_start(struct ipw_softc *sc, struct mbuf *m0, struct ieee80211_node *ni) 1542 { 1543 struct ieee80211com *ic = &sc->sc_ic; 1544 struct ieee80211vap *vap = ni->ni_vap; 1545 struct ieee80211_frame *wh; 1546 struct ipw_soft_bd *sbd; 1547 struct ipw_soft_hdr *shdr; 1548 struct ipw_soft_buf *sbuf; 1549 struct ieee80211_key *k; 1550 struct mbuf *mnew; 1551 bus_dma_segment_t segs[IPW_MAX_NSEG]; 1552 bus_addr_t physaddr; 1553 int nsegs, error, i; 1554 1555 wh = mtod(m0, struct ieee80211_frame *); 1556 1557 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1558 k = ieee80211_crypto_encap(ni, m0); 1559 if (k == NULL) { 1560 m_freem(m0); 1561 return ENOBUFS; 1562 } 1563 /* packet header may have moved, reset our local pointer */ 1564 wh = mtod(m0, struct ieee80211_frame *); 1565 } 1566 1567 if (ieee80211_radiotap_active_vap(vap)) { 1568 struct ipw_tx_radiotap_header *tap = &sc->sc_txtap; 1569 1570 tap->wt_flags = 0; 1571 1572 ieee80211_radiotap_tx(vap, m0); 1573 } 1574 1575 shdr = SLIST_FIRST(&sc->free_shdr); 1576 sbuf = SLIST_FIRST(&sc->free_sbuf); 1577 KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool")); 1578 1579 shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND); 1580 shdr->hdr.subtype = 0; 1581 shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) ? 1 : 0; 1582 shdr->hdr.encrypt = 0; 1583 shdr->hdr.keyidx = 0; 1584 shdr->hdr.keysz = 0; 1585 shdr->hdr.fragmentsz = 0; 1586 IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2); 1587 if (ic->ic_opmode == IEEE80211_M_STA) 1588 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3); 1589 else 1590 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1); 1591 1592 /* trim IEEE802.11 header */ 1593 m_adj(m0, sizeof (struct ieee80211_frame)); 1594 1595 error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs, 1596 &nsegs, 0); 1597 if (error != 0 && error != EFBIG) { 1598 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1599 error); 1600 m_freem(m0); 1601 return error; 1602 } 1603 if (error != 0) { 1604 mnew = m_defrag(m0, M_NOWAIT); 1605 if (mnew == NULL) { 1606 device_printf(sc->sc_dev, 1607 "could not defragment mbuf\n"); 1608 m_freem(m0); 1609 return ENOBUFS; 1610 } 1611 m0 = mnew; 1612 1613 error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, 1614 segs, &nsegs, 0); 1615 if (error != 0) { 1616 device_printf(sc->sc_dev, 1617 "could not map mbuf (error %d)\n", error); 1618 m_freem(m0); 1619 return error; 1620 } 1621 } 1622 1623 error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr, 1624 sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0); 1625 if (error != 0) { 1626 device_printf(sc->sc_dev, "could not map header DMA memory\n"); 1627 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map); 1628 m_freem(m0); 1629 return error; 1630 } 1631 1632 SLIST_REMOVE_HEAD(&sc->free_sbuf, next); 1633 SLIST_REMOVE_HEAD(&sc->free_shdr, next); 1634 1635 sbd = &sc->stbd_list[sc->txcur]; 1636 sbd->type = IPW_SBD_TYPE_HEADER; 1637 sbd->priv = shdr; 1638 sbd->bd->physaddr = htole32(physaddr); 1639 sbd->bd->len = htole32(sizeof (struct ipw_hdr)); 1640 sbd->bd->nfrag = 1 + nsegs; 1641 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 | 1642 IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT; 1643 1644 DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n", 1645 shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted, 1646 shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr, 1647 ":")); 1648 1649 sc->txfree--; 1650 sc->txcur = (sc->txcur + 1) % IPW_NTBD; 1651 1652 sbuf->m = m0; 1653 sbuf->ni = ni; 1654 1655 for (i = 0; i < nsegs; i++) { 1656 sbd = &sc->stbd_list[sc->txcur]; 1657 1658 sbd->bd->physaddr = htole32(segs[i].ds_addr); 1659 sbd->bd->len = htole32(segs[i].ds_len); 1660 sbd->bd->nfrag = 0; 1661 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3; 1662 if (i == nsegs - 1) { 1663 sbd->type = IPW_SBD_TYPE_DATA; 1664 sbd->priv = sbuf; 1665 sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT; 1666 } else { 1667 sbd->type = IPW_SBD_TYPE_NOASSOC; 1668 sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT; 1669 } 1670 1671 DPRINTFN(5, ("sending fragment (%d)\n", i)); 1672 1673 sc->txfree--; 1674 sc->txcur = (sc->txcur + 1) % IPW_NTBD; 1675 } 1676 1677 bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE); 1678 bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE); 1679 bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE); 1680 1681 /* kick firmware */ 1682 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur); 1683 1684 return 0; 1685 } 1686 1687 static int 1688 ipw_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 1689 const struct ieee80211_bpf_params *params) 1690 { 1691 /* no support; just discard */ 1692 m_freem(m); 1693 ieee80211_free_node(ni); 1694 return 0; 1695 } 1696 1697 static int 1698 ipw_transmit(struct ieee80211com *ic, struct mbuf *m) 1699 { 1700 struct ipw_softc *sc = ic->ic_softc; 1701 int error; 1702 1703 IPW_LOCK(sc); 1704 if ((sc->flags & IPW_FLAG_RUNNING) == 0) { 1705 IPW_UNLOCK(sc); 1706 return (ENXIO); 1707 } 1708 error = mbufq_enqueue(&sc->sc_snd, m); 1709 if (error) { 1710 IPW_UNLOCK(sc); 1711 return (error); 1712 } 1713 ipw_start(sc); 1714 IPW_UNLOCK(sc); 1715 return (0); 1716 } 1717 1718 static void 1719 ipw_start(struct ipw_softc *sc) 1720 { 1721 struct ieee80211_node *ni; 1722 struct mbuf *m; 1723 1724 IPW_LOCK_ASSERT(sc); 1725 1726 while (sc->txfree < 1 + IPW_MAX_NSEG && 1727 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 1728 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1729 if (ipw_tx_start(sc, m, ni) != 0) { 1730 if_inc_counter(ni->ni_vap->iv_ifp, 1731 IFCOUNTER_OERRORS, 1); 1732 ieee80211_free_node(ni); 1733 break; 1734 } 1735 /* start watchdog timer */ 1736 sc->sc_tx_timer = 5; 1737 } 1738 } 1739 1740 static void 1741 ipw_watchdog(void *arg) 1742 { 1743 struct ipw_softc *sc = arg; 1744 struct ieee80211com *ic = &sc->sc_ic; 1745 1746 IPW_LOCK_ASSERT(sc); 1747 1748 if (sc->sc_tx_timer > 0) { 1749 if (--sc->sc_tx_timer == 0) { 1750 device_printf(sc->sc_dev, "device timeout\n"); 1751 counter_u64_add(ic->ic_oerrors, 1); 1752 taskqueue_enqueue(taskqueue_swi, &sc->sc_init_task); 1753 } 1754 } 1755 if (sc->sc_scan_timer > 0) { 1756 if (--sc->sc_scan_timer == 0) { 1757 DPRINTFN(3, ("Scan timeout\n")); 1758 /* End the scan */ 1759 if (sc->flags & IPW_FLAG_SCANNING) { 1760 IPW_UNLOCK(sc); 1761 ieee80211_scan_done(TAILQ_FIRST(&ic->ic_vaps)); 1762 IPW_LOCK(sc); 1763 sc->flags &= ~IPW_FLAG_SCANNING; 1764 } 1765 } 1766 } 1767 if (sc->flags & IPW_FLAG_RUNNING) 1768 callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc); 1769 } 1770 1771 static void 1772 ipw_parent(struct ieee80211com *ic) 1773 { 1774 struct ipw_softc *sc = ic->ic_softc; 1775 int startall = 0; 1776 1777 IPW_LOCK(sc); 1778 if (ic->ic_nrunning > 0) { 1779 if (!(sc->flags & IPW_FLAG_RUNNING)) { 1780 ipw_init_locked(sc); 1781 startall = 1; 1782 } 1783 } else if (sc->flags & IPW_FLAG_RUNNING) 1784 ipw_stop_locked(sc); 1785 IPW_UNLOCK(sc); 1786 if (startall) 1787 ieee80211_start_all(ic); 1788 } 1789 1790 static void 1791 ipw_stop_master(struct ipw_softc *sc) 1792 { 1793 uint32_t tmp; 1794 int ntries; 1795 1796 /* disable interrupts */ 1797 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0); 1798 1799 CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER); 1800 for (ntries = 0; ntries < 50; ntries++) { 1801 if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED) 1802 break; 1803 DELAY(10); 1804 } 1805 if (ntries == 50) 1806 device_printf(sc->sc_dev, "timeout waiting for master\n"); 1807 1808 tmp = CSR_READ_4(sc, IPW_CSR_RST); 1809 CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET); 1810 1811 /* Clear all flags except the following */ 1812 sc->flags &= IPW_FLAG_HAS_RADIO_SWITCH; 1813 } 1814 1815 static int 1816 ipw_reset(struct ipw_softc *sc) 1817 { 1818 uint32_t tmp; 1819 int ntries; 1820 1821 ipw_stop_master(sc); 1822 1823 /* move adapter to D0 state */ 1824 tmp = CSR_READ_4(sc, IPW_CSR_CTL); 1825 CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT); 1826 1827 /* wait for clock stabilization */ 1828 for (ntries = 0; ntries < 1000; ntries++) { 1829 if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY) 1830 break; 1831 DELAY(200); 1832 } 1833 if (ntries == 1000) 1834 return EIO; 1835 1836 tmp = CSR_READ_4(sc, IPW_CSR_RST); 1837 CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET); 1838 1839 DELAY(10); 1840 1841 tmp = CSR_READ_4(sc, IPW_CSR_CTL); 1842 CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT); 1843 1844 return 0; 1845 } 1846 1847 static int 1848 ipw_waitfordisable(struct ipw_softc *sc, int waitfor) 1849 { 1850 int ms = hz < 1000 ? 1 : hz/10; 1851 int i, error; 1852 1853 for (i = 0; i < 100; i++) { 1854 if (ipw_read_table1(sc, IPW_INFO_CARD_DISABLED) == waitfor) 1855 return 0; 1856 error = msleep(sc, &sc->sc_mtx, PCATCH, __func__, ms); 1857 if (error == 0 || error != EWOULDBLOCK) 1858 return 0; 1859 } 1860 DPRINTF(("%s: timeout waiting for %s\n", 1861 __func__, waitfor ? "disable" : "enable")); 1862 return ETIMEDOUT; 1863 } 1864 1865 static int 1866 ipw_enable(struct ipw_softc *sc) 1867 { 1868 int error; 1869 1870 if ((sc->flags & IPW_FLAG_ENABLED) == 0) { 1871 DPRINTF(("Enable adapter\n")); 1872 error = ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0); 1873 if (error != 0) 1874 return error; 1875 error = ipw_waitfordisable(sc, 0); 1876 if (error != 0) 1877 return error; 1878 sc->flags |= IPW_FLAG_ENABLED; 1879 } 1880 return 0; 1881 } 1882 1883 static int 1884 ipw_disable(struct ipw_softc *sc) 1885 { 1886 int error; 1887 1888 if (sc->flags & IPW_FLAG_ENABLED) { 1889 DPRINTF(("Disable adapter\n")); 1890 error = ipw_cmd(sc, IPW_CMD_DISABLE, NULL, 0); 1891 if (error != 0) 1892 return error; 1893 error = ipw_waitfordisable(sc, 1); 1894 if (error != 0) 1895 return error; 1896 sc->flags &= ~IPW_FLAG_ENABLED; 1897 } 1898 return 0; 1899 } 1900 1901 /* 1902 * Upload the microcode to the device. 1903 */ 1904 static int 1905 ipw_load_ucode(struct ipw_softc *sc, const char *uc, int size) 1906 { 1907 int ntries; 1908 1909 MEM_WRITE_4(sc, 0x3000e0, 0x80000000); 1910 CSR_WRITE_4(sc, IPW_CSR_RST, 0); 1911 1912 MEM_WRITE_2(sc, 0x220000, 0x0703); 1913 MEM_WRITE_2(sc, 0x220000, 0x0707); 1914 1915 MEM_WRITE_1(sc, 0x210014, 0x72); 1916 MEM_WRITE_1(sc, 0x210014, 0x72); 1917 1918 MEM_WRITE_1(sc, 0x210000, 0x40); 1919 MEM_WRITE_1(sc, 0x210000, 0x00); 1920 MEM_WRITE_1(sc, 0x210000, 0x40); 1921 1922 MEM_WRITE_MULTI_1(sc, 0x210010, uc, size); 1923 1924 MEM_WRITE_1(sc, 0x210000, 0x00); 1925 MEM_WRITE_1(sc, 0x210000, 0x00); 1926 MEM_WRITE_1(sc, 0x210000, 0x80); 1927 1928 MEM_WRITE_2(sc, 0x220000, 0x0703); 1929 MEM_WRITE_2(sc, 0x220000, 0x0707); 1930 1931 MEM_WRITE_1(sc, 0x210014, 0x72); 1932 MEM_WRITE_1(sc, 0x210014, 0x72); 1933 1934 MEM_WRITE_1(sc, 0x210000, 0x00); 1935 MEM_WRITE_1(sc, 0x210000, 0x80); 1936 1937 for (ntries = 0; ntries < 10; ntries++) { 1938 if (MEM_READ_1(sc, 0x210000) & 1) 1939 break; 1940 DELAY(10); 1941 } 1942 if (ntries == 10) { 1943 device_printf(sc->sc_dev, 1944 "timeout waiting for ucode to initialize\n"); 1945 return EIO; 1946 } 1947 1948 MEM_WRITE_4(sc, 0x3000e0, 0); 1949 1950 return 0; 1951 } 1952 1953 /* set of macros to handle unaligned little endian data in firmware image */ 1954 #define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24) 1955 #define GETLE16(p) ((p)[0] | (p)[1] << 8) 1956 static int 1957 ipw_load_firmware(struct ipw_softc *sc, const char *fw, int size) 1958 { 1959 const uint8_t *p, *end; 1960 uint32_t tmp, dst; 1961 uint16_t len; 1962 int error; 1963 1964 p = fw; 1965 end = fw + size; 1966 while (p < end) { 1967 dst = GETLE32(p); p += 4; 1968 len = GETLE16(p); p += 2; 1969 1970 ipw_write_mem_1(sc, dst, p, len); 1971 p += len; 1972 } 1973 1974 CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK | 1975 IPW_IO_LED_OFF); 1976 1977 /* enable interrupts */ 1978 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK); 1979 1980 /* kick the firmware */ 1981 CSR_WRITE_4(sc, IPW_CSR_RST, 0); 1982 1983 tmp = CSR_READ_4(sc, IPW_CSR_CTL); 1984 CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY); 1985 1986 /* wait at most one second for firmware initialization to complete */ 1987 if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) { 1988 device_printf(sc->sc_dev, "timeout waiting for firmware " 1989 "initialization to complete\n"); 1990 return error; 1991 } 1992 1993 tmp = CSR_READ_4(sc, IPW_CSR_IO); 1994 CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK | 1995 IPW_IO_GPIO3_MASK); 1996 1997 return 0; 1998 } 1999 2000 static int 2001 ipw_setwepkeys(struct ipw_softc *sc) 2002 { 2003 struct ieee80211com *ic = &sc->sc_ic; 2004 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2005 struct ipw_wep_key wepkey; 2006 struct ieee80211_key *wk; 2007 int error, i; 2008 2009 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2010 wk = &vap->iv_nw_keys[i]; 2011 2012 if (wk->wk_cipher == NULL || 2013 wk->wk_cipher->ic_cipher != IEEE80211_CIPHER_WEP) 2014 continue; 2015 2016 wepkey.idx = i; 2017 wepkey.len = wk->wk_keylen; 2018 memset(wepkey.key, 0, sizeof wepkey.key); 2019 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen); 2020 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx, 2021 wepkey.len)); 2022 error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey, 2023 sizeof wepkey); 2024 if (error != 0) 2025 return error; 2026 } 2027 return 0; 2028 } 2029 2030 static int 2031 ipw_setwpaie(struct ipw_softc *sc, const void *ie, int ielen) 2032 { 2033 struct ipw_wpa_ie wpaie; 2034 2035 memset(&wpaie, 0, sizeof(wpaie)); 2036 wpaie.len = htole32(ielen); 2037 /* XXX verify length */ 2038 memcpy(&wpaie.ie, ie, ielen); 2039 DPRINTF(("Setting WPA IE\n")); 2040 return ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &wpaie, sizeof(wpaie)); 2041 } 2042 2043 static int 2044 ipw_setbssid(struct ipw_softc *sc, uint8_t *bssid) 2045 { 2046 static const uint8_t zerobssid[IEEE80211_ADDR_LEN]; 2047 2048 if (bssid == NULL || bcmp(bssid, zerobssid, IEEE80211_ADDR_LEN) == 0) { 2049 DPRINTF(("Setting mandatory BSSID to null\n")); 2050 return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0); 2051 } else { 2052 DPRINTF(("Setting mandatory BSSID to %6D\n", bssid, ":")); 2053 return ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, 2054 bssid, IEEE80211_ADDR_LEN); 2055 } 2056 } 2057 2058 static int 2059 ipw_setssid(struct ipw_softc *sc, void *ssid, size_t ssidlen) 2060 { 2061 if (ssidlen == 0) { 2062 /* 2063 * A bug in the firmware breaks the ``don't associate'' 2064 * bit in the scan options command. To compensate for 2065 * this install a bogus ssid when no ssid is specified 2066 * so the firmware won't try to associate. 2067 */ 2068 DPRINTF(("Setting bogus ESSID to WAR firmware bug\n")); 2069 return ipw_cmd(sc, IPW_CMD_SET_ESSID, 2070 "\x18\x19\x20\x21\x22\x23\x24\x25\x26\x27" 2071 "\x28\x29\x2a\x2b\x2c\x2d\x2e\x2f\x30\x31" 2072 "\x32\x33\x34\x35\x36\x37\x38\x39\x3a\x3b" 2073 "\x3c\x3d", IEEE80211_NWID_LEN); 2074 } else { 2075 #ifdef IPW_DEBUG 2076 if (ipw_debug > 0) { 2077 printf("Setting ESSID to "); 2078 ieee80211_print_essid(ssid, ssidlen); 2079 printf("\n"); 2080 } 2081 #endif 2082 return ipw_cmd(sc, IPW_CMD_SET_ESSID, ssid, ssidlen); 2083 } 2084 } 2085 2086 static int 2087 ipw_setscanopts(struct ipw_softc *sc, uint32_t chanmask, uint32_t flags) 2088 { 2089 struct ipw_scan_options opts; 2090 2091 DPRINTF(("Scan options: mask 0x%x flags 0x%x\n", chanmask, flags)); 2092 opts.channels = htole32(chanmask); 2093 opts.flags = htole32(flags); 2094 return ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &opts, sizeof(opts)); 2095 } 2096 2097 static int 2098 ipw_scan(struct ipw_softc *sc) 2099 { 2100 uint32_t params; 2101 int error; 2102 2103 DPRINTF(("%s: flags 0x%x\n", __func__, sc->flags)); 2104 2105 if (sc->flags & IPW_FLAG_SCANNING) 2106 return (EBUSY); 2107 sc->flags |= IPW_FLAG_SCANNING | IPW_FLAG_HACK; 2108 2109 /* NB: IPW_SCAN_DO_NOT_ASSOCIATE does not work (we set it anyway) */ 2110 error = ipw_setscanopts(sc, 0x3fff, IPW_SCAN_DO_NOT_ASSOCIATE); 2111 if (error != 0) 2112 goto done; 2113 2114 /* 2115 * Setup null/bogus ssid so firmware doesn't use any previous 2116 * ssid to try and associate. This is because the ``don't 2117 * associate'' option bit is broken (sigh). 2118 */ 2119 error = ipw_setssid(sc, NULL, 0); 2120 if (error != 0) 2121 goto done; 2122 2123 /* 2124 * NB: the adapter may be disabled on association lost; 2125 * if so just re-enable it to kick off scanning. 2126 */ 2127 DPRINTF(("Starting scan\n")); 2128 sc->sc_scan_timer = 3; 2129 if (sc->flags & IPW_FLAG_ENABLED) { 2130 params = 0; /* XXX? */ 2131 error = ipw_cmd(sc, IPW_CMD_BROADCAST_SCAN, 2132 ¶ms, sizeof(params)); 2133 } else 2134 error = ipw_enable(sc); 2135 done: 2136 if (error != 0) { 2137 DPRINTF(("Scan failed\n")); 2138 sc->flags &= ~(IPW_FLAG_SCANNING | IPW_FLAG_HACK); 2139 } 2140 return (error); 2141 } 2142 2143 static int 2144 ipw_setchannel(struct ipw_softc *sc, struct ieee80211_channel *chan) 2145 { 2146 struct ieee80211com *ic = &sc->sc_ic; 2147 uint32_t data; 2148 int error; 2149 2150 data = htole32(ieee80211_chan2ieee(ic, chan)); 2151 DPRINTF(("Setting channel to %u\n", le32toh(data))); 2152 error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data); 2153 if (error == 0) 2154 ipw_setcurchan(sc, chan); 2155 return error; 2156 } 2157 2158 static void 2159 ipw_assoc(struct ieee80211com *ic, struct ieee80211vap *vap) 2160 { 2161 struct ipw_softc *sc = ic->ic_softc; 2162 struct ieee80211_node *ni = vap->iv_bss; 2163 struct ipw_security security; 2164 uint32_t data; 2165 int error; 2166 2167 IPW_LOCK(sc); 2168 error = ipw_disable(sc); 2169 if (error != 0) 2170 goto done; 2171 2172 memset(&security, 0, sizeof security); 2173 security.authmode = (ni->ni_authmode == IEEE80211_AUTH_SHARED) ? 2174 IPW_AUTH_SHARED : IPW_AUTH_OPEN; 2175 security.ciphers = htole32(IPW_CIPHER_NONE); 2176 DPRINTF(("Setting authmode to %u\n", security.authmode)); 2177 error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFO, &security, 2178 sizeof security); 2179 if (error != 0) 2180 goto done; 2181 2182 data = htole32(vap->iv_rtsthreshold); 2183 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data))); 2184 error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data); 2185 if (error != 0) 2186 goto done; 2187 2188 data = htole32(vap->iv_fragthreshold); 2189 DPRINTF(("Setting frag threshold to %u\n", le32toh(data))); 2190 error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data); 2191 if (error != 0) 2192 goto done; 2193 2194 if (vap->iv_flags & IEEE80211_F_PRIVACY) { 2195 error = ipw_setwepkeys(sc); 2196 if (error != 0) 2197 goto done; 2198 2199 if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) { 2200 data = htole32(vap->iv_def_txkey); 2201 DPRINTF(("Setting wep tx key index to %u\n", 2202 le32toh(data))); 2203 error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data, 2204 sizeof data); 2205 if (error != 0) 2206 goto done; 2207 } 2208 } 2209 2210 data = htole32((vap->iv_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0); 2211 DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data))); 2212 error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data); 2213 if (error != 0) 2214 goto done; 2215 2216 error = ipw_setssid(sc, ni->ni_essid, ni->ni_esslen); 2217 if (error != 0) 2218 goto done; 2219 2220 error = ipw_setbssid(sc, ni->ni_bssid); 2221 if (error != 0) 2222 goto done; 2223 2224 if (vap->iv_appie_wpa != NULL) { 2225 struct ieee80211_appie *ie = vap->iv_appie_wpa; 2226 error = ipw_setwpaie(sc, ie->ie_data, ie->ie_len); 2227 if (error != 0) 2228 goto done; 2229 } 2230 if (ic->ic_opmode == IEEE80211_M_IBSS) { 2231 error = ipw_setchannel(sc, ni->ni_chan); 2232 if (error != 0) 2233 goto done; 2234 } 2235 2236 /* lock scan to ap's channel and enable associate */ 2237 error = ipw_setscanopts(sc, 2238 1<<(ieee80211_chan2ieee(ic, ni->ni_chan)-1), 0); 2239 if (error != 0) 2240 goto done; 2241 2242 error = ipw_enable(sc); /* finally, enable adapter */ 2243 if (error == 0) 2244 sc->flags |= IPW_FLAG_ASSOCIATING; 2245 done: 2246 IPW_UNLOCK(sc); 2247 } 2248 2249 static void 2250 ipw_disassoc(struct ieee80211com *ic, struct ieee80211vap *vap) 2251 { 2252 struct ieee80211_node *ni = vap->iv_bss; 2253 struct ipw_softc *sc = ic->ic_softc; 2254 2255 IPW_LOCK(sc); 2256 DPRINTF(("Disassociate from %6D\n", ni->ni_bssid, ":")); 2257 /* 2258 * NB: don't try to do this if ipw_stop_master has 2259 * shutdown the firmware and disabled interrupts. 2260 */ 2261 if (sc->flags & IPW_FLAG_FW_INITED) { 2262 sc->flags &= ~IPW_FLAG_ASSOCIATED; 2263 /* 2264 * NB: firmware currently ignores bssid parameter, but 2265 * supply it in case this changes (follow linux driver). 2266 */ 2267 (void) ipw_cmd(sc, IPW_CMD_DISASSOCIATE, 2268 ni->ni_bssid, IEEE80211_ADDR_LEN); 2269 } 2270 IPW_UNLOCK(sc); 2271 } 2272 2273 /* 2274 * Handler for sc_init_task. This is a simple wrapper around ipw_init(). 2275 * It is called on firmware panics or on watchdog timeouts. 2276 */ 2277 static void 2278 ipw_init_task(void *context, int pending) 2279 { 2280 ipw_init(context); 2281 } 2282 2283 static void 2284 ipw_init(void *priv) 2285 { 2286 struct ipw_softc *sc = priv; 2287 struct ieee80211com *ic = &sc->sc_ic; 2288 2289 IPW_LOCK(sc); 2290 ipw_init_locked(sc); 2291 IPW_UNLOCK(sc); 2292 2293 if (sc->flags & IPW_FLAG_RUNNING) 2294 ieee80211_start_all(ic); /* start all vap's */ 2295 } 2296 2297 static void 2298 ipw_init_locked(struct ipw_softc *sc) 2299 { 2300 struct ieee80211com *ic = &sc->sc_ic; 2301 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2302 const struct firmware *fp; 2303 const struct ipw_firmware_hdr *hdr; 2304 const char *fw; 2305 2306 IPW_LOCK_ASSERT(sc); 2307 2308 DPRINTF(("%s: state %s flags 0x%x\n", __func__, 2309 ieee80211_state_name[vap->iv_state], sc->flags)); 2310 2311 /* 2312 * Avoid re-entrant calls. We need to release the mutex in ipw_init() 2313 * when loading the firmware and we don't want to be called during this 2314 * operation. 2315 */ 2316 if (sc->flags & IPW_FLAG_INIT_LOCKED) 2317 return; 2318 sc->flags |= IPW_FLAG_INIT_LOCKED; 2319 2320 ipw_stop_locked(sc); 2321 2322 if (ipw_reset(sc) != 0) { 2323 device_printf(sc->sc_dev, "could not reset adapter\n"); 2324 goto fail; 2325 } 2326 2327 if (sc->sc_firmware == NULL) { 2328 device_printf(sc->sc_dev, "no firmware\n"); 2329 goto fail; 2330 } 2331 /* NB: consistency already checked on load */ 2332 fp = sc->sc_firmware; 2333 hdr = (const struct ipw_firmware_hdr *)fp->data; 2334 2335 DPRINTF(("Loading firmware image '%s'\n", fp->name)); 2336 fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz); 2337 if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) { 2338 device_printf(sc->sc_dev, "could not load microcode\n"); 2339 goto fail; 2340 } 2341 2342 ipw_stop_master(sc); 2343 2344 /* 2345 * Setup tx, rx and status rings. 2346 */ 2347 sc->txold = IPW_NTBD - 1; 2348 sc->txcur = 0; 2349 sc->txfree = IPW_NTBD - 2; 2350 sc->rxcur = IPW_NRBD - 1; 2351 2352 CSR_WRITE_4(sc, IPW_CSR_TX_BASE, sc->tbd_phys); 2353 CSR_WRITE_4(sc, IPW_CSR_TX_SIZE, IPW_NTBD); 2354 CSR_WRITE_4(sc, IPW_CSR_TX_READ, 0); 2355 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur); 2356 2357 CSR_WRITE_4(sc, IPW_CSR_RX_BASE, sc->rbd_phys); 2358 CSR_WRITE_4(sc, IPW_CSR_RX_SIZE, IPW_NRBD); 2359 CSR_WRITE_4(sc, IPW_CSR_RX_READ, 0); 2360 CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur); 2361 2362 CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys); 2363 2364 fw = (const char *)fp->data + sizeof *hdr; 2365 if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) { 2366 device_printf(sc->sc_dev, "could not load firmware\n"); 2367 goto fail; 2368 } 2369 2370 sc->flags |= IPW_FLAG_FW_INITED; 2371 2372 /* retrieve information tables base addresses */ 2373 sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE); 2374 sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE); 2375 2376 ipw_write_table1(sc, IPW_INFO_LOCK, 0); 2377 2378 if (ipw_config(sc) != 0) { 2379 device_printf(sc->sc_dev, "device configuration failed\n"); 2380 goto fail; 2381 } 2382 2383 callout_reset(&sc->sc_wdtimer, hz, ipw_watchdog, sc); 2384 sc->flags |= IPW_FLAG_RUNNING; 2385 sc->flags &= ~IPW_FLAG_INIT_LOCKED; 2386 return; 2387 2388 fail: 2389 ipw_stop_locked(sc); 2390 sc->flags &= ~IPW_FLAG_INIT_LOCKED; 2391 } 2392 2393 static int 2394 ipw_config(struct ipw_softc *sc) 2395 { 2396 struct ieee80211com *ic = &sc->sc_ic; 2397 struct ipw_configuration config; 2398 uint32_t data; 2399 int error; 2400 2401 error = ipw_disable(sc); 2402 if (error != 0) 2403 return error; 2404 2405 switch (ic->ic_opmode) { 2406 case IEEE80211_M_STA: 2407 case IEEE80211_M_HOSTAP: 2408 case IEEE80211_M_WDS: /* XXX */ 2409 data = htole32(IPW_MODE_BSS); 2410 break; 2411 case IEEE80211_M_IBSS: 2412 case IEEE80211_M_AHDEMO: 2413 data = htole32(IPW_MODE_IBSS); 2414 break; 2415 case IEEE80211_M_MONITOR: 2416 data = htole32(IPW_MODE_MONITOR); 2417 break; 2418 default: 2419 device_printf(sc->sc_dev, "unknown opmode %d\n", ic->ic_opmode); 2420 return EINVAL; 2421 } 2422 DPRINTF(("Setting mode to %u\n", le32toh(data))); 2423 error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data); 2424 if (error != 0) 2425 return error; 2426 2427 if (ic->ic_opmode == IEEE80211_M_IBSS || 2428 ic->ic_opmode == IEEE80211_M_MONITOR) { 2429 error = ipw_setchannel(sc, ic->ic_curchan); 2430 if (error != 0) 2431 return error; 2432 } 2433 2434 if (ic->ic_opmode == IEEE80211_M_MONITOR) 2435 return ipw_enable(sc); 2436 2437 config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK | 2438 IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE); 2439 if (ic->ic_opmode == IEEE80211_M_IBSS) 2440 config.flags |= htole32(IPW_CFG_IBSS_AUTO_START); 2441 if (ic->ic_promisc > 0) 2442 config.flags |= htole32(IPW_CFG_PROMISCUOUS); 2443 config.bss_chan = htole32(0x3fff); /* channels 1-14 */ 2444 config.ibss_chan = htole32(0x7ff); /* channels 1-11 */ 2445 DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags))); 2446 error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config); 2447 if (error != 0) 2448 return error; 2449 2450 data = htole32(0xf); /* 1, 2, 5.5, 11 */ 2451 DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data))); 2452 error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data); 2453 if (error != 0) 2454 return error; 2455 2456 /* Use the same rate set */ 2457 DPRINTF(("Setting msdu tx rates to 0x%x\n", le32toh(data))); 2458 error = ipw_cmd(sc, IPW_CMD_SET_MSDU_TX_RATES, &data, sizeof data); 2459 if (error != 0) 2460 return error; 2461 2462 /* Use the same rate set */ 2463 DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data))); 2464 error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data); 2465 if (error != 0) 2466 return error; 2467 2468 data = htole32(IPW_POWER_MODE_CAM); 2469 DPRINTF(("Setting power mode to %u\n", le32toh(data))); 2470 error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data); 2471 if (error != 0) 2472 return error; 2473 2474 if (ic->ic_opmode == IEEE80211_M_IBSS) { 2475 data = htole32(32); /* default value */ 2476 DPRINTF(("Setting tx power index to %u\n", le32toh(data))); 2477 error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data, 2478 sizeof data); 2479 if (error != 0) 2480 return error; 2481 } 2482 2483 return 0; 2484 } 2485 2486 static void 2487 ipw_stop(void *priv) 2488 { 2489 struct ipw_softc *sc = priv; 2490 2491 IPW_LOCK(sc); 2492 ipw_stop_locked(sc); 2493 IPW_UNLOCK(sc); 2494 } 2495 2496 static void 2497 ipw_stop_locked(struct ipw_softc *sc) 2498 { 2499 int i; 2500 2501 IPW_LOCK_ASSERT(sc); 2502 2503 callout_stop(&sc->sc_wdtimer); 2504 ipw_stop_master(sc); 2505 2506 CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET); 2507 2508 /* 2509 * Release tx buffers. 2510 */ 2511 for (i = 0; i < IPW_NTBD; i++) 2512 ipw_release_sbd(sc, &sc->stbd_list[i]); 2513 2514 sc->sc_tx_timer = 0; 2515 sc->flags &= ~IPW_FLAG_RUNNING; 2516 } 2517 2518 static int 2519 ipw_sysctl_stats(SYSCTL_HANDLER_ARGS) 2520 { 2521 struct ipw_softc *sc = arg1; 2522 uint32_t i, size, buf[256]; 2523 2524 memset(buf, 0, sizeof buf); 2525 2526 if (!(sc->flags & IPW_FLAG_FW_INITED)) 2527 return SYSCTL_OUT(req, buf, sizeof buf); 2528 2529 CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base); 2530 2531 size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256); 2532 for (i = 1; i < size; i++) 2533 buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA)); 2534 2535 return SYSCTL_OUT(req, buf, size); 2536 } 2537 2538 static int 2539 ipw_sysctl_radio(SYSCTL_HANDLER_ARGS) 2540 { 2541 struct ipw_softc *sc = arg1; 2542 int val; 2543 2544 val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) && 2545 (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED)); 2546 2547 return SYSCTL_OUT(req, &val, sizeof val); 2548 } 2549 2550 static uint32_t 2551 ipw_read_table1(struct ipw_softc *sc, uint32_t off) 2552 { 2553 return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off)); 2554 } 2555 2556 static void 2557 ipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info) 2558 { 2559 MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info); 2560 } 2561 2562 #if 0 2563 static int 2564 ipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len) 2565 { 2566 uint32_t addr, info; 2567 uint16_t count, size; 2568 uint32_t total; 2569 2570 /* addr[4] + count[2] + size[2] */ 2571 addr = MEM_READ_4(sc, sc->table2_base + off); 2572 info = MEM_READ_4(sc, sc->table2_base + off + 4); 2573 2574 count = info >> 16; 2575 size = info & 0xffff; 2576 total = count * size; 2577 2578 if (total > *len) { 2579 *len = total; 2580 return EINVAL; 2581 } 2582 2583 *len = total; 2584 ipw_read_mem_1(sc, addr, buf, total); 2585 2586 return 0; 2587 } 2588 2589 static void 2590 ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap, 2591 bus_size_t count) 2592 { 2593 for (; count > 0; offset++, datap++, count--) { 2594 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3); 2595 *datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3)); 2596 } 2597 } 2598 #endif 2599 2600 static void 2601 ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap, 2602 bus_size_t count) 2603 { 2604 for (; count > 0; offset++, datap++, count--) { 2605 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3); 2606 CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap); 2607 } 2608 } 2609 2610 static void 2611 ipw_scan_start(struct ieee80211com *ic) 2612 { 2613 struct ipw_softc *sc = ic->ic_softc; 2614 2615 IPW_LOCK(sc); 2616 ipw_scan(sc); 2617 IPW_UNLOCK(sc); 2618 } 2619 2620 static void 2621 ipw_set_channel(struct ieee80211com *ic) 2622 { 2623 struct ipw_softc *sc = ic->ic_softc; 2624 2625 IPW_LOCK(sc); 2626 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 2627 ipw_disable(sc); 2628 ipw_setchannel(sc, ic->ic_curchan); 2629 ipw_enable(sc); 2630 } 2631 IPW_UNLOCK(sc); 2632 } 2633 2634 static void 2635 ipw_scan_curchan(struct ieee80211_scan_state *ss, unsigned long maxdwell) 2636 { 2637 /* NB: all channels are scanned at once */ 2638 } 2639 2640 static void 2641 ipw_scan_mindwell(struct ieee80211_scan_state *ss) 2642 { 2643 /* NB: don't try to abort scan; wait for firmware to finish */ 2644 } 2645 2646 static void 2647 ipw_scan_end(struct ieee80211com *ic) 2648 { 2649 struct ipw_softc *sc = ic->ic_softc; 2650 2651 IPW_LOCK(sc); 2652 sc->flags &= ~IPW_FLAG_SCANNING; 2653 IPW_UNLOCK(sc); 2654 } 2655