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