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