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