if_ipw.c revision 172058
1/* $FreeBSD: head/sys/dev/ipw/if_ipw.c 172058 2007-09-05 21:31:32Z sam $ */ 2 3/*- 4 * Copyright (c) 2004-2006 5 * Damien Bergamini <damien.bergamini@free.fr>. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice unmodified, this list of conditions, and the following 12 * disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> 31__FBSDID("$FreeBSD: head/sys/dev/ipw/if_ipw.c 172058 2007-09-05 21:31:32Z sam $"); 32 33/*- 34 * Intel(R) PRO/Wireless 2100 MiniPCI driver 35 * http://www.intel.com/network/connectivity/products/wireless/prowireless_mobile.htm 36 */ 37 38#include <sys/param.h> 39#include <sys/sysctl.h> 40#include <sys/sockio.h> 41#include <sys/mbuf.h> 42#include <sys/kernel.h> 43#include <sys/socket.h> 44#include <sys/systm.h> 45#include <sys/malloc.h> 46#include <sys/queue.h> 47#include <sys/taskqueue.h> 48#include <sys/module.h> 49#include <sys/bus.h> 50#include <sys/endian.h> 51#include <sys/linker.h> 52#include <sys/firmware.h> 53 54#include <machine/bus.h> 55#include <machine/resource.h> 56#include <sys/rman.h> 57 58#include <dev/pci/pcireg.h> 59#include <dev/pci/pcivar.h> 60 61#include <net/bpf.h> 62#include <net/if.h> 63#include <net/if_arp.h> 64#include <net/ethernet.h> 65#include <net/if_dl.h> 66#include <net/if_media.h> 67#include <net/if_types.h> 68 69#include <net80211/ieee80211_var.h> 70#include <net80211/ieee80211_radiotap.h> 71 72#include <netinet/in.h> 73#include <netinet/in_systm.h> 74#include <netinet/in_var.h> 75#include <netinet/ip.h> 76#include <netinet/if_ether.h> 77 78#include <dev/ipw/if_ipwreg.h> 79#include <dev/ipw/if_ipwvar.h> 80 81#ifdef IPW_DEBUG 82#define DPRINTF(x) do { if (ipw_debug > 0) printf x; } while (0) 83#define DPRINTFN(n, x) do { if (ipw_debug >= (n)) printf x; } while (0) 84int ipw_debug = 0; 85SYSCTL_INT(_debug, OID_AUTO, ipw, CTLFLAG_RW, &ipw_debug, 0, "ipw debug level"); 86#else 87#define DPRINTF(x) 88#define DPRINTFN(n, x) 89#endif 90 91MODULE_DEPEND(ipw, pci, 1, 1, 1); 92MODULE_DEPEND(ipw, wlan, 1, 1, 1); 93MODULE_DEPEND(ipw, firmware, 1, 1, 1); 94 95struct ipw_ident { 96 uint16_t vendor; 97 uint16_t device; 98 const char *name; 99}; 100 101static const struct ipw_ident ipw_ident_table[] = { 102 { 0x8086, 0x1043, "Intel(R) PRO/Wireless 2100 MiniPCI" }, 103 104 { 0, 0, NULL } 105}; 106 107static int ipw_dma_alloc(struct ipw_softc *); 108static void ipw_release(struct ipw_softc *); 109static int ipw_media_change(struct ifnet *); 110static void ipw_media_status(struct ifnet *, struct ifmediareq *); 111static int ipw_newstate(struct ieee80211com *, enum ieee80211_state, int); 112static uint16_t ipw_read_prom_word(struct ipw_softc *, uint8_t); 113static void ipw_command_intr(struct ipw_softc *, struct ipw_soft_buf *); 114static void ipw_newstate_intr(struct ipw_softc *, struct ipw_soft_buf *); 115static void ipw_data_intr(struct ipw_softc *, struct ipw_status *, 116 struct ipw_soft_bd *, struct ipw_soft_buf *); 117static void ipw_rx_intr(struct ipw_softc *); 118static void ipw_release_sbd(struct ipw_softc *, struct ipw_soft_bd *); 119static void ipw_tx_intr(struct ipw_softc *); 120static void ipw_intr(void *); 121static void ipw_dma_map_addr(void *, bus_dma_segment_t *, int, int); 122static int ipw_cmd(struct ipw_softc *, uint32_t, void *, uint32_t); 123static int ipw_tx_start(struct ifnet *, struct mbuf *, 124 struct ieee80211_node *); 125static void ipw_start(struct ifnet *); 126static void ipw_watchdog(struct ifnet *); 127static int ipw_ioctl(struct ifnet *, u_long, caddr_t); 128static void ipw_stop_master(struct ipw_softc *); 129static int ipw_reset(struct ipw_softc *); 130static int ipw_load_ucode(struct ipw_softc *, const char *, int); 131static int ipw_load_firmware(struct ipw_softc *, const char *, int); 132static int ipw_config(struct ipw_softc *); 133static void ipw_init_task(void *, int); 134static void ipw_init(void *); 135static void ipw_stop(void *); 136static int ipw_sysctl_stats(SYSCTL_HANDLER_ARGS); 137static int ipw_sysctl_radio(SYSCTL_HANDLER_ARGS); 138static uint32_t ipw_read_table1(struct ipw_softc *, uint32_t); 139static void ipw_write_table1(struct ipw_softc *, uint32_t, uint32_t); 140static int ipw_read_table2(struct ipw_softc *, uint32_t, void *, 141 uint32_t *); 142static void ipw_read_mem_1(struct ipw_softc *, bus_size_t, uint8_t *, 143 bus_size_t); 144static void ipw_write_mem_1(struct ipw_softc *, bus_size_t, 145 const uint8_t *, bus_size_t); 146 147static int ipw_probe(device_t); 148static int ipw_attach(device_t); 149static int ipw_detach(device_t); 150static int ipw_shutdown(device_t); 151static int ipw_suspend(device_t); 152static int ipw_resume(device_t); 153 154static device_method_t ipw_methods[] = { 155 /* Device interface */ 156 DEVMETHOD(device_probe, ipw_probe), 157 DEVMETHOD(device_attach, ipw_attach), 158 DEVMETHOD(device_detach, ipw_detach), 159 DEVMETHOD(device_shutdown, ipw_shutdown), 160 DEVMETHOD(device_suspend, ipw_suspend), 161 DEVMETHOD(device_resume, ipw_resume), 162 163 { 0, 0 } 164}; 165 166static driver_t ipw_driver = { 167 "ipw", 168 ipw_methods, 169 sizeof (struct ipw_softc) 170}; 171 172static devclass_t ipw_devclass; 173 174DRIVER_MODULE(ipw, pci, ipw_driver, ipw_devclass, 0, 0); 175 176static int 177ipw_probe(device_t dev) 178{ 179 const struct ipw_ident *ident; 180 181 for (ident = ipw_ident_table; ident->name != NULL; ident++) { 182 if (pci_get_vendor(dev) == ident->vendor && 183 pci_get_device(dev) == ident->device) { 184 device_set_desc(dev, ident->name); 185 return 0; 186 } 187 } 188 return ENXIO; 189} 190 191/* Base Address Register */ 192#define IPW_PCI_BAR0 0x10 193 194static int 195ipw_attach(device_t dev) 196{ 197 struct ipw_softc *sc = device_get_softc(dev); 198 struct ifnet *ifp; 199 struct ieee80211com *ic = &sc->sc_ic; 200 struct ieee80211_channel *c; 201 uint16_t val; 202 int error, i; 203 204 sc->sc_dev = dev; 205 206 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 207 MTX_DEF | MTX_RECURSE); 208 209 TASK_INIT(&sc->sc_init_task, 0, ipw_init_task, sc); 210 211 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 212 device_printf(dev, "chip is in D%d power mode " 213 "-- setting to D0\n", pci_get_powerstate(dev)); 214 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 215 } 216 217 pci_write_config(dev, 0x41, 0, 1); 218 219 /* enable bus-mastering */ 220 pci_enable_busmaster(dev); 221 222 sc->mem_rid = IPW_PCI_BAR0; 223 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid, 224 RF_ACTIVE); 225 if (sc->mem == NULL) { 226 device_printf(dev, "could not allocate memory resource\n"); 227 goto fail; 228 } 229 230 sc->sc_st = rman_get_bustag(sc->mem); 231 sc->sc_sh = rman_get_bushandle(sc->mem); 232 233 sc->irq_rid = 0; 234 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 235 RF_ACTIVE | RF_SHAREABLE); 236 if (sc->irq == NULL) { 237 device_printf(dev, "could not allocate interrupt resource\n"); 238 goto fail; 239 } 240 241 if (ipw_reset(sc) != 0) { 242 device_printf(dev, "could not reset adapter\n"); 243 goto fail; 244 } 245 246 if (ipw_dma_alloc(sc) != 0) { 247 device_printf(dev, "could not allocate DMA resources\n"); 248 goto fail; 249 } 250 251 ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 252 if (ifp == NULL) { 253 device_printf(dev, "can not if_alloc()\n"); 254 goto fail; 255 } 256 257 ifp->if_softc = sc; 258 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 259 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 260 ifp->if_init = ipw_init; 261 ifp->if_ioctl = ipw_ioctl; 262 ifp->if_start = ipw_start; 263 ifp->if_watchdog = ipw_watchdog; 264 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 265 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 266 IFQ_SET_READY(&ifp->if_snd); 267 268 ic->ic_ifp = ifp; 269 ic->ic_phytype = IEEE80211_T_DS; 270 ic->ic_opmode = IEEE80211_M_STA; 271 ic->ic_state = IEEE80211_S_INIT; 272 273 /* set device capabilities */ 274 ic->ic_caps = 275 IEEE80211_C_IBSS | /* IBSS mode supported */ 276 IEEE80211_C_MONITOR | /* monitor mode supported */ 277 IEEE80211_C_TXPMGT | /* tx power management */ 278 IEEE80211_C_SHPREAMBLE; /* short preamble supported */ 279 280 /* read MAC address from EEPROM */ 281 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 0); 282 ic->ic_myaddr[0] = val >> 8; 283 ic->ic_myaddr[1] = val & 0xff; 284 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 1); 285 ic->ic_myaddr[2] = val >> 8; 286 ic->ic_myaddr[3] = val & 0xff; 287 val = ipw_read_prom_word(sc, IPW_EEPROM_MAC + 2); 288 ic->ic_myaddr[4] = val >> 8; 289 ic->ic_myaddr[5] = val & 0xff; 290 291 /* set supported .11b channels (read from EEPROM) */ 292 if ((val = ipw_read_prom_word(sc, IPW_EEPROM_CHANNEL_LIST)) == 0) 293 val = 0x7ff; /* default to channels 1-11 */ 294 val <<= 1; 295 for (i = 1; i < 16; i++) { 296 if (val & (1 << i)) { 297 c = &ic->ic_channels[ic->ic_nchans++]; 298 c->ic_freq = ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 299 c->ic_flags = IEEE80211_CHAN_B; 300 c->ic_ieee = i; 301 } 302 } 303 304 /* check support for radio transmitter switch in EEPROM */ 305 if (!(ipw_read_prom_word(sc, IPW_EEPROM_RADIO) & 8)) 306 sc->flags |= IPW_FLAG_HAS_RADIO_SWITCH; 307 308 ieee80211_ifattach(ic); 309 /* override state transition machine */ 310 sc->sc_newstate = ic->ic_newstate; 311 ic->ic_newstate = ipw_newstate; 312 ieee80211_media_init(ic, ipw_media_change, ipw_media_status); 313 314 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 315 sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap), 316 &sc->sc_drvbpf); 317 318 sc->sc_rxtap_len = sizeof sc->sc_rxtap; 319 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 320 sc->sc_rxtap.wr_ihdr.it_present = htole32(IPW_RX_RADIOTAP_PRESENT); 321 322 sc->sc_txtap_len = sizeof sc->sc_txtap; 323 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 324 sc->sc_txtap.wt_ihdr.it_present = htole32(IPW_TX_RADIOTAP_PRESENT); 325 326 /* 327 * Add a few sysctl knobs. 328 */ 329 sc->dwelltime = 100; 330 331 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 332 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio", 333 CTLTYPE_INT | CTLFLAG_RD, sc, 0, ipw_sysctl_radio, "I", 334 "radio transmitter switch state (0=off, 1=on)"); 335 336 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 337 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats", 338 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, ipw_sysctl_stats, "S", 339 "statistics"); 340 341 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 342 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "dwell", 343 CTLFLAG_RW, &sc->dwelltime, 0, 344 "channel dwell time (ms) for AP/station scanning"); 345 346 /* 347 * Hook our interrupt after all initialization is complete. 348 */ 349 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 350 NULL, ipw_intr, sc, &sc->sc_ih); 351 if (error != 0) { 352 device_printf(dev, "could not set up interrupt\n"); 353 goto fail; 354 } 355 356 if (bootverbose) 357 ieee80211_announce(ic); 358 359 return 0; 360 361fail: ipw_detach(dev); 362 return ENXIO; 363} 364 365static int 366ipw_detach(device_t dev) 367{ 368 struct ipw_softc *sc = device_get_softc(dev); 369 struct ieee80211com *ic = &sc->sc_ic; 370 struct ifnet *ifp = ic->ic_ifp; 371 372 ipw_stop(sc); 373 374 if (ifp != NULL) { 375 bpfdetach(ifp); 376 ieee80211_ifdetach(ic); 377 } 378 379 ipw_release(sc); 380 381 if (sc->irq != NULL) { 382 bus_teardown_intr(dev, sc->irq, sc->sc_ih); 383 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 384 } 385 386 if (sc->mem != NULL) 387 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 388 389 if (ifp != NULL) 390 if_free(ifp); 391 392 if (sc->sc_firmware != NULL) { 393 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD); 394 sc->sc_firmware = NULL; 395 } 396 397 mtx_destroy(&sc->sc_mtx); 398 399 return 0; 400} 401 402static int 403ipw_dma_alloc(struct ipw_softc *sc) 404{ 405 struct ipw_soft_bd *sbd; 406 struct ipw_soft_hdr *shdr; 407 struct ipw_soft_buf *sbuf; 408 bus_addr_t physaddr; 409 int error, i; 410 411 /* 412 * Allocate and map tx ring. 413 */ 414 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 415 BUS_SPACE_MAXADDR, NULL, NULL, IPW_TBD_SZ, 1, IPW_TBD_SZ, 0, NULL, 416 NULL, &sc->tbd_dmat); 417 if (error != 0) { 418 device_printf(sc->sc_dev, "could not create tx ring DMA tag\n"); 419 goto fail; 420 } 421 422 error = bus_dmamem_alloc(sc->tbd_dmat, (void **)&sc->tbd_list, 423 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->tbd_map); 424 if (error != 0) { 425 device_printf(sc->sc_dev, 426 "could not allocate tx ring DMA memory\n"); 427 goto fail; 428 } 429 430 error = bus_dmamap_load(sc->tbd_dmat, sc->tbd_map, sc->tbd_list, 431 IPW_TBD_SZ, ipw_dma_map_addr, &sc->tbd_phys, 0); 432 if (error != 0) { 433 device_printf(sc->sc_dev, "could not map tx ring DMA memory\n"); 434 goto fail; 435 } 436 437 /* 438 * Allocate and map rx ring. 439 */ 440 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 441 BUS_SPACE_MAXADDR, NULL, NULL, IPW_RBD_SZ, 1, IPW_RBD_SZ, 0, NULL, 442 NULL, &sc->rbd_dmat); 443 if (error != 0) { 444 device_printf(sc->sc_dev, "could not create rx ring DMA tag\n"); 445 goto fail; 446 } 447 448 error = bus_dmamem_alloc(sc->rbd_dmat, (void **)&sc->rbd_list, 449 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->rbd_map); 450 if (error != 0) { 451 device_printf(sc->sc_dev, 452 "could not allocate rx ring DMA memory\n"); 453 goto fail; 454 } 455 456 error = bus_dmamap_load(sc->rbd_dmat, sc->rbd_map, sc->rbd_list, 457 IPW_RBD_SZ, ipw_dma_map_addr, &sc->rbd_phys, 0); 458 if (error != 0) { 459 device_printf(sc->sc_dev, "could not map rx ring DMA memory\n"); 460 goto fail; 461 } 462 463 /* 464 * Allocate and map status ring. 465 */ 466 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 467 BUS_SPACE_MAXADDR, NULL, NULL, IPW_STATUS_SZ, 1, IPW_STATUS_SZ, 0, 468 NULL, NULL, &sc->status_dmat); 469 if (error != 0) { 470 device_printf(sc->sc_dev, 471 "could not create status ring DMA tag\n"); 472 goto fail; 473 } 474 475 error = bus_dmamem_alloc(sc->status_dmat, (void **)&sc->status_list, 476 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &sc->status_map); 477 if (error != 0) { 478 device_printf(sc->sc_dev, 479 "could not allocate status ring DMA memory\n"); 480 goto fail; 481 } 482 483 error = bus_dmamap_load(sc->status_dmat, sc->status_map, 484 sc->status_list, IPW_STATUS_SZ, ipw_dma_map_addr, &sc->status_phys, 485 0); 486 if (error != 0) { 487 device_printf(sc->sc_dev, 488 "could not map status ring DMA memory\n"); 489 goto fail; 490 } 491 492 /* 493 * Allocate command DMA map. 494 */ 495 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, 496 BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_cmd), 1, 497 sizeof (struct ipw_cmd), 0, NULL, NULL, &sc->cmd_dmat); 498 if (error != 0) { 499 device_printf(sc->sc_dev, "could not create command DMA tag\n"); 500 goto fail; 501 } 502 503 error = bus_dmamap_create(sc->cmd_dmat, 0, &sc->cmd_map); 504 if (error != 0) { 505 device_printf(sc->sc_dev, 506 "could not create command DMA map\n"); 507 goto fail; 508 } 509 510 /* 511 * Allocate headers DMA maps. 512 */ 513 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, 514 BUS_SPACE_MAXADDR, NULL, NULL, sizeof (struct ipw_hdr), 1, 515 sizeof (struct ipw_hdr), 0, NULL, NULL, &sc->hdr_dmat); 516 if (error != 0) { 517 device_printf(sc->sc_dev, "could not create header DMA tag\n"); 518 goto fail; 519 } 520 521 SLIST_INIT(&sc->free_shdr); 522 for (i = 0; i < IPW_NDATA; i++) { 523 shdr = &sc->shdr_list[i]; 524 error = bus_dmamap_create(sc->hdr_dmat, 0, &shdr->map); 525 if (error != 0) { 526 device_printf(sc->sc_dev, 527 "could not create header DMA map\n"); 528 goto fail; 529 } 530 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next); 531 } 532 533 /* 534 * Allocate tx buffers DMA maps. 535 */ 536 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, 537 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, IPW_MAX_NSEG, MCLBYTES, 0, 538 NULL, NULL, &sc->txbuf_dmat); 539 if (error != 0) { 540 device_printf(sc->sc_dev, "could not create tx DMA tag\n"); 541 goto fail; 542 } 543 544 SLIST_INIT(&sc->free_sbuf); 545 for (i = 0; i < IPW_NDATA; i++) { 546 sbuf = &sc->tx_sbuf_list[i]; 547 error = bus_dmamap_create(sc->txbuf_dmat, 0, &sbuf->map); 548 if (error != 0) { 549 device_printf(sc->sc_dev, 550 "could not create tx DMA map\n"); 551 goto fail; 552 } 553 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next); 554 } 555 556 /* 557 * Initialize tx ring. 558 */ 559 for (i = 0; i < IPW_NTBD; i++) { 560 sbd = &sc->stbd_list[i]; 561 sbd->bd = &sc->tbd_list[i]; 562 sbd->type = IPW_SBD_TYPE_NOASSOC; 563 } 564 565 /* 566 * Pre-allocate rx buffers and DMA maps. 567 */ 568 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, 569 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL, 570 NULL, &sc->rxbuf_dmat); 571 if (error != 0) { 572 device_printf(sc->sc_dev, "could not create rx DMA tag\n"); 573 goto fail; 574 } 575 576 for (i = 0; i < IPW_NRBD; i++) { 577 sbd = &sc->srbd_list[i]; 578 sbuf = &sc->rx_sbuf_list[i]; 579 sbd->bd = &sc->rbd_list[i]; 580 581 sbuf->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 582 if (sbuf->m == NULL) { 583 device_printf(sc->sc_dev, 584 "could not allocate rx mbuf\n"); 585 error = ENOMEM; 586 goto fail; 587 } 588 589 error = bus_dmamap_create(sc->rxbuf_dmat, 0, &sbuf->map); 590 if (error != 0) { 591 device_printf(sc->sc_dev, 592 "could not create rx DMA map\n"); 593 goto fail; 594 } 595 596 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, 597 mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr, 598 &physaddr, 0); 599 if (error != 0) { 600 device_printf(sc->sc_dev, 601 "could not map rx DMA memory\n"); 602 goto fail; 603 } 604 605 sbd->type = IPW_SBD_TYPE_DATA; 606 sbd->priv = sbuf; 607 sbd->bd->physaddr = htole32(physaddr); 608 sbd->bd->len = htole32(MCLBYTES); 609 } 610 611 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE); 612 613 return 0; 614 615fail: ipw_release(sc); 616 return error; 617} 618 619static void 620ipw_release(struct ipw_softc *sc) 621{ 622 struct ipw_soft_buf *sbuf; 623 int i; 624 625 if (sc->tbd_dmat != NULL) { 626 if (sc->stbd_list != NULL) { 627 bus_dmamap_unload(sc->tbd_dmat, sc->tbd_map); 628 bus_dmamem_free(sc->tbd_dmat, sc->tbd_list, 629 sc->tbd_map); 630 } 631 bus_dma_tag_destroy(sc->tbd_dmat); 632 } 633 634 if (sc->rbd_dmat != NULL) { 635 if (sc->rbd_list != NULL) { 636 bus_dmamap_unload(sc->rbd_dmat, sc->rbd_map); 637 bus_dmamem_free(sc->rbd_dmat, sc->rbd_list, 638 sc->rbd_map); 639 } 640 bus_dma_tag_destroy(sc->rbd_dmat); 641 } 642 643 if (sc->status_dmat != NULL) { 644 if (sc->status_list != NULL) { 645 bus_dmamap_unload(sc->status_dmat, sc->status_map); 646 bus_dmamem_free(sc->status_dmat, sc->status_list, 647 sc->status_map); 648 } 649 bus_dma_tag_destroy(sc->status_dmat); 650 } 651 652 for (i = 0; i < IPW_NTBD; i++) 653 ipw_release_sbd(sc, &sc->stbd_list[i]); 654 655 if (sc->cmd_dmat != NULL) { 656 bus_dmamap_destroy(sc->cmd_dmat, sc->cmd_map); 657 bus_dma_tag_destroy(sc->cmd_dmat); 658 } 659 660 if (sc->hdr_dmat != NULL) { 661 for (i = 0; i < IPW_NDATA; i++) 662 bus_dmamap_destroy(sc->hdr_dmat, sc->shdr_list[i].map); 663 bus_dma_tag_destroy(sc->hdr_dmat); 664 } 665 666 if (sc->txbuf_dmat != NULL) { 667 for (i = 0; i < IPW_NDATA; i++) { 668 bus_dmamap_destroy(sc->txbuf_dmat, 669 sc->tx_sbuf_list[i].map); 670 } 671 bus_dma_tag_destroy(sc->txbuf_dmat); 672 } 673 674 if (sc->rxbuf_dmat != NULL) { 675 for (i = 0; i < IPW_NRBD; i++) { 676 sbuf = &sc->rx_sbuf_list[i]; 677 if (sbuf->m != NULL) { 678 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, 679 BUS_DMASYNC_POSTREAD); 680 bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map); 681 m_freem(sbuf->m); 682 } 683 bus_dmamap_destroy(sc->rxbuf_dmat, sbuf->map); 684 } 685 bus_dma_tag_destroy(sc->rxbuf_dmat); 686 } 687} 688 689static int 690ipw_shutdown(device_t dev) 691{ 692 struct ipw_softc *sc = device_get_softc(dev); 693 694 ipw_stop(sc); 695 696 return 0; 697} 698 699static int 700ipw_suspend(device_t dev) 701{ 702 struct ipw_softc *sc = device_get_softc(dev); 703 704 ipw_stop(sc); 705 706 return 0; 707} 708 709static int 710ipw_resume(device_t dev) 711{ 712 struct ipw_softc *sc = device_get_softc(dev); 713 struct ifnet *ifp = sc->sc_ic.ic_ifp; 714 715 mtx_lock(&sc->sc_mtx); 716 717 pci_write_config(dev, 0x41, 0, 1); 718 719 if (ifp->if_flags & IFF_UP) { 720 ifp->if_init(ifp->if_softc); 721 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 722 ifp->if_start(ifp); 723 } 724 725 mtx_unlock(&sc->sc_mtx); 726 727 return 0; 728} 729 730static int 731ipw_media_change(struct ifnet *ifp) 732{ 733 struct ipw_softc *sc = ifp->if_softc; 734 int error; 735 736 mtx_lock(&sc->sc_mtx); 737 738 error = ieee80211_media_change(ifp); 739 if (error != ENETRESET) { 740 mtx_unlock(&sc->sc_mtx); 741 return error; 742 } 743 744 if ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING)) 745 ipw_init(sc); 746 747 mtx_unlock(&sc->sc_mtx); 748 749 return 0; 750} 751 752/* 753 * The firmware automatically adapts the transmit speed. We report its current 754 * value here. 755 */ 756static void 757ipw_media_status(struct ifnet *ifp, struct ifmediareq *imr) 758{ 759#define N(a) (sizeof (a) / sizeof (a[0])) 760 struct ipw_softc *sc = ifp->if_softc; 761 struct ieee80211com *ic = &sc->sc_ic; 762 static const struct { 763 uint32_t val; 764 int rate; 765 } rates[] = { 766 { IPW_RATE_DS1, 2 }, 767 { IPW_RATE_DS2, 4 }, 768 { IPW_RATE_DS5, 11 }, 769 { IPW_RATE_DS11, 22 }, 770 }; 771 uint32_t val; 772 int rate, i; 773 774 imr->ifm_status = IFM_AVALID; 775 imr->ifm_active = IFM_IEEE80211; 776 if (ic->ic_state == IEEE80211_S_RUN) 777 imr->ifm_status |= IFM_ACTIVE; 778 779 /* read current transmission rate from adapter */ 780 val = ipw_read_table1(sc, IPW_INFO_CURRENT_TX_RATE) & 0xf; 781 782 /* convert ipw rate to 802.11 rate */ 783 for (i = 0; i < N(rates) && rates[i].val != val; i++); 784 rate = (i < N(rates)) ? rates[i].rate : 0; 785 786 imr->ifm_active |= IFM_IEEE80211_11B; 787 imr->ifm_active |= ieee80211_rate2media(ic, rate, IEEE80211_MODE_11B); 788 switch (ic->ic_opmode) { 789 case IEEE80211_M_STA: 790 break; 791 792 case IEEE80211_M_IBSS: 793 imr->ifm_active |= IFM_IEEE80211_IBSS; 794 break; 795 796 case IEEE80211_M_MONITOR: 797 imr->ifm_active |= IFM_IEEE80211_MONITOR; 798 break; 799 800 case IEEE80211_M_AHDEMO: 801 case IEEE80211_M_HOSTAP: 802 case IEEE80211_M_WDS: 803 /* should not get there */ 804 break; 805 } 806#undef N 807} 808 809static int 810ipw_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 811{ 812 struct ifnet *ifp = ic->ic_ifp; 813 struct ipw_softc *sc = ifp->if_softc; 814 uint8_t macaddr[IEEE80211_ADDR_LEN]; 815 uint32_t len; 816 817 switch (nstate) { 818 case IEEE80211_S_RUN: 819 DELAY(200); /* firmware needs a short delay here */ 820 821 len = IEEE80211_ADDR_LEN; 822 ipw_read_table2(sc, IPW_INFO_CURRENT_BSSID, macaddr, &len); 823 824#if 0 825 ni = ieee80211_find_node(&ic->ic_scan, macaddr); 826 if (ni == NULL) 827 break; 828 829 ieee80211_ref_node(ni); 830 ieee80211_sta_join(ic, ni); 831 ieee80211_node_authorize(ni); 832 833 if (ic->ic_opmode == IEEE80211_M_STA) 834 ieee80211_notify_node_join(ic, ni, 1); 835#endif 836 break; 837 838 case IEEE80211_S_INIT: 839 case IEEE80211_S_SCAN: 840 case IEEE80211_S_AUTH: 841 case IEEE80211_S_ASSOC: 842 default: 843 break; 844 } 845 846 ic->ic_state = nstate; 847 848 return 0; 849} 850 851/* 852 * Read 16 bits at address 'addr' from the serial EEPROM. 853 */ 854static uint16_t 855ipw_read_prom_word(struct ipw_softc *sc, uint8_t addr) 856{ 857 uint32_t tmp; 858 uint16_t val; 859 int n; 860 861 /* clock C once before the first command */ 862 IPW_EEPROM_CTL(sc, 0); 863 IPW_EEPROM_CTL(sc, IPW_EEPROM_S); 864 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C); 865 IPW_EEPROM_CTL(sc, IPW_EEPROM_S); 866 867 /* write start bit (1) */ 868 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D); 869 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C); 870 871 /* write READ opcode (10) */ 872 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D); 873 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_D | IPW_EEPROM_C); 874 IPW_EEPROM_CTL(sc, IPW_EEPROM_S); 875 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C); 876 877 /* write address A7-A0 */ 878 for (n = 7; n >= 0; n--) { 879 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | 880 (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D)); 881 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | 882 (((addr >> n) & 1) << IPW_EEPROM_SHIFT_D) | IPW_EEPROM_C); 883 } 884 885 IPW_EEPROM_CTL(sc, IPW_EEPROM_S); 886 887 /* read data Q15-Q0 */ 888 val = 0; 889 for (n = 15; n >= 0; n--) { 890 IPW_EEPROM_CTL(sc, IPW_EEPROM_S | IPW_EEPROM_C); 891 IPW_EEPROM_CTL(sc, IPW_EEPROM_S); 892 tmp = MEM_READ_4(sc, IPW_MEM_EEPROM_CTL); 893 val |= ((tmp & IPW_EEPROM_Q) >> IPW_EEPROM_SHIFT_Q) << n; 894 } 895 896 IPW_EEPROM_CTL(sc, 0); 897 898 /* clear Chip Select and clock C */ 899 IPW_EEPROM_CTL(sc, IPW_EEPROM_S); 900 IPW_EEPROM_CTL(sc, 0); 901 IPW_EEPROM_CTL(sc, IPW_EEPROM_C); 902 903 return le16toh(val); 904} 905 906static void 907ipw_command_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf) 908{ 909 struct ipw_cmd *cmd; 910 911 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD); 912 913 cmd = mtod(sbuf->m, struct ipw_cmd *); 914 915 DPRINTFN(2, ("cmd ack'ed (%u, %u, %u, %u, %u)\n", le32toh(cmd->type), 916 le32toh(cmd->subtype), le32toh(cmd->seq), le32toh(cmd->len), 917 le32toh(cmd->status))); 918 919 wakeup(sc); 920} 921 922static void 923ipw_newstate_intr(struct ipw_softc *sc, struct ipw_soft_buf *sbuf) 924{ 925 struct ieee80211com *ic = &sc->sc_ic; 926 uint32_t state; 927 928 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD); 929 930 state = le32toh(*mtod(sbuf->m, uint32_t *)); 931 932 DPRINTFN(2, ("entering state %u\n", state)); 933 934 switch (state) { 935 case IPW_STATE_ASSOCIATED: 936 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 937 break; 938 939 case IPW_STATE_SCANNING: 940 /* don't leave run state on background scan */ 941 if (ic->ic_state != IEEE80211_S_RUN) 942 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 943 944 ic->ic_flags |= IEEE80211_F_SCAN; 945 break; 946 947 case IPW_STATE_SCAN_COMPLETE: 948 ieee80211_notify_scan_done(ic); 949 ic->ic_flags &= ~IEEE80211_F_SCAN; 950 break; 951 952 case IPW_STATE_ASSOCIATION_LOST: 953 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 954 break; 955 956 case IPW_STATE_RADIO_DISABLED: 957 ic->ic_ifp->if_flags &= ~IFF_UP; 958 ipw_stop(sc); 959 break; 960 } 961} 962 963/* 964 * XXX: Hack to set the current channel to the value advertised in beacons or 965 * probe responses. Only used during AP detection. 966 */ 967static void 968ipw_fix_channel(struct ieee80211com *ic, struct mbuf *m) 969{ 970 struct ieee80211_frame *wh; 971 uint8_t subtype; 972 uint8_t *frm, *efrm; 973 974 wh = mtod(m, struct ieee80211_frame *); 975 976 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT) 977 return; 978 979 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 980 981 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON && 982 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP) 983 return; 984 985 frm = (uint8_t *)(wh + 1); 986 efrm = mtod(m, uint8_t *) + m->m_len; 987 988 frm += 12; /* skip tstamp, bintval and capinfo fields */ 989 while (frm < efrm) { 990 if (*frm == IEEE80211_ELEMID_DSPARMS) 991#if IEEE80211_CHAN_MAX < 255 992 if (frm[2] <= IEEE80211_CHAN_MAX) 993#endif 994 ic->ic_bsschan = ieee80211_find_channel(ic, 995 ieee80211_ieee2mhz(frm[2], 0), 996 IEEE80211_MODE_AUTO); 997 998 frm += frm[1] + 2; 999 } 1000} 1001 1002static void 1003ipw_data_intr(struct ipw_softc *sc, struct ipw_status *status, 1004 struct ipw_soft_bd *sbd, struct ipw_soft_buf *sbuf) 1005{ 1006 struct ieee80211com *ic = &sc->sc_ic; 1007 struct ifnet *ifp = ic->ic_ifp; 1008 struct mbuf *mnew, *m; 1009 struct ieee80211_frame *wh; 1010 struct ieee80211_node *ni; 1011 bus_addr_t physaddr; 1012 int error; 1013 1014 DPRINTFN(5, ("received frame len=%u, rssi=%u\n", le32toh(status->len), 1015 status->rssi)); 1016 1017 if (le32toh(status->len) < sizeof (struct ieee80211_frame_min) || 1018 le32toh(status->len) > MCLBYTES) 1019 return; 1020 1021 /* 1022 * Try to allocate a new mbuf for this ring element and load it before 1023 * processing the current mbuf. If the ring element cannot be loaded, 1024 * drop the received packet and reuse the old mbuf. In the unlikely 1025 * case that the old mbuf can't be reloaded either, explicitly panic. 1026 */ 1027 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1028 if (mnew == NULL) { 1029 ifp->if_ierrors++; 1030 return; 1031 } 1032 1033 bus_dmamap_sync(sc->rxbuf_dmat, sbuf->map, BUS_DMASYNC_POSTREAD); 1034 bus_dmamap_unload(sc->rxbuf_dmat, sbuf->map); 1035 1036 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, mtod(mnew, void *), 1037 MCLBYTES, ipw_dma_map_addr, &physaddr, 0); 1038 if (error != 0) { 1039 m_freem(mnew); 1040 1041 /* try to reload the old mbuf */ 1042 error = bus_dmamap_load(sc->rxbuf_dmat, sbuf->map, 1043 mtod(sbuf->m, void *), MCLBYTES, ipw_dma_map_addr, 1044 &physaddr, 0); 1045 if (error != 0) { 1046 /* very unlikely that it will fail... */ 1047 panic("%s: could not load old rx mbuf", 1048 device_get_name(sc->sc_dev)); 1049 } 1050 ifp->if_ierrors++; 1051 return; 1052 } 1053 1054 /* 1055 * New mbuf successfully loaded, update Rx ring and continue 1056 * processing. 1057 */ 1058 m = sbuf->m; 1059 sbuf->m = mnew; 1060 sbd->bd->physaddr = htole32(physaddr); 1061 1062 /* finalize mbuf */ 1063 m->m_pkthdr.rcvif = ifp; 1064 m->m_pkthdr.len = m->m_len = le32toh(status->len); 1065 1066 if (bpf_peers_present(sc->sc_drvbpf)) { 1067 struct ipw_rx_radiotap_header *tap = &sc->sc_rxtap; 1068 1069 tap->wr_flags = 0; 1070 tap->wr_antsignal = status->rssi; 1071 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 1072 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 1073 1074 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 1075 } 1076 1077 if (ic->ic_state == IEEE80211_S_SCAN) 1078 ipw_fix_channel(ic, m); 1079 1080 wh = mtod(m, struct ieee80211_frame *); 1081 mtx_unlock(&sc->sc_mtx); 1082 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 1083 1084 /* send the frame to the 802.11 layer */ 1085 ieee80211_input(ic, m, ni, status->rssi, -95/*XXX*/, 0); 1086 1087 /* node is no longer needed */ 1088 ieee80211_free_node(ni); 1089 mtx_lock(&sc->sc_mtx); 1090 1091 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE); 1092} 1093 1094static void 1095ipw_rx_intr(struct ipw_softc *sc) 1096{ 1097 struct ipw_status *status; 1098 struct ipw_soft_bd *sbd; 1099 struct ipw_soft_buf *sbuf; 1100 uint32_t r, i; 1101 1102 if (!(sc->flags & IPW_FLAG_FW_INITED)) 1103 return; 1104 1105 r = CSR_READ_4(sc, IPW_CSR_RX_READ); 1106 1107 bus_dmamap_sync(sc->status_dmat, sc->status_map, BUS_DMASYNC_POSTREAD); 1108 1109 for (i = (sc->rxcur + 1) % IPW_NRBD; i != r; i = (i + 1) % IPW_NRBD) { 1110 status = &sc->status_list[i]; 1111 sbd = &sc->srbd_list[i]; 1112 sbuf = sbd->priv; 1113 1114 switch (le16toh(status->code) & 0xf) { 1115 case IPW_STATUS_CODE_COMMAND: 1116 ipw_command_intr(sc, sbuf); 1117 break; 1118 1119 case IPW_STATUS_CODE_NEWSTATE: 1120 ipw_newstate_intr(sc, sbuf); 1121 break; 1122 1123 case IPW_STATUS_CODE_DATA_802_3: 1124 case IPW_STATUS_CODE_DATA_802_11: 1125 ipw_data_intr(sc, status, sbd, sbuf); 1126 break; 1127 1128 case IPW_STATUS_CODE_NOTIFICATION: 1129 DPRINTFN(2, ("received notification\n")); 1130 break; 1131 1132 default: 1133 device_printf(sc->sc_dev, "unknown status code %u\n", 1134 le16toh(status->code)); 1135 } 1136 1137 /* firmware was killed, stop processing received frames */ 1138 if (!(sc->flags & IPW_FLAG_FW_INITED)) 1139 return; 1140 1141 sbd->bd->flags = 0; 1142 } 1143 1144 bus_dmamap_sync(sc->rbd_dmat, sc->rbd_map, BUS_DMASYNC_PREWRITE); 1145 1146 /* kick the firmware */ 1147 sc->rxcur = (r == 0) ? IPW_NRBD - 1 : r - 1; 1148 CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur); 1149} 1150 1151static void 1152ipw_release_sbd(struct ipw_softc *sc, struct ipw_soft_bd *sbd) 1153{ 1154 struct ipw_soft_hdr *shdr; 1155 struct ipw_soft_buf *sbuf; 1156 1157 switch (sbd->type) { 1158 case IPW_SBD_TYPE_COMMAND: 1159 bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, 1160 BUS_DMASYNC_POSTWRITE); 1161 bus_dmamap_unload(sc->cmd_dmat, sc->cmd_map); 1162 break; 1163 1164 case IPW_SBD_TYPE_HEADER: 1165 shdr = sbd->priv; 1166 bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_POSTWRITE); 1167 bus_dmamap_unload(sc->hdr_dmat, shdr->map); 1168 SLIST_INSERT_HEAD(&sc->free_shdr, shdr, next); 1169 break; 1170 1171 case IPW_SBD_TYPE_DATA: 1172 sbuf = sbd->priv; 1173 bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, 1174 BUS_DMASYNC_POSTWRITE); 1175 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map); 1176 SLIST_INSERT_HEAD(&sc->free_sbuf, sbuf, next); 1177 1178 if (sbuf->m->m_flags & M_TXCB) 1179 ieee80211_process_callback(sbuf->ni, sbuf->m, 0/*XXX*/); 1180 m_freem(sbuf->m); 1181 ieee80211_free_node(sbuf->ni); 1182 1183 sc->sc_tx_timer = 0; 1184 break; 1185 } 1186 1187 sbd->type = IPW_SBD_TYPE_NOASSOC; 1188} 1189 1190static void 1191ipw_tx_intr(struct ipw_softc *sc) 1192{ 1193 struct ifnet *ifp = sc->sc_ic.ic_ifp; 1194 struct ipw_soft_bd *sbd; 1195 uint32_t r, i; 1196 1197 if (!(sc->flags & IPW_FLAG_FW_INITED)) 1198 return; 1199 1200 r = CSR_READ_4(sc, IPW_CSR_TX_READ); 1201 1202 for (i = (sc->txold + 1) % IPW_NTBD; i != r; i = (i + 1) % IPW_NTBD) { 1203 sbd = &sc->stbd_list[i]; 1204 1205 if (sbd->type == IPW_SBD_TYPE_DATA) 1206 ifp->if_opackets++; 1207 1208 ipw_release_sbd(sc, sbd); 1209 sc->txfree++; 1210 } 1211 1212 /* remember what the firmware has processed */ 1213 sc->txold = (r == 0) ? IPW_NTBD - 1 : r - 1; 1214 1215 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1216 ipw_start(ifp); 1217} 1218 1219static void 1220ipw_intr(void *arg) 1221{ 1222 struct ipw_softc *sc = arg; 1223 uint32_t r; 1224 1225 mtx_lock(&sc->sc_mtx); 1226 1227 if ((r = CSR_READ_4(sc, IPW_CSR_INTR)) == 0 || r == 0xffffffff) { 1228 mtx_unlock(&sc->sc_mtx); 1229 return; 1230 } 1231 1232 /* disable interrupts */ 1233 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0); 1234 1235 /* acknowledge all interrupts */ 1236 CSR_WRITE_4(sc, IPW_CSR_INTR, r); 1237 1238 if (r & (IPW_INTR_FATAL_ERROR | IPW_INTR_PARITY_ERROR)) { 1239 device_printf(sc->sc_dev, "firmware error\n"); 1240 taskqueue_enqueue_fast(taskqueue_fast, &sc->sc_init_task); 1241 r = 0; /* don't process more interrupts */ 1242 } 1243 1244 if (r & IPW_INTR_FW_INIT_DONE) 1245 wakeup(sc); 1246 1247 if (r & IPW_INTR_RX_TRANSFER) 1248 ipw_rx_intr(sc); 1249 1250 if (r & IPW_INTR_TX_TRANSFER) 1251 ipw_tx_intr(sc); 1252 1253 /* re-enable interrupts */ 1254 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK); 1255 1256 mtx_unlock(&sc->sc_mtx); 1257} 1258 1259static void 1260ipw_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 1261{ 1262 if (error != 0) 1263 return; 1264 1265 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 1266 1267 *(bus_addr_t *)arg = segs[0].ds_addr; 1268} 1269 1270/* 1271 * Send a command to the firmware and wait for the acknowledgement. 1272 */ 1273static int 1274ipw_cmd(struct ipw_softc *sc, uint32_t type, void *data, uint32_t len) 1275{ 1276 struct ipw_soft_bd *sbd; 1277 bus_addr_t physaddr; 1278 int error; 1279 1280 sbd = &sc->stbd_list[sc->txcur]; 1281 1282 error = bus_dmamap_load(sc->cmd_dmat, sc->cmd_map, &sc->cmd, 1283 sizeof (struct ipw_cmd), ipw_dma_map_addr, &physaddr, 0); 1284 if (error != 0) { 1285 device_printf(sc->sc_dev, "could not map command DMA memory\n"); 1286 return error; 1287 } 1288 1289 sc->cmd.type = htole32(type); 1290 sc->cmd.subtype = 0; 1291 sc->cmd.len = htole32(len); 1292 sc->cmd.seq = 0; 1293 memcpy(sc->cmd.data, data, len); 1294 1295 sbd->type = IPW_SBD_TYPE_COMMAND; 1296 sbd->bd->physaddr = htole32(physaddr); 1297 sbd->bd->len = htole32(sizeof (struct ipw_cmd)); 1298 sbd->bd->nfrag = 1; 1299 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_COMMAND | 1300 IPW_BD_FLAG_TX_LAST_FRAGMENT; 1301 1302 bus_dmamap_sync(sc->cmd_dmat, sc->cmd_map, BUS_DMASYNC_PREWRITE); 1303 bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE); 1304 1305 DPRINTFN(2, ("sending command (%u, %u, %u, %u)\n", type, 0, 0, len)); 1306 1307 /* kick firmware */ 1308 sc->txfree--; 1309 sc->txcur = (sc->txcur + 1) % IPW_NTBD; 1310 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur); 1311 1312 /* wait at most one second for command to complete */ 1313 return msleep(sc, &sc->sc_mtx, 0, "ipwcmd", hz); 1314} 1315 1316static int 1317ipw_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni) 1318{ 1319 struct ipw_softc *sc = ifp->if_softc; 1320 struct ieee80211com *ic = &sc->sc_ic; 1321 struct ieee80211_frame *wh; 1322 struct ipw_soft_bd *sbd; 1323 struct ipw_soft_hdr *shdr; 1324 struct ipw_soft_buf *sbuf; 1325 struct ieee80211_key *k; 1326 struct mbuf *mnew; 1327 bus_dma_segment_t segs[IPW_MAX_NSEG]; 1328 bus_addr_t physaddr; 1329 int nsegs, error, i; 1330 1331 wh = mtod(m0, struct ieee80211_frame *); 1332 1333 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1334 k = ieee80211_crypto_encap(ic, ni, m0); 1335 if (k == NULL) { 1336 m_freem(m0); 1337 return ENOBUFS; 1338 } 1339 1340 /* packet header may have moved, reset our local pointer */ 1341 wh = mtod(m0, struct ieee80211_frame *); 1342 } 1343 1344 if (bpf_peers_present(sc->sc_drvbpf)) { 1345 struct ipw_tx_radiotap_header *tap = &sc->sc_txtap; 1346 1347 tap->wt_flags = 0; 1348 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1349 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1350 1351 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1352 } 1353 1354 shdr = SLIST_FIRST(&sc->free_shdr); 1355 sbuf = SLIST_FIRST(&sc->free_sbuf); 1356 KASSERT(shdr != NULL && sbuf != NULL, ("empty sw hdr/buf pool")); 1357 1358 shdr->hdr.type = htole32(IPW_HDR_TYPE_SEND); 1359 shdr->hdr.subtype = 0; 1360 shdr->hdr.encrypted = (wh->i_fc[1] & IEEE80211_FC1_WEP) ? 1 : 0; 1361 shdr->hdr.encrypt = 0; 1362 shdr->hdr.keyidx = 0; 1363 shdr->hdr.keysz = 0; 1364 shdr->hdr.fragmentsz = 0; 1365 IEEE80211_ADDR_COPY(shdr->hdr.src_addr, wh->i_addr2); 1366 if (ic->ic_opmode == IEEE80211_M_STA) 1367 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr3); 1368 else 1369 IEEE80211_ADDR_COPY(shdr->hdr.dst_addr, wh->i_addr1); 1370 1371 /* trim IEEE802.11 header */ 1372 m_adj(m0, sizeof (struct ieee80211_frame)); 1373 1374 error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, segs, 1375 &nsegs, 0); 1376 if (error != 0 && error != EFBIG) { 1377 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1378 error); 1379 m_freem(m0); 1380 return error; 1381 } 1382 if (error != 0) { 1383 mnew = m_defrag(m0, M_DONTWAIT); 1384 if (mnew == NULL) { 1385 device_printf(sc->sc_dev, 1386 "could not defragment mbuf\n"); 1387 m_freem(m0); 1388 return ENOBUFS; 1389 } 1390 m0 = mnew; 1391 1392 error = bus_dmamap_load_mbuf_sg(sc->txbuf_dmat, sbuf->map, m0, 1393 segs, &nsegs, 0); 1394 if (error != 0) { 1395 device_printf(sc->sc_dev, 1396 "could not map mbuf (error %d)\n", error); 1397 m_freem(m0); 1398 return error; 1399 } 1400 } 1401 1402 error = bus_dmamap_load(sc->hdr_dmat, shdr->map, &shdr->hdr, 1403 sizeof (struct ipw_hdr), ipw_dma_map_addr, &physaddr, 0); 1404 if (error != 0) { 1405 device_printf(sc->sc_dev, "could not map header DMA memory\n"); 1406 bus_dmamap_unload(sc->txbuf_dmat, sbuf->map); 1407 m_freem(m0); 1408 return error; 1409 } 1410 1411 SLIST_REMOVE_HEAD(&sc->free_sbuf, next); 1412 SLIST_REMOVE_HEAD(&sc->free_shdr, next); 1413 1414 sbd = &sc->stbd_list[sc->txcur]; 1415 sbd->type = IPW_SBD_TYPE_HEADER; 1416 sbd->priv = shdr; 1417 sbd->bd->physaddr = htole32(physaddr); 1418 sbd->bd->len = htole32(sizeof (struct ipw_hdr)); 1419 sbd->bd->nfrag = 1 + nsegs; 1420 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3 | 1421 IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT; 1422 1423 DPRINTFN(5, ("sending tx hdr (%u, %u, %u, %u, %6D, %6D)\n", 1424 shdr->hdr.type, shdr->hdr.subtype, shdr->hdr.encrypted, 1425 shdr->hdr.encrypt, shdr->hdr.src_addr, ":", shdr->hdr.dst_addr, 1426 ":")); 1427 1428 sc->txfree--; 1429 sc->txcur = (sc->txcur + 1) % IPW_NTBD; 1430 1431 sbuf->m = m0; 1432 sbuf->ni = ni; 1433 1434 for (i = 0; i < nsegs; i++) { 1435 sbd = &sc->stbd_list[sc->txcur]; 1436 1437 sbd->bd->physaddr = htole32(segs[i].ds_addr); 1438 sbd->bd->len = htole32(segs[i].ds_len); 1439 sbd->bd->nfrag = 0; 1440 sbd->bd->flags = IPW_BD_FLAG_TX_FRAME_802_3; 1441 if (i == nsegs - 1) { 1442 sbd->type = IPW_SBD_TYPE_DATA; 1443 sbd->priv = sbuf; 1444 sbd->bd->flags |= IPW_BD_FLAG_TX_LAST_FRAGMENT; 1445 } else { 1446 sbd->type = IPW_SBD_TYPE_NOASSOC; 1447 sbd->bd->flags |= IPW_BD_FLAG_TX_NOT_LAST_FRAGMENT; 1448 } 1449 1450 DPRINTFN(5, ("sending fragment (%d, %d)\n", i, segs[i].ds_len)); 1451 1452 sc->txfree--; 1453 sc->txcur = (sc->txcur + 1) % IPW_NTBD; 1454 } 1455 1456 bus_dmamap_sync(sc->hdr_dmat, shdr->map, BUS_DMASYNC_PREWRITE); 1457 bus_dmamap_sync(sc->txbuf_dmat, sbuf->map, BUS_DMASYNC_PREWRITE); 1458 bus_dmamap_sync(sc->tbd_dmat, sc->tbd_map, BUS_DMASYNC_PREWRITE); 1459 1460 /* kick firmware */ 1461 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur); 1462 1463 return 0; 1464} 1465 1466static void 1467ipw_start(struct ifnet *ifp) 1468{ 1469 struct ipw_softc *sc = ifp->if_softc; 1470 struct ieee80211com *ic = &sc->sc_ic; 1471 struct mbuf *m0; 1472 struct ether_header *eh; 1473 struct ieee80211_node *ni; 1474 1475 mtx_lock(&sc->sc_mtx); 1476 1477 if (ic->ic_state != IEEE80211_S_RUN) { 1478 mtx_unlock(&sc->sc_mtx); 1479 return; 1480 } 1481 1482 for (;;) { 1483 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 1484 if (m0 == NULL) 1485 break; 1486 1487 if (sc->txfree < 1 + IPW_MAX_NSEG) { 1488 IFQ_DRV_PREPEND(&ifp->if_snd, m0); 1489 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1490 break; 1491 } 1492 1493 if (m0->m_len < sizeof (struct ether_header) && 1494 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL) 1495 continue; 1496 1497 eh = mtod(m0, struct ether_header *); 1498 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 1499 if (ni == NULL) { 1500 m_freem(m0); 1501 continue; 1502 } 1503 BPF_MTAP(ifp, m0); 1504 1505 m0 = ieee80211_encap(ic, m0, ni); 1506 if (m0 == NULL) { 1507 ieee80211_free_node(ni); 1508 continue; 1509 } 1510 1511 if (bpf_peers_present(ic->ic_rawbpf)) 1512 bpf_mtap(ic->ic_rawbpf, m0); 1513 1514 if (ipw_tx_start(ifp, m0, ni) != 0) { 1515 ieee80211_free_node(ni); 1516 ifp->if_oerrors++; 1517 break; 1518 } 1519 1520 /* start watchdog timer */ 1521 sc->sc_tx_timer = 5; 1522 ifp->if_timer = 1; 1523 } 1524 1525 mtx_unlock(&sc->sc_mtx); 1526} 1527 1528static void 1529ipw_watchdog(struct ifnet *ifp) 1530{ 1531 struct ipw_softc *sc = ifp->if_softc; 1532 1533 mtx_lock(&sc->sc_mtx); 1534 1535 ifp->if_timer = 0; 1536 1537 if (sc->sc_tx_timer > 0) { 1538 if (--sc->sc_tx_timer == 0) { 1539 if_printf(ifp, "device timeout\n"); 1540 ifp->if_oerrors++; 1541 taskqueue_enqueue_fast(taskqueue_fast, 1542 &sc->sc_init_task); 1543 mtx_unlock(&sc->sc_mtx); 1544 return; 1545 } 1546 ifp->if_timer = 1; 1547 } 1548 1549 mtx_unlock(&sc->sc_mtx); 1550} 1551 1552static int 1553ipw_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1554{ 1555 struct ipw_softc *sc = ifp->if_softc; 1556 struct ieee80211com *ic = &sc->sc_ic; 1557 int error = 0; 1558 1559 mtx_lock(&sc->sc_mtx); 1560 1561 switch (cmd) { 1562 case SIOCSIFFLAGS: 1563 if (ifp->if_flags & IFF_UP) { 1564 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 1565 ipw_init(sc); 1566 } else { 1567 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1568 ipw_stop(sc); 1569 } 1570 break; 1571 1572 default: 1573 error = ieee80211_ioctl(ic, cmd, data); 1574 } 1575 1576 if (error == ENETRESET) { 1577 if ((ifp->if_flags & IFF_UP) && 1578 (ifp->if_drv_flags & IFF_DRV_RUNNING)) 1579 ipw_init(sc); 1580 error = 0; 1581 } 1582 1583 mtx_unlock(&sc->sc_mtx); 1584 1585 return error; 1586} 1587 1588static void 1589ipw_stop_master(struct ipw_softc *sc) 1590{ 1591 uint32_t tmp; 1592 int ntries; 1593 1594 /* disable interrupts */ 1595 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, 0); 1596 1597 CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_STOP_MASTER); 1598 for (ntries = 0; ntries < 50; ntries++) { 1599 if (CSR_READ_4(sc, IPW_CSR_RST) & IPW_RST_MASTER_DISABLED) 1600 break; 1601 DELAY(10); 1602 } 1603 if (ntries == 50) 1604 device_printf(sc->sc_dev, "timeout waiting for master\n"); 1605 1606 tmp = CSR_READ_4(sc, IPW_CSR_RST); 1607 CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_PRINCETON_RESET); 1608 1609 sc->flags &= ~IPW_FLAG_FW_INITED; 1610} 1611 1612static int 1613ipw_reset(struct ipw_softc *sc) 1614{ 1615 uint32_t tmp; 1616 int ntries; 1617 1618 ipw_stop_master(sc); 1619 1620 /* move adapter to D0 state */ 1621 tmp = CSR_READ_4(sc, IPW_CSR_CTL); 1622 CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT); 1623 1624 /* wait for clock stabilization */ 1625 for (ntries = 0; ntries < 1000; ntries++) { 1626 if (CSR_READ_4(sc, IPW_CSR_CTL) & IPW_CTL_CLOCK_READY) 1627 break; 1628 DELAY(200); 1629 } 1630 if (ntries == 1000) 1631 return EIO; 1632 1633 tmp = CSR_READ_4(sc, IPW_CSR_RST); 1634 CSR_WRITE_4(sc, IPW_CSR_RST, tmp | IPW_RST_SW_RESET); 1635 1636 DELAY(10); 1637 1638 tmp = CSR_READ_4(sc, IPW_CSR_CTL); 1639 CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_INIT); 1640 1641 return 0; 1642} 1643 1644/* 1645 * Upload the microcode to the device. 1646 */ 1647static int 1648ipw_load_ucode(struct ipw_softc *sc, const char *uc, int size) 1649{ 1650 int ntries; 1651 1652 MEM_WRITE_4(sc, 0x3000e0, 0x80000000); 1653 CSR_WRITE_4(sc, IPW_CSR_RST, 0); 1654 1655 MEM_WRITE_2(sc, 0x220000, 0x0703); 1656 MEM_WRITE_2(sc, 0x220000, 0x0707); 1657 1658 MEM_WRITE_1(sc, 0x210014, 0x72); 1659 MEM_WRITE_1(sc, 0x210014, 0x72); 1660 1661 MEM_WRITE_1(sc, 0x210000, 0x40); 1662 MEM_WRITE_1(sc, 0x210000, 0x00); 1663 MEM_WRITE_1(sc, 0x210000, 0x40); 1664 1665 MEM_WRITE_MULTI_1(sc, 0x210010, uc, size); 1666 1667 MEM_WRITE_1(sc, 0x210000, 0x00); 1668 MEM_WRITE_1(sc, 0x210000, 0x00); 1669 MEM_WRITE_1(sc, 0x210000, 0x80); 1670 1671 MEM_WRITE_2(sc, 0x220000, 0x0703); 1672 MEM_WRITE_2(sc, 0x220000, 0x0707); 1673 1674 MEM_WRITE_1(sc, 0x210014, 0x72); 1675 MEM_WRITE_1(sc, 0x210014, 0x72); 1676 1677 MEM_WRITE_1(sc, 0x210000, 0x00); 1678 MEM_WRITE_1(sc, 0x210000, 0x80); 1679 1680 for (ntries = 0; ntries < 10; ntries++) { 1681 if (MEM_READ_1(sc, 0x210000) & 1) 1682 break; 1683 DELAY(10); 1684 } 1685 if (ntries == 10) { 1686 device_printf(sc->sc_dev, 1687 "timeout waiting for ucode to initialize\n"); 1688 return EIO; 1689 } 1690 1691 MEM_WRITE_4(sc, 0x3000e0, 0); 1692 1693 return 0; 1694} 1695 1696/* set of macros to handle unaligned little endian data in firmware image */ 1697#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24) 1698#define GETLE16(p) ((p)[0] | (p)[1] << 8) 1699static int 1700ipw_load_firmware(struct ipw_softc *sc, const char *fw, int size) 1701{ 1702 const uint8_t *p, *end; 1703 uint32_t tmp, dst; 1704 uint16_t len; 1705 int error; 1706 1707 p = fw; 1708 end = fw + size; 1709 while (p < end) { 1710 dst = GETLE32(p); p += 4; 1711 len = GETLE16(p); p += 2; 1712 1713 ipw_write_mem_1(sc, dst, p, len); 1714 p += len; 1715 } 1716 1717 CSR_WRITE_4(sc, IPW_CSR_IO, IPW_IO_GPIO1_ENABLE | IPW_IO_GPIO3_MASK | 1718 IPW_IO_LED_OFF); 1719 1720 /* enable interrupts */ 1721 CSR_WRITE_4(sc, IPW_CSR_INTR_MASK, IPW_INTR_MASK); 1722 1723 /* kick the firmware */ 1724 CSR_WRITE_4(sc, IPW_CSR_RST, 0); 1725 1726 tmp = CSR_READ_4(sc, IPW_CSR_CTL); 1727 CSR_WRITE_4(sc, IPW_CSR_CTL, tmp | IPW_CTL_ALLOW_STANDBY); 1728 1729 /* wait at most one second for firmware initialization to complete */ 1730 if ((error = msleep(sc, &sc->sc_mtx, 0, "ipwinit", hz)) != 0) { 1731 device_printf(sc->sc_dev, "timeout waiting for firmware " 1732 "initialization to complete\n"); 1733 return error; 1734 } 1735 1736 tmp = CSR_READ_4(sc, IPW_CSR_IO); 1737 CSR_WRITE_4(sc, IPW_CSR_IO, tmp | IPW_IO_GPIO1_MASK | 1738 IPW_IO_GPIO3_MASK); 1739 1740 return 0; 1741} 1742 1743static int 1744ipw_config(struct ipw_softc *sc) 1745{ 1746 struct ieee80211com *ic = &sc->sc_ic; 1747 struct ifnet *ifp = ic->ic_ifp; 1748 struct ipw_security security; 1749 struct ieee80211_key *k; 1750 struct ipw_wep_key wepkey; 1751 struct ipw_scan_options options; 1752 struct ipw_configuration config; 1753 uint32_t data; 1754 int error, i; 1755 1756 switch (ic->ic_opmode) { 1757 case IEEE80211_M_STA: 1758 case IEEE80211_M_HOSTAP: 1759 case IEEE80211_M_WDS: /* XXX */ 1760 data = htole32(IPW_MODE_BSS); 1761 break; 1762 case IEEE80211_M_IBSS: 1763 case IEEE80211_M_AHDEMO: 1764 data = htole32(IPW_MODE_IBSS); 1765 break; 1766 case IEEE80211_M_MONITOR: 1767 data = htole32(IPW_MODE_MONITOR); 1768 break; 1769 } 1770 DPRINTF(("Setting mode to %u\n", le32toh(data))); 1771 error = ipw_cmd(sc, IPW_CMD_SET_MODE, &data, sizeof data); 1772 if (error != 0) 1773 return error; 1774 1775 if (ic->ic_opmode == IEEE80211_M_IBSS || 1776 ic->ic_opmode == IEEE80211_M_MONITOR) { 1777 data = htole32(ieee80211_chan2ieee(ic, ic->ic_curchan)); 1778 DPRINTF(("Setting channel to %u\n", le32toh(data))); 1779 error = ipw_cmd(sc, IPW_CMD_SET_CHANNEL, &data, sizeof data); 1780 if (error != 0) 1781 return error; 1782 } 1783 1784 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 1785 DPRINTF(("Enabling adapter\n")); 1786 return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0); 1787 } 1788 1789 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp)); 1790 DPRINTF(("Setting MAC address to %6D\n", ic->ic_myaddr, ":")); 1791 error = ipw_cmd(sc, IPW_CMD_SET_MAC_ADDRESS, ic->ic_myaddr, 1792 IEEE80211_ADDR_LEN); 1793 if (error != 0) 1794 return error; 1795 1796 config.flags = htole32(IPW_CFG_BSS_MASK | IPW_CFG_IBSS_MASK | 1797 IPW_CFG_PREAMBLE_AUTO | IPW_CFG_802_1x_ENABLE); 1798 if (ic->ic_opmode == IEEE80211_M_IBSS) 1799 config.flags |= htole32(IPW_CFG_IBSS_AUTO_START); 1800 if (ifp->if_flags & IFF_PROMISC) 1801 config.flags |= htole32(IPW_CFG_PROMISCUOUS); 1802 config.bss_chan = htole32(0x3fff); /* channels 1-14 */ 1803 config.ibss_chan = htole32(0x7ff); /* channels 1-11 */ 1804 DPRINTF(("Setting configuration to 0x%x\n", le32toh(config.flags))); 1805 error = ipw_cmd(sc, IPW_CMD_SET_CONFIGURATION, &config, sizeof config); 1806 if (error != 0) 1807 return error; 1808 1809 data = htole32(0x3); /* 1, 2 */ 1810 DPRINTF(("Setting basic tx rates to 0x%x\n", le32toh(data))); 1811 error = ipw_cmd(sc, IPW_CMD_SET_BASIC_TX_RATES, &data, sizeof data); 1812 if (error != 0) 1813 return error; 1814 1815 data = htole32(0xf); /* 1, 2, 5.5, 11 */ 1816 DPRINTF(("Setting tx rates to 0x%x\n", le32toh(data))); 1817 error = ipw_cmd(sc, IPW_CMD_SET_TX_RATES, &data, sizeof data); 1818 if (error != 0) 1819 return error; 1820 1821 data = htole32(IPW_POWER_MODE_CAM); 1822 DPRINTF(("Setting power mode to %u\n", le32toh(data))); 1823 error = ipw_cmd(sc, IPW_CMD_SET_POWER_MODE, &data, sizeof data); 1824 if (error != 0) 1825 return error; 1826 1827 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1828 data = htole32(32); /* default value */ 1829 DPRINTF(("Setting tx power index to %u\n", le32toh(data))); 1830 error = ipw_cmd(sc, IPW_CMD_SET_TX_POWER_INDEX, &data, 1831 sizeof data); 1832 if (error != 0) 1833 return error; 1834 } 1835 1836 data = htole32(ic->ic_rtsthreshold); 1837 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data))); 1838 error = ipw_cmd(sc, IPW_CMD_SET_RTS_THRESHOLD, &data, sizeof data); 1839 if (error != 0) 1840 return error; 1841 1842 data = htole32(ic->ic_fragthreshold); 1843 DPRINTF(("Setting frag threshold to %u\n", le32toh(data))); 1844 error = ipw_cmd(sc, IPW_CMD_SET_FRAG_THRESHOLD, &data, sizeof data); 1845 if (error != 0) 1846 return error; 1847 1848#ifdef IPW_DEBUG 1849 if (ipw_debug > 0) { 1850 printf("Setting ESSID to "); 1851 ieee80211_print_essid(ic->ic_des_ssid[0].ssid, 1852 ic->ic_des_ssid[0].len); 1853 printf("\n"); 1854 } 1855#endif 1856 error = ipw_cmd(sc, IPW_CMD_SET_ESSID, ic->ic_des_ssid[0].ssid, 1857 ic->ic_des_ssid[0].len); 1858 if (error != 0) 1859 return error; 1860 1861 /* no mandatory BSSID */ 1862 DPRINTF(("Setting mandatory BSSID to null\n")); 1863 error = ipw_cmd(sc, IPW_CMD_SET_MANDATORY_BSSID, NULL, 0); 1864 if (error != 0) 1865 return error; 1866 1867 if (ic->ic_flags & IEEE80211_F_DESBSSID) { 1868 DPRINTF(("Setting desired BSSID to %6D\n", ic->ic_des_bssid, 1869 ":")); 1870 error = ipw_cmd(sc, IPW_CMD_SET_DESIRED_BSSID, 1871 ic->ic_des_bssid, IEEE80211_ADDR_LEN); 1872 if (error != 0) 1873 return error; 1874 } 1875 1876 memset(&security, 0, sizeof security); 1877 security.authmode = (ic->ic_bss->ni_authmode == IEEE80211_AUTH_SHARED) ? 1878 IPW_AUTH_SHARED : IPW_AUTH_OPEN; 1879 security.ciphers = htole32(IPW_CIPHER_NONE); 1880 DPRINTF(("Setting authmode to %u\n", security.authmode)); 1881 error = ipw_cmd(sc, IPW_CMD_SET_SECURITY_INFORMATION, &security, 1882 sizeof security); 1883 if (error != 0) 1884 return error; 1885 1886 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 1887 k = ic->ic_crypto.cs_nw_keys; 1888 for (i = 0; i < IEEE80211_WEP_NKID; i++, k++) { 1889 if (k->wk_keylen == 0) 1890 continue; 1891 1892 wepkey.idx = i; 1893 wepkey.len = k->wk_keylen; 1894 memset(wepkey.key, 0, sizeof wepkey.key); 1895 memcpy(wepkey.key, k->wk_key, k->wk_keylen); 1896 DPRINTF(("Setting wep key index %u len %u\n", 1897 wepkey.idx, wepkey.len)); 1898 error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY, &wepkey, 1899 sizeof wepkey); 1900 if (error != 0) 1901 return error; 1902 } 1903 1904 data = htole32(ic->ic_crypto.cs_def_txkey); 1905 DPRINTF(("Setting wep tx key index to %u\n", le32toh(data))); 1906 error = ipw_cmd(sc, IPW_CMD_SET_WEP_KEY_INDEX, &data, 1907 sizeof data); 1908 if (error != 0) 1909 return error; 1910 } 1911 1912 data = htole32((ic->ic_flags & IEEE80211_F_PRIVACY) ? IPW_WEPON : 0); 1913 DPRINTF(("Setting wep flags to 0x%x\n", le32toh(data))); 1914 error = ipw_cmd(sc, IPW_CMD_SET_WEP_FLAGS, &data, sizeof data); 1915 if (error != 0) 1916 return error; 1917 1918#if 0 1919 struct ipw_wpa_ie ie; 1920 1921 memset(&ie, 0, sizeof ie); 1922 ie.len = htole32(sizeof (struct ieee80211_ie_wpa)); 1923 DPRINTF(("Setting wpa ie\n")); 1924 error = ipw_cmd(sc, IPW_CMD_SET_WPA_IE, &ie, sizeof ie); 1925 if (error != 0) 1926 return error; 1927#endif 1928 1929 if (ic->ic_opmode == IEEE80211_M_IBSS) { 1930 data = htole32(ic->ic_bintval); 1931 DPRINTF(("Setting beacon interval to %u\n", le32toh(data))); 1932 error = ipw_cmd(sc, IPW_CMD_SET_BEACON_INTERVAL, &data, 1933 sizeof data); 1934 if (error != 0) 1935 return error; 1936 } 1937 1938 options.flags = 0; 1939 options.channels = htole32(0x3fff); /* scan channels 1-14 */ 1940 DPRINTF(("Setting scan options to 0x%x\n", le32toh(options.flags))); 1941 error = ipw_cmd(sc, IPW_CMD_SET_SCAN_OPTIONS, &options, sizeof options); 1942 if (error != 0) 1943 return error; 1944 1945 /* finally, enable adapter (start scanning for an access point) */ 1946 DPRINTF(("Enabling adapter\n")); 1947 return ipw_cmd(sc, IPW_CMD_ENABLE, NULL, 0); 1948} 1949 1950/* 1951 * Handler for sc_init_task. This is a simple wrapper around ipw_init(). 1952 * It is called on firmware panics or on watchdog timeouts. 1953 */ 1954static void 1955ipw_init_task(void *context, int pending) 1956{ 1957 ipw_init(context); 1958} 1959 1960static void 1961ipw_init(void *priv) 1962{ 1963 struct ipw_softc *sc = priv; 1964 struct ieee80211com *ic = &sc->sc_ic; 1965 struct ifnet *ifp = ic->ic_ifp; 1966 const struct firmware *fp; 1967 const struct ipw_firmware_hdr *hdr; 1968 const char *imagename, *fw; 1969 int owned; 1970 1971 /* 1972 * ipw_init() is exposed through ifp->if_init so it might be called 1973 * without the driver's lock held. Since msleep() doesn't like being 1974 * called on a recursed mutex, we acquire the driver's lock only if 1975 * we're not already holding it. 1976 */ 1977 if (!(owned = mtx_owned(&sc->sc_mtx))) 1978 mtx_lock(&sc->sc_mtx); 1979 1980 /* 1981 * Avoid re-entrant calls. We need to release the mutex in ipw_init() 1982 * when loading the firmware and we don't want to be called during this 1983 * operation. 1984 */ 1985 if (sc->flags & IPW_FLAG_INIT_LOCKED) { 1986 if (!owned) 1987 mtx_unlock(&sc->sc_mtx); 1988 return; 1989 } 1990 sc->flags |= IPW_FLAG_INIT_LOCKED; 1991 1992 ipw_stop(sc); 1993 1994 if (ipw_reset(sc) != 0) { 1995 device_printf(sc->sc_dev, "could not reset adapter\n"); 1996 goto fail1; 1997 } 1998 1999 switch (ic->ic_opmode) { 2000 case IEEE80211_M_STA: 2001 imagename = "ipw_bss"; 2002 break; 2003 case IEEE80211_M_IBSS: 2004 imagename = "ipw_ibss"; 2005 break; 2006 case IEEE80211_M_MONITOR: 2007 imagename = "ipw_monitor"; 2008 break; 2009 default: 2010 imagename = NULL; /* should not get there */ 2011 } 2012 2013 /* 2014 * Load firmware image using the firmware(9) subsystem. We need to 2015 * release the driver's lock first. 2016 */ 2017 if (sc->sc_firmware == NULL || strcmp(sc->sc_firmware->name, 2018 imagename) != 0) { 2019 mtx_unlock(&sc->sc_mtx); 2020 if (sc->sc_firmware != NULL) 2021 firmware_put(sc->sc_firmware, FIRMWARE_UNLOAD); 2022 sc->sc_firmware = firmware_get(imagename); 2023 mtx_lock(&sc->sc_mtx); 2024 } 2025 2026 if (sc->sc_firmware == NULL) { 2027 device_printf(sc->sc_dev, 2028 "could not load firmware image '%s'\n", imagename); 2029 goto fail1; 2030 } 2031 2032 fp = sc->sc_firmware; 2033 if (fp->datasize < sizeof *hdr) { 2034 device_printf(sc->sc_dev, 2035 "firmware image too short %zu\n", fp->datasize); 2036 goto fail2; 2037 } 2038 2039 hdr = (const struct ipw_firmware_hdr *)fp->data; 2040 2041 if (fp->datasize < sizeof *hdr + le32toh(hdr->mainsz) + 2042 le32toh(hdr->ucodesz)) { 2043 device_printf(sc->sc_dev, 2044 "firmware image too short %zu\n", fp->datasize); 2045 goto fail2; 2046 } 2047 2048 fw = (const char *)fp->data + sizeof *hdr + le32toh(hdr->mainsz); 2049 if (ipw_load_ucode(sc, fw, le32toh(hdr->ucodesz)) != 0) { 2050 device_printf(sc->sc_dev, "could not load microcode\n"); 2051 goto fail2; 2052 } 2053 2054 ipw_stop_master(sc); 2055 2056 /* 2057 * Setup tx, rx and status rings. 2058 */ 2059 sc->txold = IPW_NTBD - 1; 2060 sc->txcur = 0; 2061 sc->txfree = IPW_NTBD - 2; 2062 sc->rxcur = IPW_NRBD - 1; 2063 2064 CSR_WRITE_4(sc, IPW_CSR_TX_BASE, sc->tbd_phys); 2065 CSR_WRITE_4(sc, IPW_CSR_TX_SIZE, IPW_NTBD); 2066 CSR_WRITE_4(sc, IPW_CSR_TX_READ, 0); 2067 CSR_WRITE_4(sc, IPW_CSR_TX_WRITE, sc->txcur); 2068 2069 CSR_WRITE_4(sc, IPW_CSR_RX_BASE, sc->rbd_phys); 2070 CSR_WRITE_4(sc, IPW_CSR_RX_SIZE, IPW_NRBD); 2071 CSR_WRITE_4(sc, IPW_CSR_RX_READ, 0); 2072 CSR_WRITE_4(sc, IPW_CSR_RX_WRITE, sc->rxcur); 2073 2074 CSR_WRITE_4(sc, IPW_CSR_STATUS_BASE, sc->status_phys); 2075 2076 fw = (const char *)fp->data + sizeof *hdr; 2077 if (ipw_load_firmware(sc, fw, le32toh(hdr->mainsz)) != 0) { 2078 device_printf(sc->sc_dev, "could not load firmware\n"); 2079 goto fail2; 2080 } 2081 2082 sc->flags |= IPW_FLAG_FW_INITED; 2083 2084 /* retrieve information tables base addresses */ 2085 sc->table1_base = CSR_READ_4(sc, IPW_CSR_TABLE1_BASE); 2086 sc->table2_base = CSR_READ_4(sc, IPW_CSR_TABLE2_BASE); 2087 2088 ipw_write_table1(sc, IPW_INFO_LOCK, 0); 2089 2090 if (ipw_config(sc) != 0) { 2091 device_printf(sc->sc_dev, "device configuration failed\n"); 2092 goto fail1; 2093 } 2094 2095 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2096 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2097 2098 sc->flags &=~ IPW_FLAG_INIT_LOCKED; 2099 2100 if (!owned) 2101 mtx_unlock(&sc->sc_mtx); 2102 2103 return; 2104 2105fail2: firmware_put(fp, FIRMWARE_UNLOAD); 2106 sc->sc_firmware = NULL; 2107fail1: ifp->if_flags &= ~IFF_UP; 2108 ipw_stop(sc); 2109 sc->flags &=~ IPW_FLAG_INIT_LOCKED; 2110 if (!owned) 2111 mtx_unlock(&sc->sc_mtx); 2112} 2113 2114static void 2115ipw_stop(void *priv) 2116{ 2117 struct ipw_softc *sc = priv; 2118 struct ieee80211com *ic = &sc->sc_ic; 2119 struct ifnet *ifp = ic->ic_ifp; 2120 int i; 2121 2122 mtx_lock(&sc->sc_mtx); 2123 2124 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2125 2126 ipw_stop_master(sc); 2127 2128 CSR_WRITE_4(sc, IPW_CSR_RST, IPW_RST_SW_RESET); 2129 2130 /* 2131 * Release tx buffers. 2132 */ 2133 for (i = 0; i < IPW_NTBD; i++) 2134 ipw_release_sbd(sc, &sc->stbd_list[i]); 2135 2136 sc->sc_tx_timer = 0; 2137 ifp->if_timer = 0; 2138 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2139 2140 mtx_unlock(&sc->sc_mtx); 2141} 2142 2143static int 2144ipw_sysctl_stats(SYSCTL_HANDLER_ARGS) 2145{ 2146 struct ipw_softc *sc = arg1; 2147 uint32_t i, size, buf[256]; 2148 2149 if (!(sc->flags & IPW_FLAG_FW_INITED)) { 2150 memset(buf, 0, sizeof buf); 2151 return SYSCTL_OUT(req, buf, sizeof buf); 2152 } 2153 2154 CSR_WRITE_4(sc, IPW_CSR_AUTOINC_ADDR, sc->table1_base); 2155 2156 size = min(CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA), 256); 2157 for (i = 1; i < size; i++) 2158 buf[i] = MEM_READ_4(sc, CSR_READ_4(sc, IPW_CSR_AUTOINC_DATA)); 2159 2160 return SYSCTL_OUT(req, buf, sizeof buf); 2161} 2162 2163static int 2164ipw_sysctl_radio(SYSCTL_HANDLER_ARGS) 2165{ 2166 struct ipw_softc *sc = arg1; 2167 int val; 2168 2169 val = !((sc->flags & IPW_FLAG_HAS_RADIO_SWITCH) && 2170 (CSR_READ_4(sc, IPW_CSR_IO) & IPW_IO_RADIO_DISABLED)); 2171 2172 return SYSCTL_OUT(req, &val, sizeof val); 2173} 2174 2175static uint32_t 2176ipw_read_table1(struct ipw_softc *sc, uint32_t off) 2177{ 2178 return MEM_READ_4(sc, MEM_READ_4(sc, sc->table1_base + off)); 2179} 2180 2181static void 2182ipw_write_table1(struct ipw_softc *sc, uint32_t off, uint32_t info) 2183{ 2184 MEM_WRITE_4(sc, MEM_READ_4(sc, sc->table1_base + off), info); 2185} 2186 2187static int 2188ipw_read_table2(struct ipw_softc *sc, uint32_t off, void *buf, uint32_t *len) 2189{ 2190 uint32_t addr, info; 2191 uint16_t count, size; 2192 uint32_t total; 2193 2194 /* addr[4] + count[2] + size[2] */ 2195 addr = MEM_READ_4(sc, sc->table2_base + off); 2196 info = MEM_READ_4(sc, sc->table2_base + off + 4); 2197 2198 count = info >> 16; 2199 size = info & 0xffff; 2200 total = count * size; 2201 2202 if (total > *len) { 2203 *len = total; 2204 return EINVAL; 2205 } 2206 2207 *len = total; 2208 ipw_read_mem_1(sc, addr, buf, total); 2209 2210 return 0; 2211} 2212 2213static void 2214ipw_read_mem_1(struct ipw_softc *sc, bus_size_t offset, uint8_t *datap, 2215 bus_size_t count) 2216{ 2217 for (; count > 0; offset++, datap++, count--) { 2218 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3); 2219 *datap = CSR_READ_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3)); 2220 } 2221} 2222 2223static void 2224ipw_write_mem_1(struct ipw_softc *sc, bus_size_t offset, const uint8_t *datap, 2225 bus_size_t count) 2226{ 2227 for (; count > 0; offset++, datap++, count--) { 2228 CSR_WRITE_4(sc, IPW_CSR_INDIRECT_ADDR, offset & ~3); 2229 CSR_WRITE_1(sc, IPW_CSR_INDIRECT_DATA + (offset & 3), *datap); 2230 } 2231} 2232