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