if_iwi.c revision 152629
1/* $FreeBSD: head/sys/dev/iwi/if_iwi.c 152629 2005-11-20 04:27:24Z scottl $ */ 2 3/*- 4 * Copyright (c) 2004, 2005 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/iwi/if_iwi.c 152629 2005-11-20 04:27:24Z scottl $"); 32 33/*- 34 * Intel(R) PRO/Wireless 2200BG/2225BG/2915ABG 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/module.h> 47#include <sys/bus.h> 48#include <sys/endian.h> 49#include <sys/proc.h> 50#include <sys/mount.h> 51#include <sys/namei.h> 52#include <sys/vnode.h> 53 54#include <machine/bus.h> 55#include <machine/resource.h> 56#include <machine/clock.h> 57#include <sys/rman.h> 58 59#include <dev/pci/pcireg.h> 60#include <dev/pci/pcivar.h> 61 62#include <net/bpf.h> 63#include <net/if.h> 64#include <net/if_arp.h> 65#include <net/ethernet.h> 66#include <net/if_dl.h> 67#include <net/if_media.h> 68#include <net/if_types.h> 69 70#include <net80211/ieee80211_var.h> 71#include <net80211/ieee80211_radiotap.h> 72 73#include <netinet/in.h> 74#include <netinet/in_systm.h> 75#include <netinet/in_var.h> 76#include <netinet/ip.h> 77#include <netinet/if_ether.h> 78 79#include <dev/iwi/if_iwireg.h> 80#include <dev/iwi/if_iwivar.h> 81 82#ifdef IWI_DEBUG 83#define DPRINTF(x) do { if (iwi_debug > 0) printf x; } while (0) 84#define DPRINTFN(n, x) do { if (iwi_debug >= (n)) printf x; } while (0) 85int iwi_debug = 0; 86SYSCTL_INT(_debug, OID_AUTO, iwi, CTLFLAG_RW, &iwi_debug, 0, "iwi debug level"); 87#else 88#define DPRINTF(x) 89#define DPRINTFN(n, x) 90#endif 91 92MODULE_DEPEND(iwi, pci, 1, 1, 1); 93MODULE_DEPEND(iwi, wlan, 1, 1, 1); 94 95struct iwi_ident { 96 uint16_t vendor; 97 uint16_t device; 98 const char *name; 99}; 100 101static const struct iwi_ident iwi_ident_table[] = { 102 { 0x8086, 0x4220, "Intel(R) PRO/Wireless 2200BG" }, 103 { 0x8086, 0x4221, "Intel(R) PRO/Wireless 2225BG" }, 104 { 0x8086, 0x4223, "Intel(R) PRO/Wireless 2915ABG" }, 105 { 0x8086, 0x4224, "Intel(R) PRO/Wireless 2915ABG" }, 106 107 { 0, 0, NULL } 108}; 109 110static void iwi_dma_map_addr(void *, bus_dma_segment_t *, int, int); 111static int iwi_alloc_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *, 112 int); 113static void iwi_reset_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *); 114static void iwi_free_cmd_ring(struct iwi_softc *, struct iwi_cmd_ring *); 115static int iwi_alloc_tx_ring(struct iwi_softc *, struct iwi_tx_ring *, 116 int, bus_addr_t, bus_addr_t); 117static void iwi_reset_tx_ring(struct iwi_softc *, struct iwi_tx_ring *); 118static void iwi_free_tx_ring(struct iwi_softc *, struct iwi_tx_ring *); 119static int iwi_alloc_rx_ring(struct iwi_softc *, struct iwi_rx_ring *, 120 int); 121static void iwi_reset_rx_ring(struct iwi_softc *, struct iwi_rx_ring *); 122static void iwi_free_rx_ring(struct iwi_softc *, struct iwi_rx_ring *); 123static struct ieee80211_node *iwi_node_alloc(struct ieee80211_node_table *); 124static void iwi_node_free(struct ieee80211_node *); 125static int iwi_media_change(struct ifnet *); 126static void iwi_media_status(struct ifnet *, struct ifmediareq *); 127static int iwi_newstate(struct ieee80211com *, enum ieee80211_state, int); 128static int iwi_wme_update(struct ieee80211com *); 129static uint16_t iwi_read_prom_word(struct iwi_softc *, uint8_t); 130static void iwi_fix_channel(struct ieee80211com *, struct mbuf *); 131static void iwi_frame_intr(struct iwi_softc *, struct iwi_rx_data *, int, 132 struct iwi_frame *); 133static void iwi_notification_intr(struct iwi_softc *, struct iwi_notif *); 134static void iwi_rx_intr(struct iwi_softc *); 135static void iwi_tx_intr(struct iwi_softc *, struct iwi_tx_ring *); 136static void iwi_intr(void *); 137static int iwi_cmd(struct iwi_softc *, uint8_t, void *, uint8_t, int); 138static void iwi_write_ibssnode(struct iwi_softc *, const struct iwi_node *); 139static int iwi_tx_start(struct ifnet *, struct mbuf *, 140 struct ieee80211_node *, int); 141static void iwi_start(struct ifnet *); 142static void iwi_watchdog(struct ifnet *); 143static int iwi_ioctl(struct ifnet *, u_long, caddr_t); 144static void iwi_stop_master(struct iwi_softc *); 145static int iwi_reset(struct iwi_softc *); 146static int iwi_load_ucode(struct iwi_softc *, const char *); 147static int iwi_load_firmware(struct iwi_softc *, const char *); 148static int iwi_config(struct iwi_softc *); 149static int iwi_set_chan(struct iwi_softc *, struct ieee80211_channel *); 150static int iwi_scan(struct iwi_softc *); 151static int iwi_auth_and_assoc(struct iwi_softc *); 152static void iwi_init(void *); 153static void iwi_stop(void *); 154static int iwi_read_firmware(const char *, caddr_t *, size_t *); 155static int iwi_sysctl_stats(SYSCTL_HANDLER_ARGS); 156static int iwi_sysctl_radio(SYSCTL_HANDLER_ARGS); 157 158static int iwi_probe(device_t); 159static int iwi_attach(device_t); 160static int iwi_detach(device_t); 161static int iwi_shutdown(device_t); 162static int iwi_suspend(device_t); 163static int iwi_resume(device_t); 164 165static device_method_t iwi_methods[] = { 166 /* Device interface */ 167 DEVMETHOD(device_probe, iwi_probe), 168 DEVMETHOD(device_attach, iwi_attach), 169 DEVMETHOD(device_detach, iwi_detach), 170 DEVMETHOD(device_shutdown, iwi_shutdown), 171 DEVMETHOD(device_suspend, iwi_suspend), 172 DEVMETHOD(device_resume, iwi_resume), 173 174 { 0, 0 } 175}; 176 177static driver_t iwi_driver = { 178 "iwi", 179 iwi_methods, 180 sizeof (struct iwi_softc) 181}; 182 183static devclass_t iwi_devclass; 184 185DRIVER_MODULE(iwi, pci, iwi_driver, iwi_devclass, 0, 0); 186 187/* 188 * Supported rates for 802.11a/b/g modes (in 500Kbps unit). 189 */ 190static const struct ieee80211_rateset iwi_rateset_11a = 191 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } }; 192 193static const struct ieee80211_rateset iwi_rateset_11b = 194 { 4, { 2, 4, 11, 22 } }; 195 196static const struct ieee80211_rateset iwi_rateset_11g = 197 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } }; 198 199static __inline uint8_t 200MEM_READ_1(struct iwi_softc *sc, uint32_t addr) 201{ 202 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr); 203 return CSR_READ_1(sc, IWI_CSR_INDIRECT_DATA); 204} 205 206static __inline uint32_t 207MEM_READ_4(struct iwi_softc *sc, uint32_t addr) 208{ 209 CSR_WRITE_4(sc, IWI_CSR_INDIRECT_ADDR, addr); 210 return CSR_READ_4(sc, IWI_CSR_INDIRECT_DATA); 211} 212 213static int 214iwi_probe(device_t dev) 215{ 216 const struct iwi_ident *ident; 217 218 for (ident = iwi_ident_table; ident->name != NULL; ident++) { 219 if (pci_get_vendor(dev) == ident->vendor && 220 pci_get_device(dev) == ident->device) { 221 device_set_desc(dev, ident->name); 222 return 0; 223 } 224 } 225 return ENXIO; 226} 227 228/* Base Address Register */ 229#define IWI_PCI_BAR0 0x10 230 231static int 232iwi_attach(device_t dev) 233{ 234 struct iwi_softc *sc = device_get_softc(dev); 235 struct ifnet *ifp; 236 struct ieee80211com *ic = &sc->sc_ic; 237 uint16_t val; 238 int error, i; 239 240 sc->sc_dev = dev; 241 242 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 243 MTX_DEF | MTX_RECURSE); 244 245 sc->sc_unr = new_unrhdr(0, IWI_MAX_IBSSNODE, &sc->sc_mtx); 246 247 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 248 device_printf(dev, "chip is in D%d power mode " 249 "-- setting to D0\n", pci_get_powerstate(dev)); 250 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 251 } 252 253 pci_write_config(dev, 0x41, 0, 1); 254 255 /* enable bus-mastering */ 256 pci_enable_busmaster(dev); 257 258 sc->mem_rid = IWI_PCI_BAR0; 259 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid, 260 RF_ACTIVE); 261 if (sc->mem == NULL) { 262 device_printf(dev, "could not allocate memory resource\n"); 263 goto fail; 264 } 265 266 sc->sc_st = rman_get_bustag(sc->mem); 267 sc->sc_sh = rman_get_bushandle(sc->mem); 268 269 sc->irq_rid = 0; 270 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 271 RF_ACTIVE | RF_SHAREABLE); 272 if (sc->irq == NULL) { 273 device_printf(dev, "could not allocate interrupt resource\n"); 274 goto fail; 275 } 276 277 if (iwi_reset(sc) != 0) { 278 device_printf(dev, "could not reset adapter\n"); 279 goto fail; 280 } 281 282 /* 283 * Allocate rings. 284 */ 285 if (iwi_alloc_cmd_ring(sc, &sc->cmdq, IWI_CMD_RING_COUNT) != 0) { 286 device_printf(dev, "could not allocate Cmd ring\n"); 287 goto fail; 288 } 289 290 error = iwi_alloc_tx_ring(sc, &sc->txq[0], IWI_TX_RING_COUNT, 291 IWI_CSR_TX1_RIDX, IWI_CSR_TX1_WIDX); 292 if (error != 0) { 293 device_printf(dev, "could not allocate Tx ring 1\n"); 294 goto fail; 295 } 296 297 error = iwi_alloc_tx_ring(sc, &sc->txq[1], IWI_TX_RING_COUNT, 298 IWI_CSR_TX2_RIDX, IWI_CSR_TX2_WIDX); 299 if (error != 0) { 300 device_printf(dev, "could not allocate Tx ring 2\n"); 301 goto fail; 302 } 303 304 error = iwi_alloc_tx_ring(sc, &sc->txq[2], IWI_TX_RING_COUNT, 305 IWI_CSR_TX3_RIDX, IWI_CSR_TX3_WIDX); 306 if (error != 0) { 307 device_printf(dev, "could not allocate Tx ring 3\n"); 308 goto fail; 309 } 310 311 error = iwi_alloc_tx_ring(sc, &sc->txq[3], IWI_TX_RING_COUNT, 312 IWI_CSR_TX4_RIDX, IWI_CSR_TX4_WIDX); 313 if (error != 0) { 314 device_printf(dev, "could not allocate Tx ring 4\n"); 315 goto fail; 316 } 317 318 if (iwi_alloc_rx_ring(sc, &sc->rxq, IWI_RX_RING_COUNT) != 0) { 319 device_printf(dev, "could not allocate Rx ring\n"); 320 goto fail; 321 } 322 323 ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 324 if (ifp == NULL) { 325 device_printf(dev, "can not if_alloc()\n"); 326 goto fail; 327 } 328 ifp->if_softc = sc; 329 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 330 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 331 ifp->if_init = iwi_init; 332 ifp->if_ioctl = iwi_ioctl; 333 ifp->if_start = iwi_start; 334 ifp->if_watchdog = iwi_watchdog; 335 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 336 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 337 IFQ_SET_READY(&ifp->if_snd); 338 339 ic->ic_ifp = ifp; 340 ic->ic_wme.wme_update = iwi_wme_update; 341 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 342 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 343 ic->ic_state = IEEE80211_S_INIT; 344 345 /* set device capabilities */ 346 ic->ic_caps = 347 IEEE80211_C_IBSS | /* IBSS mode supported */ 348 IEEE80211_C_MONITOR | /* monitor mode supported */ 349 IEEE80211_C_TXPMGT | /* tx power management */ 350 IEEE80211_C_SHPREAMBLE | /* short preamble supported */ 351 IEEE80211_C_WPA | /* 802.11i */ 352 IEEE80211_C_WME; /* 802.11e */ 353 354 /* read MAC address from EEPROM */ 355 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 0); 356 ic->ic_myaddr[0] = val & 0xff; 357 ic->ic_myaddr[1] = val >> 8; 358 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 1); 359 ic->ic_myaddr[2] = val & 0xff; 360 ic->ic_myaddr[3] = val >> 8; 361 val = iwi_read_prom_word(sc, IWI_EEPROM_MAC + 2); 362 ic->ic_myaddr[4] = val & 0xff; 363 ic->ic_myaddr[5] = val >> 8; 364 365 if (pci_get_device(dev) >= 0x4223) { 366 /* set supported .11a rates (2915ABG only) */ 367 ic->ic_sup_rates[IEEE80211_MODE_11A] = iwi_rateset_11a; 368 369 /* set supported .11a channels */ 370 for (i = 36; i <= 64; i += 4) { 371 ic->ic_channels[i].ic_freq = 372 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 373 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 374 } 375 for (i = 149; i <= 165; i += 4) { 376 ic->ic_channels[i].ic_freq = 377 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ); 378 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A; 379 } 380 } 381 382 /* set supported .11b and .11g rates */ 383 ic->ic_sup_rates[IEEE80211_MODE_11B] = iwi_rateset_11b; 384 ic->ic_sup_rates[IEEE80211_MODE_11G] = iwi_rateset_11g; 385 386 /* set supported .11b and .11g channels (1 through 14) */ 387 for (i = 1; i <= 14; i++) { 388 ic->ic_channels[i].ic_freq = 389 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ); 390 ic->ic_channels[i].ic_flags = 391 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM | 392 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ; 393 } 394 395 ieee80211_ifattach(ic); 396 /* override default methods */ 397 ic->ic_node_alloc = iwi_node_alloc; 398 sc->sc_node_free = ic->ic_node_free; 399 ic->ic_node_free = iwi_node_free; 400 /* override state transition machine */ 401 sc->sc_newstate = ic->ic_newstate; 402 ic->ic_newstate = iwi_newstate; 403 ieee80211_media_init(ic, iwi_media_change, iwi_media_status); 404 405 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 406 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf); 407 408 sc->sc_rxtap_len = sizeof sc->sc_rxtapu; 409 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 410 sc->sc_rxtap.wr_ihdr.it_present = htole32(IWI_RX_RADIOTAP_PRESENT); 411 412 sc->sc_txtap_len = sizeof sc->sc_txtapu; 413 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 414 sc->sc_txtap.wt_ihdr.it_present = htole32(IWI_TX_RADIOTAP_PRESENT); 415 416 /* 417 * Add a few sysctl knobs. 418 */ 419 sc->dwelltime = 100; 420 sc->bluetooth = 1; 421 sc->antenna = 0; 422 423 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 424 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "radio", 425 CTLTYPE_INT | CTLFLAG_RD, sc, 0, iwi_sysctl_radio, "I", 426 "radio transmitter switch state (0=off, 1=on)"); 427 428 SYSCTL_ADD_PROC(device_get_sysctl_ctx(dev), 429 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "stats", 430 CTLTYPE_OPAQUE | CTLFLAG_RD, sc, 0, iwi_sysctl_stats, "S", 431 "statistics"); 432 433 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 434 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "dwell", 435 CTLFLAG_RW, &sc->dwelltime, 0, 436 "channel dwell time (ms) for AP/station scanning"); 437 438 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 439 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "bluetooth", 440 CTLFLAG_RW, &sc->bluetooth, 0, "bluetooth coexistence"); 441 442 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 443 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, "antenna", 444 CTLFLAG_RW, &sc->antenna, 0, "antenna (0=auto)"); 445 446 /* 447 * Hook our interrupt after all initialization is complete. 448 */ 449 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET | INTR_MPSAFE, 450 iwi_intr, sc, &sc->sc_ih); 451 if (error != 0) { 452 device_printf(dev, "could not set up interrupt\n"); 453 goto fail; 454 } 455 456 if (bootverbose) 457 ieee80211_announce(ic); 458 459 return 0; 460 461fail: iwi_detach(dev); 462 return ENXIO; 463} 464 465static int 466iwi_detach(device_t dev) 467{ 468 struct iwi_softc *sc = device_get_softc(dev); 469 struct ieee80211com *ic = &sc->sc_ic; 470 struct ifnet *ifp = ic->ic_ifp; 471 472 iwi_stop(sc); 473 474 if (ifp != NULL) { 475 bpfdetach(ifp); 476 ieee80211_ifdetach(ic); 477 } 478 479 iwi_free_cmd_ring(sc, &sc->cmdq); 480 iwi_free_tx_ring(sc, &sc->txq[0]); 481 iwi_free_tx_ring(sc, &sc->txq[1]); 482 iwi_free_tx_ring(sc, &sc->txq[2]); 483 iwi_free_tx_ring(sc, &sc->txq[3]); 484 iwi_free_rx_ring(sc, &sc->rxq); 485 486 if (sc->irq != NULL) { 487 bus_teardown_intr(dev, sc->irq, sc->sc_ih); 488 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 489 } 490 491 if (sc->mem != NULL) 492 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 493 494 if (ifp != NULL) 495 if_free(ifp); 496 497 if (sc->sc_unr != NULL) 498 delete_unrhdr(sc->sc_unr); 499 500 mtx_destroy(&sc->sc_mtx); 501 502 return 0; 503} 504 505static void 506iwi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 507{ 508 if (error != 0) 509 return; 510 511 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 512 513 *(bus_addr_t *)arg = segs[0].ds_addr; 514} 515 516static int 517iwi_alloc_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring, int count) 518{ 519 int error; 520 521 ring->count = count; 522 ring->queued = 0; 523 ring->cur = ring->next = 0; 524 525 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 526 BUS_SPACE_MAXADDR, NULL, NULL, count * IWI_CMD_DESC_SIZE, 1, 527 count * IWI_CMD_DESC_SIZE, 0, NULL, NULL, &ring->desc_dmat); 528 if (error != 0) { 529 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 530 goto fail; 531 } 532 533 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 534 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 535 if (error != 0) { 536 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 537 goto fail; 538 } 539 540 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 541 count * IWI_CMD_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0); 542 if (error != 0) { 543 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 544 goto fail; 545 } 546 547 return 0; 548 549fail: iwi_free_cmd_ring(sc, ring); 550 return error; 551} 552 553static void 554iwi_reset_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring) 555{ 556 ring->queued = 0; 557 ring->cur = ring->next = 0; 558} 559 560static void 561iwi_free_cmd_ring(struct iwi_softc *sc, struct iwi_cmd_ring *ring) 562{ 563 if (ring->desc != NULL) { 564 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 565 BUS_DMASYNC_POSTWRITE); 566 bus_dmamap_unload(ring->desc_dmat, ring->desc_map); 567 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map); 568 } 569 570 if (ring->desc_dmat != NULL) 571 bus_dma_tag_destroy(ring->desc_dmat); 572} 573 574static int 575iwi_alloc_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring, int count, 576 bus_addr_t csr_ridx, bus_addr_t csr_widx) 577{ 578 int i, error; 579 580 ring->count = count; 581 ring->queued = 0; 582 ring->cur = ring->next = 0; 583 ring->csr_ridx = csr_ridx; 584 ring->csr_widx = csr_widx; 585 586 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 587 BUS_SPACE_MAXADDR, NULL, NULL, count * IWI_TX_DESC_SIZE, 1, 588 count * IWI_TX_DESC_SIZE, 0, NULL, NULL, &ring->desc_dmat); 589 if (error != 0) { 590 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 591 goto fail; 592 } 593 594 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 595 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 596 if (error != 0) { 597 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 598 goto fail; 599 } 600 601 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 602 count * IWI_TX_DESC_SIZE, iwi_dma_map_addr, &ring->physaddr, 0); 603 if (error != 0) { 604 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 605 goto fail; 606 } 607 608 ring->data = malloc(count * sizeof (struct iwi_tx_data), M_DEVBUF, 609 M_NOWAIT | M_ZERO); 610 if (ring->data == NULL) { 611 device_printf(sc->sc_dev, "could not allocate soft data\n"); 612 error = ENOMEM; 613 goto fail; 614 } 615 616 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, 617 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL, 618 NULL, &ring->data_dmat); 619 if (error != 0) { 620 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 621 goto fail; 622 } 623 624 for (i = 0; i < count; i++) { 625 error = bus_dmamap_create(ring->data_dmat, 0, 626 &ring->data[i].map); 627 if (error != 0) { 628 device_printf(sc->sc_dev, "could not create DMA map\n"); 629 goto fail; 630 } 631 } 632 633 return 0; 634 635fail: iwi_free_tx_ring(sc, ring); 636 return error; 637} 638 639static void 640iwi_reset_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring) 641{ 642 struct iwi_tx_data *data; 643 int i; 644 645 for (i = 0; i < ring->count; i++) { 646 data = &ring->data[i]; 647 648 if (data->m != NULL) { 649 bus_dmamap_sync(ring->data_dmat, data->map, 650 BUS_DMASYNC_POSTWRITE); 651 bus_dmamap_unload(ring->data_dmat, data->map); 652 m_freem(data->m); 653 data->m = NULL; 654 } 655 656 if (data->ni != NULL) { 657 ieee80211_free_node(data->ni); 658 data->ni = NULL; 659 } 660 } 661 662 ring->queued = 0; 663 ring->cur = ring->next = 0; 664} 665 666static void 667iwi_free_tx_ring(struct iwi_softc *sc, struct iwi_tx_ring *ring) 668{ 669 struct iwi_tx_data *data; 670 int i; 671 672 if (ring->desc != NULL) { 673 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 674 BUS_DMASYNC_POSTWRITE); 675 bus_dmamap_unload(ring->desc_dmat, ring->desc_map); 676 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map); 677 } 678 679 if (ring->desc_dmat != NULL) 680 bus_dma_tag_destroy(ring->desc_dmat); 681 682 if (ring->data != NULL) { 683 for (i = 0; i < ring->count; i++) { 684 data = &ring->data[i]; 685 686 if (data->m != NULL) { 687 bus_dmamap_sync(ring->data_dmat, data->map, 688 BUS_DMASYNC_POSTWRITE); 689 bus_dmamap_unload(ring->data_dmat, data->map); 690 m_freem(data->m); 691 } 692 693 if (data->ni != NULL) 694 ieee80211_free_node(data->ni); 695 696 if (data->map != NULL) 697 bus_dmamap_destroy(ring->data_dmat, data->map); 698 } 699 700 free(ring->data, M_DEVBUF); 701 } 702 703 if (ring->data_dmat != NULL) 704 bus_dma_tag_destroy(ring->data_dmat); 705} 706 707static int 708iwi_alloc_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring, int count) 709{ 710 struct iwi_rx_data *data; 711 int i, error; 712 713 ring->count = count; 714 ring->cur = 0; 715 716 ring->data = malloc(count * sizeof (struct iwi_rx_data), M_DEVBUF, 717 M_NOWAIT | M_ZERO); 718 if (ring->data == NULL) { 719 device_printf(sc->sc_dev, "could not allocate soft data\n"); 720 error = ENOMEM; 721 goto fail; 722 } 723 724 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT, 725 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0, NULL, 726 NULL, &ring->data_dmat); 727 if (error != 0) { 728 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 729 goto fail; 730 } 731 732 for (i = 0; i < count; i++) { 733 data = &ring->data[i]; 734 735 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 736 if (error != 0) { 737 device_printf(sc->sc_dev, "could not create DMA map\n"); 738 goto fail; 739 } 740 741 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 742 if (data->m == NULL) { 743 device_printf(sc->sc_dev, 744 "could not allocate rx mbuf\n"); 745 error = ENOMEM; 746 goto fail; 747 } 748 749 error = bus_dmamap_load(ring->data_dmat, data->map, 750 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr, 751 &data->physaddr, 0); 752 if (error != 0) { 753 device_printf(sc->sc_dev, 754 "could not load rx buf DMA map"); 755 goto fail; 756 } 757 758 data->reg = IWI_CSR_RX_BASE + i * 4; 759 } 760 761 return 0; 762 763fail: iwi_free_rx_ring(sc, ring); 764 return error; 765} 766 767static void 768iwi_reset_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring) 769{ 770 ring->cur = 0; 771} 772 773static void 774iwi_free_rx_ring(struct iwi_softc *sc, struct iwi_rx_ring *ring) 775{ 776 struct iwi_rx_data *data; 777 int i; 778 779 if (ring->data != NULL) { 780 for (i = 0; i < ring->count; i++) { 781 data = &ring->data[i]; 782 783 if (data->m != NULL) { 784 bus_dmamap_sync(ring->data_dmat, data->map, 785 BUS_DMASYNC_POSTREAD); 786 bus_dmamap_unload(ring->data_dmat, data->map); 787 m_freem(data->m); 788 } 789 790 if (data->map != NULL) 791 bus_dmamap_destroy(ring->data_dmat, data->map); 792 } 793 794 free(ring->data, M_DEVBUF); 795 } 796 797 if (ring->data_dmat != NULL) 798 bus_dma_tag_destroy(ring->data_dmat); 799} 800 801static int 802iwi_shutdown(device_t dev) 803{ 804 struct iwi_softc *sc = device_get_softc(dev); 805 806 iwi_stop(sc); 807 808 return 0; 809} 810 811static int 812iwi_suspend(device_t dev) 813{ 814 struct iwi_softc *sc = device_get_softc(dev); 815 816 iwi_stop(sc); 817 818 return 0; 819} 820 821static int 822iwi_resume(device_t dev) 823{ 824 struct iwi_softc *sc = device_get_softc(dev); 825 struct ifnet *ifp = sc->sc_ic.ic_ifp; 826 827 IWI_LOCK(sc); 828 829 pci_write_config(dev, 0x41, 0, 1); 830 831 if (ifp->if_flags & IFF_UP) { 832 ifp->if_init(ifp->if_softc); 833 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 834 ifp->if_start(ifp); 835 } 836 837 IWI_UNLOCK(sc); 838 839 return 0; 840} 841 842static struct ieee80211_node * 843iwi_node_alloc(struct ieee80211_node_table *nt) 844{ 845 struct iwi_node *in; 846 847 in = malloc(sizeof (struct iwi_node), M_80211_NODE, M_NOWAIT | M_ZERO); 848 if (in == NULL) 849 return NULL; 850 851 in->in_station = -1; 852 853 return &in->in_node; 854} 855 856static void 857iwi_node_free(struct ieee80211_node *ni) 858{ 859 struct ieee80211com *ic = ni->ni_ic; 860 struct iwi_softc *sc = ic->ic_ifp->if_softc; 861 struct iwi_node *in = (struct iwi_node *)ni; 862 863 if (in->in_station != -1) 864 free_unr(sc->sc_unr, in->in_station); 865 866 sc->sc_node_free(ni); 867} 868 869static int 870iwi_media_change(struct ifnet *ifp) 871{ 872 struct iwi_softc *sc = ifp->if_softc; 873 int error; 874 875 IWI_LOCK(sc); 876 877 error = ieee80211_media_change(ifp); 878 if (error != ENETRESET) { 879 IWI_UNLOCK(sc); 880 return error; 881 } 882 883 if ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING)) 884 iwi_init(sc); 885 886 IWI_UNLOCK(sc); 887 888 return 0; 889} 890 891/* 892 * The firmware automatically adapts the transmit speed. We report its current 893 * value here. 894 */ 895static void 896iwi_media_status(struct ifnet *ifp, struct ifmediareq *imr) 897{ 898 struct iwi_softc *sc = ifp->if_softc; 899 struct ieee80211com *ic = &sc->sc_ic; 900#define N(a) (sizeof (a) / sizeof (a[0])) 901 static const struct { 902 uint32_t val; 903 int rate; 904 } rates[] = { 905 { IWI_RATE_DS1, 2 }, 906 { IWI_RATE_DS2, 4 }, 907 { IWI_RATE_DS5, 11 }, 908 { IWI_RATE_DS11, 22 }, 909 { IWI_RATE_OFDM6, 12 }, 910 { IWI_RATE_OFDM9, 18 }, 911 { IWI_RATE_OFDM12, 24 }, 912 { IWI_RATE_OFDM18, 36 }, 913 { IWI_RATE_OFDM24, 48 }, 914 { IWI_RATE_OFDM36, 72 }, 915 { IWI_RATE_OFDM48, 96 }, 916 { IWI_RATE_OFDM54, 108 }, 917 }; 918 uint32_t val; 919 int rate, i; 920 921 imr->ifm_status = IFM_AVALID; 922 imr->ifm_active = IFM_IEEE80211; 923 if (ic->ic_state == IEEE80211_S_RUN) 924 imr->ifm_status |= IFM_ACTIVE; 925 926 /* read current transmission rate from adapter */ 927 val = CSR_READ_4(sc, IWI_CSR_CURRENT_TX_RATE); 928 929 /* convert rate to 802.11 rate */ 930 for (i = 0; i < N(rates) && rates[i].val != val; i++); 931 rate = (i < N(rates)) ? rates[i].rate : 0; 932 933 imr->ifm_active |= ieee80211_rate2media(ic, rate, ic->ic_curmode); 934 switch (ic->ic_opmode) { 935 case IEEE80211_M_STA: 936 break; 937 938 case IEEE80211_M_IBSS: 939 imr->ifm_active |= IFM_IEEE80211_ADHOC; 940 break; 941 942 case IEEE80211_M_MONITOR: 943 imr->ifm_active |= IFM_IEEE80211_MONITOR; 944 break; 945 946 case IEEE80211_M_AHDEMO: 947 case IEEE80211_M_HOSTAP: 948 /* should not get there */ 949 break; 950 } 951#undef N 952} 953 954static int 955iwi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 956{ 957 struct ifnet *ifp = ic->ic_ifp; 958 struct iwi_softc *sc = ifp->if_softc; 959 960 switch (nstate) { 961 case IEEE80211_S_SCAN: 962 if (sc->flags & IWI_FLAG_SCANNING) 963 break; 964 965 ieee80211_node_table_reset(&ic->ic_scan); 966 ic->ic_flags |= IEEE80211_F_SCAN | IEEE80211_F_ASCAN; 967 sc->flags |= IWI_FLAG_SCANNING; 968 iwi_scan(sc); 969 break; 970 971 case IEEE80211_S_AUTH: 972 iwi_auth_and_assoc(sc); 973 break; 974 975 case IEEE80211_S_RUN: 976 if (ic->ic_opmode == IEEE80211_M_IBSS) 977 ieee80211_new_state(ic, IEEE80211_S_AUTH, -1); 978 else if (ic->ic_opmode == IEEE80211_M_MONITOR) 979 iwi_set_chan(sc, ic->ic_ibss_chan); 980 981 return sc->sc_newstate(ic, nstate, 982 IEEE80211_FC0_SUBTYPE_ASSOC_RESP); 983 984 case IEEE80211_S_ASSOC: 985 break; 986 987 case IEEE80211_S_INIT: 988 sc->flags &= ~IWI_FLAG_SCANNING; 989 break; 990 } 991 992 ic->ic_state = nstate; 993 return 0; 994} 995 996/* 997 * WME parameters coming from IEEE 802.11e specification. These values are 998 * already declared in ieee80211_proto.c, but they are static so they can't 999 * be reused here. 1000 */ 1001static const struct wmeParams iwi_wme_cck_params[WME_NUM_AC] = { 1002 { 0, 3, 5, 7, 0 }, /* WME_AC_BE */ 1003 { 0, 3, 5, 10, 0 }, /* WME_AC_BK */ 1004 { 0, 2, 4, 5, 188 }, /* WME_AC_VI */ 1005 { 0, 2, 3, 4, 102 } /* WME_AC_VO */ 1006}; 1007 1008static const struct wmeParams iwi_wme_ofdm_params[WME_NUM_AC] = { 1009 { 0, 3, 4, 6, 0 }, /* WME_AC_BE */ 1010 { 0, 3, 4, 10, 0 }, /* WME_AC_BK */ 1011 { 0, 2, 3, 4, 94 }, /* WME_AC_VI */ 1012 { 0, 2, 2, 3, 47 } /* WME_AC_VO */ 1013}; 1014 1015static int 1016iwi_wme_update(struct ieee80211com *ic) 1017{ 1018#define IWI_EXP2(v) htole16((1 << (v)) - 1) 1019#define IWI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v)) 1020 struct iwi_softc *sc = ic->ic_ifp->if_softc; 1021 struct iwi_wme_params wme[3]; 1022 const struct wmeParams *wmep; 1023 int ac; 1024 1025 /* 1026 * We shall not override firmware default WME values if WME is not 1027 * actually enabled. 1028 */ 1029 if (!(ic->ic_flags & IEEE80211_F_WME)) 1030 return 0; 1031 1032 for (ac = 0; ac < WME_NUM_AC; ac++) { 1033 /* set WME values for current operating mode */ 1034 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac]; 1035 wme[0].aifsn[ac] = wmep->wmep_aifsn; 1036 wme[0].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin); 1037 wme[0].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax); 1038 wme[0].burst[ac] = IWI_USEC(wmep->wmep_txopLimit); 1039 wme[0].acm[ac] = wmep->wmep_acm; 1040 1041 /* set WME values for CCK modulation */ 1042 wmep = &iwi_wme_cck_params[ac]; 1043 wme[1].aifsn[ac] = wmep->wmep_aifsn; 1044 wme[1].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin); 1045 wme[1].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax); 1046 wme[1].burst[ac] = IWI_USEC(wmep->wmep_txopLimit); 1047 wme[1].acm[ac] = wmep->wmep_acm; 1048 1049 /* set WME values for OFDM modulation */ 1050 wmep = &iwi_wme_ofdm_params[ac]; 1051 wme[2].aifsn[ac] = wmep->wmep_aifsn; 1052 wme[2].cwmin[ac] = IWI_EXP2(wmep->wmep_logcwmin); 1053 wme[2].cwmax[ac] = IWI_EXP2(wmep->wmep_logcwmax); 1054 wme[2].burst[ac] = IWI_USEC(wmep->wmep_txopLimit); 1055 wme[2].acm[ac] = wmep->wmep_acm; 1056 } 1057 1058 DPRINTF(("Setting WME parameters\n")); 1059 return iwi_cmd(sc, IWI_CMD_SET_WME_PARAMS, wme, sizeof wme, 1); 1060#undef IWI_USEC 1061#undef IWI_EXP2 1062} 1063 1064/* 1065 * Read 16 bits at address 'addr' from the serial EEPROM. 1066 */ 1067static uint16_t 1068iwi_read_prom_word(struct iwi_softc *sc, uint8_t addr) 1069{ 1070 uint32_t tmp; 1071 uint16_t val; 1072 int n; 1073 1074 /* clock C once before the first command */ 1075 IWI_EEPROM_CTL(sc, 0); 1076 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1077 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 1078 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1079 1080 /* write start bit (1) */ 1081 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D); 1082 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C); 1083 1084 /* write READ opcode (10) */ 1085 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D); 1086 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_D | IWI_EEPROM_C); 1087 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1088 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 1089 1090 /* write address A7-A0 */ 1091 for (n = 7; n >= 0; n--) { 1092 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | 1093 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D)); 1094 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | 1095 (((addr >> n) & 1) << IWI_EEPROM_SHIFT_D) | IWI_EEPROM_C); 1096 } 1097 1098 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1099 1100 /* read data Q15-Q0 */ 1101 val = 0; 1102 for (n = 15; n >= 0; n--) { 1103 IWI_EEPROM_CTL(sc, IWI_EEPROM_S | IWI_EEPROM_C); 1104 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1105 tmp = MEM_READ_4(sc, IWI_MEM_EEPROM_CTL); 1106 val |= ((tmp & IWI_EEPROM_Q) >> IWI_EEPROM_SHIFT_Q) << n; 1107 } 1108 1109 IWI_EEPROM_CTL(sc, 0); 1110 1111 /* clear Chip Select and clock C */ 1112 IWI_EEPROM_CTL(sc, IWI_EEPROM_S); 1113 IWI_EEPROM_CTL(sc, 0); 1114 IWI_EEPROM_CTL(sc, IWI_EEPROM_C); 1115 1116 return val; 1117} 1118 1119/* 1120 * XXX: Hack to set the current channel to the value advertised in beacons or 1121 * probe responses. Only used during AP detection. 1122 */ 1123static void 1124iwi_fix_channel(struct ieee80211com *ic, struct mbuf *m) 1125{ 1126 struct ieee80211_frame *wh; 1127 uint8_t subtype; 1128 uint8_t *frm, *efrm; 1129 1130 wh = mtod(m, struct ieee80211_frame *); 1131 1132 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) != IEEE80211_FC0_TYPE_MGT) 1133 return; 1134 1135 subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 1136 1137 if (subtype != IEEE80211_FC0_SUBTYPE_BEACON && 1138 subtype != IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1139 return; 1140 1141 frm = (uint8_t *)(wh + 1); 1142 efrm = mtod(m, uint8_t *) + m->m_len; 1143 1144 frm += 12; /* skip tstamp, bintval and capinfo fields */ 1145 while (frm < efrm) { 1146 if (*frm == IEEE80211_ELEMID_DSPARMS) 1147#if IEEE80211_CHAN_MAX < 255 1148 if (frm[2] <= IEEE80211_CHAN_MAX) 1149#endif 1150 ic->ic_curchan = &ic->ic_channels[frm[2]]; 1151 1152 frm += frm[1] + 2; 1153 } 1154} 1155 1156static void 1157iwi_frame_intr(struct iwi_softc *sc, struct iwi_rx_data *data, int i, 1158 struct iwi_frame *frame) 1159{ 1160 struct ieee80211com *ic = &sc->sc_ic; 1161 struct ifnet *ifp = ic->ic_ifp; 1162 struct mbuf *mnew, *m; 1163 struct ieee80211_frame *wh; 1164 struct ieee80211_node *ni; 1165 int error; 1166 1167 DPRINTFN(5, ("received frame len=%u chan=%u rssi=%u\n", 1168 le16toh(frame->len), frame->chan, frame->rssi_dbm)); 1169 1170 if (le16toh(frame->len) < sizeof (struct ieee80211_frame)) 1171 return; 1172 1173 /* 1174 * Try to allocate a new mbuf for this ring element and load it before 1175 * processing the current mbuf. If the ring element cannot be loaded, 1176 * drop the received packet and reuse the old mbuf. In the unlikely 1177 * case that the old mbuf can't be reloaded either, explicitly panic. 1178 */ 1179 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1180 if (mnew == NULL) { 1181 ifp->if_ierrors++; 1182 return; 1183 } 1184 1185 bus_dmamap_unload(sc->rxq.data_dmat, data->map); 1186 1187 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1188 mtod(mnew, void *), MCLBYTES, iwi_dma_map_addr, &data->physaddr, 1189 0); 1190 if (error != 0) { 1191 m_freem(mnew); 1192 1193 /* try to reload the old mbuf */ 1194 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1195 mtod(data->m, void *), MCLBYTES, iwi_dma_map_addr, 1196 &data->physaddr, 0); 1197 if (error != 0) { 1198 /* very unlikely that it will fail... */ 1199 panic("%s: could not load old rx mbuf", 1200 device_get_name(sc->sc_dev)); 1201 } 1202 ifp->if_ierrors++; 1203 return; 1204 } 1205 1206 /* 1207 * New mbuf successfully loaded, update Rx ring and continue 1208 * processing. 1209 */ 1210 m = data->m; 1211 data->m = mnew; 1212 CSR_WRITE_4(sc, data->reg, data->physaddr); 1213 1214 /* finalize mbuf */ 1215 m->m_pkthdr.rcvif = ifp; 1216 m->m_pkthdr.len = m->m_len = sizeof (struct iwi_hdr) + 1217 sizeof (struct iwi_frame) + le16toh(frame->len); 1218 1219 m_adj(m, sizeof (struct iwi_hdr) + sizeof (struct iwi_frame)); 1220 1221 if (ic->ic_state == IEEE80211_S_SCAN) 1222 iwi_fix_channel(ic, m); 1223 1224 if (sc->sc_drvbpf != NULL) { 1225 struct iwi_rx_radiotap_header *tap = &sc->sc_rxtap; 1226 1227 tap->wr_flags = 0; 1228 tap->wr_rate = frame->rate; 1229 tap->wr_chan_freq = 1230 htole16(ic->ic_channels[frame->chan].ic_freq); 1231 tap->wr_chan_flags = 1232 htole16(ic->ic_channels[frame->chan].ic_flags); 1233 tap->wr_antsignal = frame->signal; 1234 tap->wr_antenna = frame->antenna; 1235 1236 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 1237 } 1238 1239 wh = mtod(m, struct ieee80211_frame *); 1240 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 1241 1242 /* send the frame to the 802.11 layer */ 1243 ieee80211_input(ic, m, ni, frame->rssi_dbm, 0); 1244 1245 /* node is no longer needed */ 1246 ieee80211_free_node(ni); 1247} 1248 1249static void 1250iwi_notification_intr(struct iwi_softc *sc, struct iwi_notif *notif) 1251{ 1252 struct ieee80211com *ic = &sc->sc_ic; 1253 struct iwi_notif_scan_channel *chan; 1254 struct iwi_notif_scan_complete *scan; 1255 struct iwi_notif_authentication *auth; 1256 struct iwi_notif_association *assoc; 1257 1258 switch (notif->type) { 1259 case IWI_NOTIF_TYPE_SCAN_CHANNEL: 1260 chan = (struct iwi_notif_scan_channel *)(notif + 1); 1261 1262 DPRINTFN(2, ("Scanning channel (%u)\n", chan->nchan)); 1263 break; 1264 1265 case IWI_NOTIF_TYPE_SCAN_COMPLETE: 1266 scan = (struct iwi_notif_scan_complete *)(notif + 1); 1267 1268 DPRINTFN(2, ("Scan completed (%u, %u)\n", scan->nchan, 1269 scan->status)); 1270 1271 /* monitor mode uses scan to set the channel ... */ 1272 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 1273 sc->flags &= ~IWI_FLAG_SCANNING; 1274 ieee80211_end_scan(ic); 1275 } else 1276 iwi_set_chan(sc, ic->ic_ibss_chan); 1277 break; 1278 1279 case IWI_NOTIF_TYPE_AUTHENTICATION: 1280 auth = (struct iwi_notif_authentication *)(notif + 1); 1281 1282 DPRINTFN(2, ("Authentication (%u)\n", auth->state)); 1283 1284 switch (auth->state) { 1285 case IWI_AUTHENTICATED: 1286 ieee80211_node_authorize(ic->ic_bss); 1287 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1); 1288 break; 1289 1290 case IWI_DEAUTHENTICATED: 1291 break; 1292 1293 default: 1294 device_printf(sc->sc_dev, 1295 "unknown authentication state %u\n", auth->state); 1296 } 1297 break; 1298 1299 case IWI_NOTIF_TYPE_ASSOCIATION: 1300 assoc = (struct iwi_notif_association *)(notif + 1); 1301 1302 DPRINTFN(2, ("Association (%u, %u)\n", assoc->state, 1303 assoc->status)); 1304 1305 switch (assoc->state) { 1306 case IWI_AUTHENTICATED: 1307 /* re-association, do nothing */ 1308 break; 1309 1310 case IWI_ASSOCIATED: 1311 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 1312 break; 1313 1314 case IWI_DEASSOCIATED: 1315 ieee80211_begin_scan(ic, 1); 1316 break; 1317 1318 default: 1319 device_printf(sc->sc_dev, 1320 "unknown association state %u\n", assoc->state); 1321 } 1322 break; 1323 1324 case IWI_NOTIF_TYPE_CALIBRATION: 1325 case IWI_NOTIF_TYPE_BEACON: 1326 case IWI_NOTIF_TYPE_NOISE: 1327 DPRINTFN(5, ("Notification (%u)\n", notif->type)); 1328 break; 1329 1330 default: 1331 device_printf(sc->sc_dev, "unknown notification type %u\n", 1332 notif->type); 1333 } 1334} 1335 1336static void 1337iwi_rx_intr(struct iwi_softc *sc) 1338{ 1339 struct iwi_rx_data *data; 1340 struct iwi_hdr *hdr; 1341 uint32_t hw; 1342 1343 hw = CSR_READ_4(sc, IWI_CSR_RX_RIDX); 1344 1345 for (; sc->rxq.cur != hw;) { 1346 data = &sc->rxq.data[sc->rxq.cur]; 1347 1348 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 1349 BUS_DMASYNC_POSTREAD); 1350 1351 hdr = mtod(data->m, struct iwi_hdr *); 1352 1353 switch (hdr->type) { 1354 case IWI_HDR_TYPE_FRAME: 1355 iwi_frame_intr(sc, data, sc->rxq.cur, 1356 (struct iwi_frame *)(hdr + 1)); 1357 break; 1358 1359 case IWI_HDR_TYPE_NOTIF: 1360 iwi_notification_intr(sc, 1361 (struct iwi_notif *)(hdr + 1)); 1362 break; 1363 1364 default: 1365 device_printf(sc->sc_dev, "unknown hdr type %u\n", 1366 hdr->type); 1367 } 1368 1369 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur)); 1370 1371 sc->rxq.cur = (sc->rxq.cur + 1) % IWI_RX_RING_COUNT; 1372 } 1373 1374 /* tell the firmware what we have processed */ 1375 hw = (hw == 0) ? IWI_RX_RING_COUNT - 1 : hw - 1; 1376 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, hw); 1377} 1378 1379static void 1380iwi_tx_intr(struct iwi_softc *sc, struct iwi_tx_ring *txq) 1381{ 1382 struct ieee80211com *ic = &sc->sc_ic; 1383 struct ifnet *ifp = ic->ic_ifp; 1384 struct iwi_tx_data *data; 1385 uint32_t hw; 1386 1387 hw = CSR_READ_4(sc, txq->csr_ridx); 1388 1389 for (; txq->next != hw;) { 1390 data = &txq->data[txq->next]; 1391 1392 bus_dmamap_sync(txq->data_dmat, data->map, 1393 BUS_DMASYNC_POSTWRITE); 1394 bus_dmamap_unload(txq->data_dmat, data->map); 1395 m_freem(data->m); 1396 data->m = NULL; 1397 ieee80211_free_node(data->ni); 1398 data->ni = NULL; 1399 1400 DPRINTFN(15, ("tx done idx=%u\n", txq->next)); 1401 1402 ifp->if_opackets++; 1403 1404 txq->queued--; 1405 txq->next = (txq->next + 1) % IWI_TX_RING_COUNT; 1406 } 1407 1408 sc->sc_tx_timer = 0; 1409 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1410 iwi_start(ifp); 1411} 1412 1413static void 1414iwi_intr(void *arg) 1415{ 1416 struct iwi_softc *sc = arg; 1417 uint32_t r; 1418 1419 IWI_LOCK(sc); 1420 1421 if ((r = CSR_READ_4(sc, IWI_CSR_INTR)) == 0 || r == 0xffffffff) { 1422 IWI_UNLOCK(sc); 1423 return; 1424 } 1425 1426 /* disable interrupts */ 1427 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0); 1428 1429 if (r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR)) { 1430 device_printf(sc->sc_dev, "fatal error\n"); 1431 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP; 1432 iwi_stop(sc); 1433 } 1434 1435 if (r & IWI_INTR_FW_INITED) { 1436 if (!(r & (IWI_INTR_FATAL_ERROR | IWI_INTR_PARITY_ERROR))) 1437 wakeup(sc); 1438 } 1439 1440 if (r & IWI_INTR_RADIO_OFF) { 1441 DPRINTF(("radio transmitter turned off\n")); 1442 sc->sc_ic.ic_ifp->if_flags &= ~IFF_UP; 1443 iwi_stop(sc); 1444 } 1445 1446 if (r & IWI_INTR_CMD_DONE) 1447 wakeup(sc); 1448 1449 if (r & IWI_INTR_TX1_DONE) 1450 iwi_tx_intr(sc, &sc->txq[0]); 1451 1452 if (r & IWI_INTR_TX2_DONE) 1453 iwi_tx_intr(sc, &sc->txq[1]); 1454 1455 if (r & IWI_INTR_TX3_DONE) 1456 iwi_tx_intr(sc, &sc->txq[2]); 1457 1458 if (r & IWI_INTR_TX4_DONE) 1459 iwi_tx_intr(sc, &sc->txq[3]); 1460 1461 if (r & IWI_INTR_RX_DONE) 1462 iwi_rx_intr(sc); 1463 1464 /* acknowledge interrupts */ 1465 CSR_WRITE_4(sc, IWI_CSR_INTR, r); 1466 1467 /* re-enable interrupts */ 1468 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK); 1469 1470 IWI_UNLOCK(sc); 1471} 1472 1473static int 1474iwi_cmd(struct iwi_softc *sc, uint8_t type, void *data, uint8_t len, int async) 1475{ 1476 struct iwi_cmd_desc *desc; 1477 1478 desc = &sc->cmdq.desc[sc->cmdq.cur]; 1479 1480 desc->hdr.type = IWI_HDR_TYPE_COMMAND; 1481 desc->hdr.flags = IWI_HDR_FLAG_IRQ; 1482 desc->type = type; 1483 desc->len = len; 1484 memcpy(desc->data, data, len); 1485 1486 bus_dmamap_sync(sc->cmdq.desc_dmat, sc->cmdq.desc_map, 1487 BUS_DMASYNC_PREWRITE); 1488 1489 DPRINTFN(2, ("sending command idx=%u type=%u len=%u\n", sc->cmdq.cur, 1490 type, len)); 1491 1492 sc->cmdq.cur = (sc->cmdq.cur + 1) % IWI_CMD_RING_COUNT; 1493 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur); 1494 1495 return async ? 0 : msleep(sc, &sc->sc_mtx, 0, "iwicmd", hz); 1496} 1497 1498static void 1499iwi_write_ibssnode(struct iwi_softc *sc, const struct iwi_node *in) 1500{ 1501 struct iwi_ibssnode node; 1502 1503 /* write node information into NIC memory */ 1504 memset(&node, 0, sizeof node); 1505 IEEE80211_ADDR_COPY(node.bssid, in->in_node.ni_macaddr); 1506 1507 CSR_WRITE_REGION_1(sc, 1508 IWI_CSR_NODE_BASE + in->in_station * sizeof node, 1509 (uint8_t *)&node, sizeof node); 1510} 1511 1512static int 1513iwi_tx_start(struct ifnet *ifp, struct mbuf *m0, struct ieee80211_node *ni, 1514 int ac) 1515{ 1516 struct iwi_softc *sc = ifp->if_softc; 1517 struct ieee80211com *ic = &sc->sc_ic; 1518 struct iwi_node *in = (struct iwi_node *)ni; 1519 struct ieee80211_frame *wh; 1520 struct ieee80211_key *k; 1521 const struct chanAccParams *cap; 1522 struct iwi_tx_ring *txq = &sc->txq[ac]; 1523 struct iwi_tx_data *data; 1524 struct iwi_tx_desc *desc; 1525 struct mbuf *mnew; 1526 bus_dma_segment_t segs[IWI_MAX_NSEG]; 1527 int error, nsegs, hdrlen, i, noack = 0; 1528 1529 wh = mtod(m0, struct ieee80211_frame *); 1530 1531 if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) { 1532 hdrlen = sizeof (struct ieee80211_qosframe); 1533 cap = &ic->ic_wme.wme_chanParams; 1534 noack = cap->cap_wmeParams[ac].wmep_noackPolicy; 1535 } else 1536 hdrlen = sizeof (struct ieee80211_frame); 1537 1538 /* 1539 * This is only used in IBSS mode where the firmware expect an index 1540 * in a h/w table instead of a destination address. 1541 */ 1542 if (ic->ic_opmode == IEEE80211_M_IBSS && in->in_station == -1) { 1543 in->in_station = alloc_unr(sc->sc_unr); 1544 if (in->in_station == -1) { /* h/w table is full */ 1545 m_freem(m0); 1546 ieee80211_free_node(ni); 1547 ifp->if_oerrors++; 1548 return 0; 1549 } 1550 iwi_write_ibssnode(sc, in); 1551 } 1552 1553 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1554 k = ieee80211_crypto_encap(ic, ni, m0); 1555 if (k == NULL) { 1556 m_freem(m0); 1557 return ENOBUFS; 1558 } 1559 1560 /* packet header may have moved, reset our local pointer */ 1561 wh = mtod(m0, struct ieee80211_frame *); 1562 } 1563 1564 if (sc->sc_drvbpf != NULL) { 1565 struct iwi_tx_radiotap_header *tap = &sc->sc_txtap; 1566 1567 tap->wt_flags = 0; 1568 tap->wt_chan_freq = htole16(ic->ic_ibss_chan->ic_freq); 1569 tap->wt_chan_flags = htole16(ic->ic_ibss_chan->ic_flags); 1570 1571 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 1572 } 1573 1574 data = &txq->data[txq->cur]; 1575 desc = &txq->desc[txq->cur]; 1576 1577 /* save and trim IEEE802.11 header */ 1578 m_copydata(m0, 0, hdrlen, (caddr_t)&desc->wh); 1579 m_adj(m0, hdrlen); 1580 1581 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, m0, segs, 1582 &nsegs, 0); 1583 if (error != 0 && error != EFBIG) { 1584 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1585 error); 1586 m_freem(m0); 1587 return error; 1588 } 1589 if (error != 0) { 1590 mnew = m_defrag(m0, M_DONTWAIT); 1591 if (mnew == NULL) { 1592 device_printf(sc->sc_dev, 1593 "could not defragment mbuf\n"); 1594 m_freem(m0); 1595 return ENOBUFS; 1596 } 1597 m0 = mnew; 1598 1599 error = bus_dmamap_load_mbuf_sg(txq->data_dmat, data->map, 1600 m0, segs, &nsegs, 0); 1601 if (error != 0) { 1602 device_printf(sc->sc_dev, 1603 "could not map mbuf (error %d)\n", error); 1604 m_freem(m0); 1605 return error; 1606 } 1607 } 1608 1609 data->m = m0; 1610 data->ni = ni; 1611 1612 desc->hdr.type = IWI_HDR_TYPE_DATA; 1613 desc->hdr.flags = IWI_HDR_FLAG_IRQ; 1614 desc->station = 1615 (ic->ic_opmode == IEEE80211_M_IBSS) ? in->in_station : 0; 1616 desc->cmd = IWI_DATA_CMD_TX; 1617 desc->len = htole16(m0->m_pkthdr.len); 1618 desc->flags = 0; 1619 desc->xflags = 0; 1620 1621 if (!noack && !IEEE80211_IS_MULTICAST(desc->wh.i_addr1)) 1622 desc->flags |= IWI_DATA_FLAG_NEED_ACK; 1623 1624#if 0 1625 if (ic->ic_flags & IEEE80211_F_PRIVACY) { 1626 desc->wh.i_fc[1] |= IEEE80211_FC1_WEP; 1627 desc->wep_txkey = ic->ic_crypto.cs_def_txkey; 1628 } else 1629#endif 1630 desc->flags |= IWI_DATA_FLAG_NO_WEP; 1631 1632 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1633 desc->flags |= IWI_DATA_FLAG_SHPREAMBLE; 1634 1635 if (desc->wh.i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) 1636 desc->xflags |= IWI_DATA_XFLAG_QOS; 1637 1638 desc->nseg = htole32(nsegs); 1639 for (i = 0; i < nsegs; i++) { 1640 desc->seg_addr[i] = htole32(segs[i].ds_addr); 1641 desc->seg_len[i] = htole16(segs[i].ds_len); 1642 } 1643 1644 bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1645 bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE); 1646 1647 DPRINTFN(5, ("sending data frame txq=%u idx=%u len=%u nseg=%u\n", 1648 ac, txq->cur, le16toh(desc->len), nsegs)); 1649 1650 txq->queued++; 1651 txq->cur = (txq->cur + 1) % IWI_TX_RING_COUNT; 1652 CSR_WRITE_4(sc, txq->csr_widx, txq->cur); 1653 1654 return 0; 1655} 1656 1657static void 1658iwi_start(struct ifnet *ifp) 1659{ 1660 struct iwi_softc *sc = ifp->if_softc; 1661 struct ieee80211com *ic = &sc->sc_ic; 1662 struct mbuf *m0; 1663 struct ether_header *eh; 1664 struct ieee80211_node *ni; 1665 int ac; 1666 1667 IWI_LOCK(sc); 1668 1669 if (ic->ic_state != IEEE80211_S_RUN) { 1670 IWI_UNLOCK(sc); 1671 return; 1672 } 1673 1674 for (;;) { 1675 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 1676 if (m0 == NULL) 1677 break; 1678 1679 if (m0->m_len < sizeof (struct ether_header) && 1680 (m0 = m_pullup(m0, sizeof (struct ether_header))) == NULL) { 1681 ifp->if_oerrors++; 1682 continue; 1683 } 1684 eh = mtod(m0, struct ether_header *); 1685 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 1686 if (ni == NULL) { 1687 m_freem(m0); 1688 ifp->if_oerrors++; 1689 continue; 1690 } 1691 1692 /* classify mbuf so we can find which tx ring to use */ 1693 if (ieee80211_classify(ic, m0, ni) != 0) { 1694 m_freem(m0); 1695 ieee80211_free_node(ni); 1696 ifp->if_oerrors++; 1697 continue; 1698 } 1699 1700 /* no QoS encapsulation for EAPOL frames */ 1701 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ? 1702 M_WME_GETAC(m0) : WME_AC_BE; 1703 1704 if (sc->txq[ac].queued > IWI_TX_RING_COUNT - 8) { 1705 /* there is no place left in this ring */ 1706 IFQ_DRV_PREPEND(&ifp->if_snd, m0); 1707 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1708 break; 1709 } 1710 1711 BPF_MTAP(ifp, m0); 1712 1713 m0 = ieee80211_encap(ic, m0, ni); 1714 if (m0 == NULL) { 1715 ieee80211_free_node(ni); 1716 ifp->if_oerrors++; 1717 continue; 1718 } 1719 1720 if (ic->ic_rawbpf != NULL) 1721 bpf_mtap(ic->ic_rawbpf, m0); 1722 1723 if (iwi_tx_start(ifp, m0, ni, ac) != 0) { 1724 ieee80211_free_node(ni); 1725 ifp->if_oerrors++; 1726 break; 1727 } 1728 1729 sc->sc_tx_timer = 5; 1730 ifp->if_timer = 1; 1731 } 1732 1733 IWI_UNLOCK(sc); 1734} 1735 1736static void 1737iwi_watchdog(struct ifnet *ifp) 1738{ 1739 struct iwi_softc *sc = ifp->if_softc; 1740 struct ieee80211com *ic = &sc->sc_ic; 1741 1742 IWI_LOCK(sc); 1743 1744 ifp->if_timer = 0; 1745 1746 if (sc->sc_tx_timer > 0) { 1747 if (--sc->sc_tx_timer == 0) { 1748 if_printf(ifp, "device timeout\n"); 1749 ifp->if_oerrors++; 1750 ifp->if_flags &= ~IFF_UP; 1751 iwi_stop(sc); 1752 IWI_UNLOCK(sc); 1753 return; 1754 } 1755 ifp->if_timer = 1; 1756 } 1757 1758 ieee80211_watchdog(ic); 1759 1760 IWI_UNLOCK(sc); 1761} 1762 1763static int 1764iwi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 1765{ 1766 struct iwi_softc *sc = ifp->if_softc; 1767 struct ieee80211com *ic = &sc->sc_ic; 1768 int error = 0; 1769 1770 IWI_LOCK(sc); 1771 1772 switch (cmd) { 1773 case SIOCSIFFLAGS: 1774 if (ifp->if_flags & IFF_UP) { 1775 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 1776 iwi_init(sc); 1777 } else { 1778 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1779 iwi_stop(sc); 1780 } 1781 break; 1782 1783 default: 1784 error = ieee80211_ioctl(ic, cmd, data); 1785 } 1786 1787 if (error == ENETRESET) { 1788 if ((ifp->if_flags & IFF_UP) && 1789 (ifp->if_drv_flags & IFF_DRV_RUNNING) && 1790 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)) 1791 iwi_init(sc); 1792 error = 0; 1793 } 1794 1795 IWI_UNLOCK(sc); 1796 1797 return error; 1798} 1799 1800static void 1801iwi_stop_master(struct iwi_softc *sc) 1802{ 1803 uint32_t tmp; 1804 int ntries; 1805 1806 /* disable interrupts */ 1807 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, 0); 1808 1809 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_STOP_MASTER); 1810 for (ntries = 0; ntries < 5; ntries++) { 1811 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED) 1812 break; 1813 DELAY(10); 1814 } 1815 if (ntries == 5) 1816 device_printf(sc->sc_dev, "timeout waiting for master\n"); 1817 1818 tmp = CSR_READ_4(sc, IWI_CSR_RST); 1819 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_PRINCETON_RESET); 1820 1821 sc->flags &= ~IWI_FLAG_FW_INITED; 1822} 1823 1824static int 1825iwi_reset(struct iwi_softc *sc) 1826{ 1827 uint32_t tmp; 1828 int i, ntries; 1829 1830 iwi_stop_master(sc); 1831 1832 tmp = CSR_READ_4(sc, IWI_CSR_CTL); 1833 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT); 1834 1835 CSR_WRITE_4(sc, IWI_CSR_READ_INT, IWI_READ_INT_INIT_HOST); 1836 1837 /* wait for clock stabilization */ 1838 for (ntries = 0; ntries < 1000; ntries++) { 1839 if (CSR_READ_4(sc, IWI_CSR_CTL) & IWI_CTL_CLOCK_READY) 1840 break; 1841 DELAY(200); 1842 } 1843 if (ntries == 1000) { 1844 device_printf(sc->sc_dev, 1845 "timeout waiting for clock stabilization\n"); 1846 return EIO; 1847 } 1848 1849 tmp = CSR_READ_4(sc, IWI_CSR_RST); 1850 CSR_WRITE_4(sc, IWI_CSR_RST, tmp | IWI_RST_SOFT_RESET); 1851 1852 DELAY(10); 1853 1854 tmp = CSR_READ_4(sc, IWI_CSR_CTL); 1855 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_INIT); 1856 1857 /* clear NIC memory */ 1858 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0); 1859 for (i = 0; i < 0xc000; i++) 1860 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0); 1861 1862 return 0; 1863} 1864 1865static int 1866iwi_load_ucode(struct iwi_softc *sc, const char *name) 1867{ 1868 uint32_t tmp; 1869 uint16_t *w; 1870 char *uc; 1871 size_t size; 1872 int i, ntries, error; 1873 1874 IWI_UNLOCK(sc); 1875 error = iwi_read_firmware(name, &uc, &size); 1876 IWI_LOCK(sc); 1877 if (error != 0) { 1878 device_printf(sc->sc_dev, "could not read ucode image\n"); 1879 goto fail1; 1880 } 1881 1882 if (size < sizeof (struct iwi_firmware_hdr)) { 1883 error = EINVAL; 1884 goto fail2; 1885 } 1886 1887 uc += sizeof (struct iwi_firmware_hdr); 1888 size -= sizeof (struct iwi_firmware_hdr); 1889 1890 CSR_WRITE_4(sc, IWI_CSR_RST, CSR_READ_4(sc, IWI_CSR_RST) | 1891 IWI_RST_STOP_MASTER); 1892 for (ntries = 0; ntries < 5; ntries++) { 1893 if (CSR_READ_4(sc, IWI_CSR_RST) & IWI_RST_MASTER_DISABLED) 1894 break; 1895 DELAY(10); 1896 } 1897 if (ntries == 5) { 1898 device_printf(sc->sc_dev, "timeout waiting for master\n"); 1899 error = EIO; 1900 goto fail2; 1901 } 1902 1903 MEM_WRITE_4(sc, 0x3000e0, 0x80000000); 1904 DELAY(5000); 1905 1906 tmp = CSR_READ_4(sc, IWI_CSR_RST); 1907 tmp &= ~IWI_RST_PRINCETON_RESET; 1908 CSR_WRITE_4(sc, IWI_CSR_RST, tmp); 1909 1910 DELAY(5000); 1911 MEM_WRITE_4(sc, 0x3000e0, 0); 1912 DELAY(1000); 1913 MEM_WRITE_4(sc, 0x300004, 1); 1914 DELAY(1000); 1915 MEM_WRITE_4(sc, 0x300004, 0); 1916 DELAY(1000); 1917 MEM_WRITE_1(sc, 0x200000, 0x00); 1918 MEM_WRITE_1(sc, 0x200000, 0x40); 1919 DELAY(1000); 1920 1921 /* write microcode into adapter memory */ 1922 for (w = (uint16_t *)uc; size > 0; w++, size -= 2) 1923 MEM_WRITE_2(sc, 0x200010, htole16(*w)); 1924 1925 MEM_WRITE_1(sc, 0x200000, 0x00); 1926 MEM_WRITE_1(sc, 0x200000, 0x80); 1927 1928 /* wait until we get an answer */ 1929 for (ntries = 0; ntries < 100; ntries++) { 1930 if (MEM_READ_1(sc, 0x200000) & 1) 1931 break; 1932 DELAY(100); 1933 } 1934 if (ntries == 100) { 1935 device_printf(sc->sc_dev, 1936 "timeout waiting for ucode to initialize\n"); 1937 error = EIO; 1938 goto fail2; 1939 } 1940 1941 /* read the answer or the firmware will not initialize properly */ 1942 for (i = 0; i < 7; i++) 1943 MEM_READ_4(sc, 0x200004); 1944 1945 MEM_WRITE_1(sc, 0x200000, 0x00); 1946 1947fail2: free(uc, M_DEVBUF); 1948fail1: 1949 return error; 1950} 1951 1952/* macro to handle unaligned little endian data in firmware image */ 1953#define GETLE32(p) ((p)[0] | (p)[1] << 8 | (p)[2] << 16 | (p)[3] << 24) 1954 1955static int 1956iwi_load_firmware(struct iwi_softc *sc, const char *name) 1957{ 1958 bus_dma_tag_t dmat; 1959 bus_dmamap_t map; 1960 bus_addr_t physaddr; 1961 void *virtaddr; 1962 char *fw; 1963 u_char *p, *end; 1964 uint32_t sentinel, ctl, src, dst, sum, len, mlen, tmp; 1965 size_t size; 1966 int ntries, error; 1967 1968 /* read firmware image from filesystem */ 1969 IWI_UNLOCK(sc); 1970 error = iwi_read_firmware(name, &fw, &size); 1971 IWI_LOCK(sc); 1972 if (error != 0) { 1973 device_printf(sc->sc_dev, "could not read firmware image\n"); 1974 goto fail1; 1975 } 1976 1977 if (size < sizeof (struct iwi_firmware_hdr)) { 1978 error = EINVAL; 1979 goto fail2; 1980 } 1981 1982 fw += sizeof (struct iwi_firmware_hdr); 1983 size -= sizeof (struct iwi_firmware_hdr); 1984 1985 /* allocate DMA memory for mapping firmware image */ 1986 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT, 1987 BUS_SPACE_MAXADDR, NULL, NULL, size, 1, size, 0, NULL, NULL, &dmat); 1988 if (error != 0) { 1989 device_printf(sc->sc_dev, 1990 "could not create firmware DMA tag\n"); 1991 goto fail2; 1992 } 1993 1994 error = bus_dmamem_alloc(dmat, &virtaddr, BUS_DMA_NOWAIT, &map); 1995 if (error != 0) { 1996 device_printf(sc->sc_dev, 1997 "could not allocate firmware DMA memory\n"); 1998 goto fail3; 1999 } 2000 2001 error = bus_dmamap_load(dmat, map, virtaddr, size, iwi_dma_map_addr, 2002 &physaddr, 0); 2003 if (error != 0) { 2004 device_printf(sc->sc_dev, "could not load firmware DMA map\n"); 2005 goto fail4; 2006 } 2007 2008 /* copy firmware image to DMA memory */ 2009 memcpy(virtaddr, fw, size); 2010 2011 /* make sure the adapter will get up-to-date values */ 2012 bus_dmamap_sync(dmat, map, BUS_DMASYNC_PREWRITE); 2013 2014 /* tell the adapter where the command blocks are stored */ 2015 MEM_WRITE_4(sc, 0x3000a0, 0x27000); 2016 2017 /* 2018 * Store command blocks into adapter's internal memory using register 2019 * indirections. The adapter will read the firmware image through DMA 2020 * using information stored in command blocks. 2021 */ 2022 src = physaddr; 2023 p = virtaddr; 2024 end = p + size; 2025 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_ADDR, 0x27000); 2026 2027 while (p < end) { 2028 dst = GETLE32(p); p += 4; src += 4; 2029 len = GETLE32(p); p += 4; src += 4; 2030 p += len; 2031 2032 while (len > 0) { 2033 mlen = min(len, IWI_CB_MAXDATALEN); 2034 2035 ctl = IWI_CB_DEFAULT_CTL | mlen; 2036 sum = ctl ^ src ^ dst; 2037 2038 /* write a command block */ 2039 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, ctl); 2040 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, src); 2041 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, dst); 2042 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, sum); 2043 2044 src += mlen; 2045 dst += mlen; 2046 len -= mlen; 2047 } 2048 } 2049 2050 /* write a fictive final command block (sentinel) */ 2051 sentinel = CSR_READ_4(sc, IWI_CSR_AUTOINC_ADDR); 2052 CSR_WRITE_4(sc, IWI_CSR_AUTOINC_DATA, 0); 2053 2054 tmp = CSR_READ_4(sc, IWI_CSR_RST); 2055 tmp &= ~(IWI_RST_MASTER_DISABLED | IWI_RST_STOP_MASTER); 2056 CSR_WRITE_4(sc, IWI_CSR_RST, tmp); 2057 2058 /* tell the adapter to start processing command blocks */ 2059 MEM_WRITE_4(sc, 0x3000a4, 0x540100); 2060 2061 /* wait until the adapter reaches the sentinel */ 2062 for (ntries = 0; ntries < 400; ntries++) { 2063 if (MEM_READ_4(sc, 0x3000d0) >= sentinel) 2064 break; 2065 DELAY(100); 2066 } 2067 if (ntries == 400) { 2068 device_printf(sc->sc_dev, 2069 "timeout processing command blocks\n"); 2070 error = EIO; 2071 goto fail5; 2072 } 2073 2074 /* we're done with command blocks processing */ 2075 MEM_WRITE_4(sc, 0x3000a4, 0x540c00); 2076 2077 /* allow interrupts so we know when the firmware is ready */ 2078 CSR_WRITE_4(sc, IWI_CSR_INTR_MASK, IWI_INTR_MASK); 2079 2080 /* tell the adapter to initialize the firmware */ 2081 CSR_WRITE_4(sc, IWI_CSR_RST, 0); 2082 2083 tmp = CSR_READ_4(sc, IWI_CSR_CTL); 2084 CSR_WRITE_4(sc, IWI_CSR_CTL, tmp | IWI_CTL_ALLOW_STANDBY); 2085 2086 /* wait at most one second for firmware initialization to complete */ 2087 if ((error = msleep(sc, &sc->sc_mtx, 0, "iwiinit", hz)) != 0) { 2088 device_printf(sc->sc_dev, "timeout waiting for firmware " 2089 "initialization to complete\n"); 2090 } 2091 2092fail5: bus_dmamap_sync(dmat, map, BUS_DMASYNC_POSTWRITE); 2093 bus_dmamap_unload(dmat, map); 2094fail4: bus_dmamem_free(dmat, virtaddr, map); 2095fail3: bus_dma_tag_destroy(dmat); 2096fail2: free(fw, M_DEVBUF); 2097fail1: 2098 return error; 2099} 2100 2101static int 2102iwi_config(struct iwi_softc *sc) 2103{ 2104 struct ieee80211com *ic = &sc->sc_ic; 2105 struct ifnet *ifp = ic->ic_ifp; 2106 struct iwi_configuration config; 2107 struct iwi_rateset rs; 2108 struct iwi_txpower power; 2109 struct ieee80211_key *wk; 2110 struct iwi_wep_key wepkey; 2111 uint32_t data; 2112 int error, i; 2113 2114 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp)); 2115 DPRINTF(("Setting MAC address to %6D\n", ic->ic_myaddr, ":")); 2116 error = iwi_cmd(sc, IWI_CMD_SET_MAC_ADDRESS, ic->ic_myaddr, 2117 IEEE80211_ADDR_LEN, 0); 2118 if (error != 0) 2119 return error; 2120 2121 memset(&config, 0, sizeof config); 2122 config.bluetooth_coexistence = sc->bluetooth; 2123 config.antenna = sc->antenna; 2124 config.multicast_enabled = 1; 2125 config.answer_pbreq = (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0; 2126 config.disable_unicast_decryption = 1; 2127 config.disable_multicast_decryption = 1; 2128 DPRINTF(("Configuring adapter\n")); 2129 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config, 0); 2130 if (error != 0) 2131 return error; 2132 2133 data = htole32(IWI_POWER_MODE_CAM); 2134 DPRINTF(("Setting power mode to %u\n", le32toh(data))); 2135 error = iwi_cmd(sc, IWI_CMD_SET_POWER_MODE, &data, sizeof data, 0); 2136 if (error != 0) 2137 return error; 2138 2139 data = htole32(ic->ic_rtsthreshold); 2140 DPRINTF(("Setting RTS threshold to %u\n", le32toh(data))); 2141 error = iwi_cmd(sc, IWI_CMD_SET_RTS_THRESHOLD, &data, sizeof data, 0); 2142 if (error != 0) 2143 return error; 2144 2145 data = htole32(ic->ic_fragthreshold); 2146 DPRINTF(("Setting fragmentation threshold to %u\n", le32toh(data))); 2147 error = iwi_cmd(sc, IWI_CMD_SET_FRAG_THRESHOLD, &data, sizeof data, 0); 2148 if (error != 0) 2149 return error; 2150 2151 if (ic->ic_opmode == IEEE80211_M_IBSS) { 2152 power.mode = IWI_MODE_11B; 2153 power.nchan = 11; 2154 for (i = 0; i < 11; i++) { 2155 power.chan[i].chan = i + 1; 2156 power.chan[i].power = IWI_TXPOWER_MAX; 2157 } 2158 DPRINTF(("Setting .11b channels tx power\n")); 2159 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 2160 0); 2161 if (error != 0) 2162 return error; 2163 2164 power.mode = IWI_MODE_11G; 2165 DPRINTF(("Setting .11g channels tx power\n")); 2166 error = iwi_cmd(sc, IWI_CMD_SET_TX_POWER, &power, sizeof power, 2167 0); 2168 if (error != 0) 2169 return error; 2170 } 2171 2172 rs.mode = IWI_MODE_11G; 2173 rs.type = IWI_RATESET_TYPE_SUPPORTED; 2174 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11G].rs_nrates; 2175 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11G].rs_rates, 2176 rs.nrates); 2177 DPRINTF(("Setting .11bg supported rates (%u)\n", rs.nrates)); 2178 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0); 2179 if (error != 0) 2180 return error; 2181 2182 rs.mode = IWI_MODE_11A; 2183 rs.type = IWI_RATESET_TYPE_SUPPORTED; 2184 rs.nrates = ic->ic_sup_rates[IEEE80211_MODE_11A].rs_nrates; 2185 memcpy(rs.rates, ic->ic_sup_rates[IEEE80211_MODE_11A].rs_rates, 2186 rs.nrates); 2187 DPRINTF(("Setting .11a supported rates (%u)\n", rs.nrates)); 2188 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 0); 2189 if (error != 0) 2190 return error; 2191 2192 /* if we have a desired ESSID, set it now */ 2193 if (ic->ic_des_esslen != 0) { 2194#ifdef IWI_DEBUG 2195 if (iwi_debug > 0) { 2196 printf("Setting desired ESSID to "); 2197 ieee80211_print_essid(ic->ic_des_essid, 2198 ic->ic_des_esslen); 2199 printf("\n"); 2200 } 2201#endif 2202 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ic->ic_des_essid, 2203 ic->ic_des_esslen, 0); 2204 if (error != 0) 2205 return error; 2206 } 2207 2208 data = htole32(arc4random()); 2209 DPRINTF(("Setting initialization vector to %u\n", le32toh(data))); 2210 error = iwi_cmd(sc, IWI_CMD_SET_IV, &data, sizeof data, 0); 2211 if (error != 0) 2212 return error; 2213 2214 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2215 wk = &ic->ic_crypto.cs_nw_keys[i]; 2216 2217 wepkey.cmd = IWI_WEP_KEY_CMD_SETKEY; 2218 wepkey.idx = i; 2219 wepkey.len = wk->wk_keylen; 2220 memset(wepkey.key, 0, sizeof wepkey.key); 2221 memcpy(wepkey.key, wk->wk_key, wk->wk_keylen); 2222 DPRINTF(("Setting wep key index %u len %u\n", wepkey.idx, 2223 wepkey.len)); 2224 error = iwi_cmd(sc, IWI_CMD_SET_WEP_KEY, &wepkey, 2225 sizeof wepkey, 0); 2226 if (error != 0) 2227 return error; 2228 } 2229 2230 /* enable adapter */ 2231 DPRINTF(("Enabling adapter\n")); 2232 return iwi_cmd(sc, IWI_CMD_ENABLE, NULL, 0, 0); 2233} 2234 2235static int 2236iwi_set_chan(struct iwi_softc *sc, struct ieee80211_channel *chan) 2237{ 2238 struct ieee80211com *ic = &sc->sc_ic; 2239 struct iwi_scan scan; 2240 2241 memset(&scan, 0, sizeof scan); 2242 memset(scan.type, IWI_SCAN_TYPE_PASSIVE, sizeof scan.type); 2243 scan.passive = htole16(2000); 2244 scan.channels[0] = 1 | 2245 (IEEE80211_IS_CHAN_5GHZ(chan) ? IWI_CHAN_5GHZ : IWI_CHAN_2GHZ); 2246 scan.channels[1] = ieee80211_chan2ieee(ic, chan); 2247 2248 DPRINTF(("Setting channel to %u\n", ieee80211_chan2ieee(ic, chan))); 2249 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1); 2250} 2251 2252static int 2253iwi_scan(struct iwi_softc *sc) 2254{ 2255 struct ieee80211com *ic = &sc->sc_ic; 2256 struct iwi_scan scan; 2257 uint8_t *p; 2258 int i, count; 2259 2260 memset(&scan, 0, sizeof scan); 2261 2262 if (ic->ic_des_esslen != 0) { 2263 scan.bdirected = htole16(sc->dwelltime); 2264 memset(scan.type, IWI_SCAN_TYPE_BDIRECTED, sizeof scan.type); 2265 } else { 2266 scan.broadcast = htole16(sc->dwelltime); 2267 memset(scan.type, IWI_SCAN_TYPE_BROADCAST, sizeof scan.type); 2268 } 2269 2270 p = scan.channels; 2271 count = 0; 2272 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 2273 if (IEEE80211_IS_CHAN_5GHZ(&ic->ic_channels[i]) && 2274 isset(ic->ic_chan_active, i)) { 2275 *++p = i; 2276 count++; 2277 } 2278 } 2279 *(p - count) = IWI_CHAN_5GHZ | count; 2280 2281 count = 0; 2282 for (i = 0; i <= IEEE80211_CHAN_MAX; i++) { 2283 if (IEEE80211_IS_CHAN_2GHZ(&ic->ic_channels[i]) && 2284 isset(ic->ic_chan_active, i)) { 2285 *++p = i; 2286 count++; 2287 } 2288 } 2289 *(p - count) = IWI_CHAN_2GHZ | count; 2290 2291 DPRINTF(("Start scanning\n")); 2292 return iwi_cmd(sc, IWI_CMD_SCAN, &scan, sizeof scan, 1); 2293} 2294 2295static int 2296iwi_auth_and_assoc(struct iwi_softc *sc) 2297{ 2298 struct ieee80211com *ic = &sc->sc_ic; 2299 struct ifnet *ifp = ic->ic_ifp; 2300 struct ieee80211_node *ni = ic->ic_bss; 2301 struct ieee80211_wme_info wme; 2302 struct iwi_configuration config; 2303 struct iwi_associate assoc; 2304 struct iwi_rateset rs; 2305 uint16_t capinfo; 2306 uint32_t data; 2307 int error; 2308 2309 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) { 2310 memset(&config, 0, sizeof config); 2311 config.bluetooth_coexistence = sc->bluetooth; 2312 config.antenna = sc->antenna; 2313 config.multicast_enabled = 1; 2314 config.use_protection = 1; 2315 config.answer_pbreq = 2316 (ic->ic_opmode == IEEE80211_M_IBSS) ? 1 : 0; 2317 config.disable_unicast_decryption = 1; 2318 config.disable_multicast_decryption = 1; 2319 DPRINTF(("Configuring adapter\n")); 2320 error = iwi_cmd(sc, IWI_CMD_SET_CONFIG, &config, sizeof config, 2321 1); 2322 if (error != 0) 2323 return error; 2324 } 2325 2326#ifdef IWI_DEBUG 2327 if (iwi_debug > 0) { 2328 printf("Setting ESSID to "); 2329 ieee80211_print_essid(ni->ni_essid, ni->ni_esslen); 2330 printf("\n"); 2331 } 2332#endif 2333 error = iwi_cmd(sc, IWI_CMD_SET_ESSID, ni->ni_essid, ni->ni_esslen, 1); 2334 if (error != 0) 2335 return error; 2336 2337 /* the rate set has already been "negotiated" */ 2338 rs.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A : 2339 IWI_MODE_11G; 2340 rs.type = IWI_RATESET_TYPE_NEGOTIATED; 2341 rs.nrates = ni->ni_rates.rs_nrates; 2342 memcpy(rs.rates, ni->ni_rates.rs_rates, rs.nrates); 2343 DPRINTF(("Setting negociated rates (%u)\n", rs.nrates)); 2344 error = iwi_cmd(sc, IWI_CMD_SET_RATES, &rs, sizeof rs, 1); 2345 if (error != 0) 2346 return error; 2347 2348 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) { 2349 wme.wme_id = IEEE80211_ELEMID_VENDOR; 2350 wme.wme_len = sizeof (struct ieee80211_wme_info) - 2; 2351 wme.wme_oui[0] = 0x00; 2352 wme.wme_oui[1] = 0x50; 2353 wme.wme_oui[2] = 0xf2; 2354 wme.wme_type = WME_OUI_TYPE; 2355 wme.wme_subtype = WME_INFO_OUI_SUBTYPE; 2356 wme.wme_version = WME_VERSION; 2357 wme.wme_info = 0; 2358 2359 DPRINTF(("Setting WME IE (len=%u)\n", wme.wme_len)); 2360 error = iwi_cmd(sc, IWI_CMD_SET_WMEIE, &wme, sizeof wme, 1); 2361 if (error != 0) 2362 return error; 2363 } 2364 2365 if (ic->ic_opt_ie != NULL) { 2366 DPRINTF(("Setting optional IE (len=%u)\n", ic->ic_opt_ie_len)); 2367 error = iwi_cmd(sc, IWI_CMD_SET_OPTIE, ic->ic_opt_ie, 2368 ic->ic_opt_ie_len, 1); 2369 if (error != 0) 2370 return error; 2371 } 2372 2373 data = htole32(ni->ni_rssi); 2374 DPRINTF(("Setting sensitivity to %d\n", (int8_t)ni->ni_rssi)); 2375 error = iwi_cmd(sc, IWI_CMD_SET_SENSITIVITY, &data, sizeof data, 1); 2376 if (error != 0) 2377 return error; 2378 2379 memset(&assoc, 0, sizeof assoc); 2380 assoc.mode = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? IWI_MODE_11A : 2381 IWI_MODE_11G; 2382 assoc.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 2383 if (ni->ni_authmode == IEEE80211_AUTH_SHARED) 2384 assoc.auth = ic->ic_crypto.cs_def_txkey << 4 | IWI_AUTH_SHARED; 2385 if ((ic->ic_flags & IEEE80211_F_WME) && ni->ni_wme_ie != NULL) 2386 assoc.policy |= htole16(IWI_POLICY_WME); 2387 if (ic->ic_opt_ie != NULL) 2388 assoc.policy |= htole16(IWI_POLICY_WPA); 2389 memcpy(assoc.tstamp, ni->ni_tstamp.data, 8); 2390 2391 if (ic->ic_opmode == IEEE80211_M_IBSS) 2392 capinfo = IEEE80211_CAPINFO_IBSS; 2393 else 2394 capinfo = IEEE80211_CAPINFO_ESS; 2395 if (ic->ic_flags & IEEE80211_F_PRIVACY) 2396 capinfo |= IEEE80211_CAPINFO_PRIVACY; 2397 if ((ic->ic_flags & IEEE80211_F_SHPREAMBLE) && 2398 IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) 2399 capinfo |= IEEE80211_CAPINFO_SHORT_PREAMBLE; 2400 if (ic->ic_flags & IEEE80211_F_SHSLOT) 2401 capinfo |= IEEE80211_CAPINFO_SHORT_SLOTTIME; 2402 assoc.capinfo = htole16(capinfo); 2403 2404 assoc.lintval = htole16(ic->ic_lintval); 2405 assoc.intval = htole16(ni->ni_intval); 2406 IEEE80211_ADDR_COPY(assoc.bssid, ni->ni_bssid); 2407 if (ic->ic_opmode == IEEE80211_M_IBSS) 2408 IEEE80211_ADDR_COPY(assoc.dst, ifp->if_broadcastaddr); 2409 else 2410 IEEE80211_ADDR_COPY(assoc.dst, ni->ni_bssid); 2411 2412 DPRINTF(("Trying to associate to %6D channel %u auth %u\n", 2413 assoc.bssid, ":", assoc.chan, assoc.auth)); 2414 return iwi_cmd(sc, IWI_CMD_ASSOCIATE, &assoc, sizeof assoc, 1); 2415} 2416 2417static void 2418iwi_init(void *priv) 2419{ 2420 struct iwi_softc *sc = priv; 2421 struct ieee80211com *ic = &sc->sc_ic; 2422 struct ifnet *ifp = ic->ic_ifp; 2423 struct iwi_rx_data *data; 2424 const char *uc, *fw; 2425 int i; 2426 2427 iwi_stop(sc); 2428 2429 if (iwi_reset(sc) != 0) { 2430 device_printf(sc->sc_dev, "could not reset adapter\n"); 2431 goto fail; 2432 } 2433 2434 switch (ic->ic_opmode) { 2435 case IEEE80211_M_STA: 2436 uc = "iwi-ucode-bss"; 2437 fw = "iwi-bss"; 2438 break; 2439 2440 case IEEE80211_M_IBSS: 2441 uc = "iwi-ucode-ibss"; 2442 fw = "iwi-ibss"; 2443 break; 2444 2445 case IEEE80211_M_MONITOR: 2446 uc = "iwi-ucode-monitor"; 2447 fw = "iwi-monitor"; 2448 break; 2449 2450 default: 2451 uc = fw = NULL; /* should not get there */ 2452 break; 2453 } 2454 2455 if (iwi_load_firmware(sc, "iwi-boot") != 0) { 2456 device_printf(sc->sc_dev, "could not load boot firmware\n"); 2457 goto fail; 2458 } 2459 2460 if (iwi_load_ucode(sc, uc) != 0) { 2461 device_printf(sc->sc_dev, "could not load microcode\n"); 2462 goto fail; 2463 } 2464 2465 iwi_stop_master(sc); 2466 2467 CSR_WRITE_4(sc, IWI_CSR_CMD_BASE, sc->cmdq.physaddr); 2468 CSR_WRITE_4(sc, IWI_CSR_CMD_SIZE, sc->cmdq.count); 2469 CSR_WRITE_4(sc, IWI_CSR_CMD_WIDX, sc->cmdq.cur); 2470 2471 CSR_WRITE_4(sc, IWI_CSR_TX1_BASE, sc->txq[0].physaddr); 2472 CSR_WRITE_4(sc, IWI_CSR_TX1_SIZE, sc->txq[0].count); 2473 CSR_WRITE_4(sc, IWI_CSR_TX1_WIDX, sc->txq[0].cur); 2474 2475 CSR_WRITE_4(sc, IWI_CSR_TX2_BASE, sc->txq[1].physaddr); 2476 CSR_WRITE_4(sc, IWI_CSR_TX2_SIZE, sc->txq[1].count); 2477 CSR_WRITE_4(sc, IWI_CSR_TX2_WIDX, sc->txq[1].cur); 2478 2479 CSR_WRITE_4(sc, IWI_CSR_TX3_BASE, sc->txq[2].physaddr); 2480 CSR_WRITE_4(sc, IWI_CSR_TX3_SIZE, sc->txq[2].count); 2481 CSR_WRITE_4(sc, IWI_CSR_TX3_WIDX, sc->txq[2].cur); 2482 2483 CSR_WRITE_4(sc, IWI_CSR_TX4_BASE, sc->txq[3].physaddr); 2484 CSR_WRITE_4(sc, IWI_CSR_TX4_SIZE, sc->txq[3].count); 2485 CSR_WRITE_4(sc, IWI_CSR_TX4_WIDX, sc->txq[3].cur); 2486 2487 for (i = 0; i < sc->rxq.count; i++) { 2488 data = &sc->rxq.data[i]; 2489 CSR_WRITE_4(sc, data->reg, data->physaddr); 2490 } 2491 2492 CSR_WRITE_4(sc, IWI_CSR_RX_WIDX, sc->rxq.count - 1); 2493 2494 if (iwi_load_firmware(sc, fw) != 0) { 2495 device_printf(sc->sc_dev, "could not load main firmware\n"); 2496 goto fail; 2497 } 2498 2499 sc->flags |= IWI_FLAG_FW_INITED; 2500 2501 if (iwi_config(sc) != 0) { 2502 device_printf(sc->sc_dev, "device configuration failed\n"); 2503 goto fail; 2504 } 2505 2506 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2507 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL) 2508 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 2509 } else 2510 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 2511 2512 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2513 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2514 2515 return; 2516 2517fail: ifp->if_flags &= ~IFF_UP; 2518 iwi_stop(sc); 2519} 2520 2521static void 2522iwi_stop(void *priv) 2523{ 2524 struct iwi_softc *sc = priv; 2525 struct ieee80211com *ic = &sc->sc_ic; 2526 struct ifnet *ifp = ic->ic_ifp; 2527 2528 iwi_stop_master(sc); 2529 2530 CSR_WRITE_4(sc, IWI_CSR_RST, IWI_RST_SOFT_RESET); 2531 2532 /* reset rings */ 2533 iwi_reset_cmd_ring(sc, &sc->cmdq); 2534 iwi_reset_tx_ring(sc, &sc->txq[0]); 2535 iwi_reset_tx_ring(sc, &sc->txq[1]); 2536 iwi_reset_tx_ring(sc, &sc->txq[2]); 2537 iwi_reset_tx_ring(sc, &sc->txq[3]); 2538 iwi_reset_rx_ring(sc, &sc->rxq); 2539 2540 sc->sc_tx_timer = 0; 2541 ifp->if_timer = 0; 2542 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2543 2544 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 2545} 2546 2547/* 2548 * Read firmware image from the filesystem and load it into kernel memory. 2549 * Must be called from a process context. 2550 */ 2551static int 2552iwi_read_firmware(const char *name, caddr_t *bufp, size_t *len) 2553{ 2554 char path[64]; 2555 struct thread *td = curthread; 2556 struct nameidata nd; 2557 struct vattr va; 2558 struct iovec iov; 2559 struct uio uio; 2560 caddr_t buf; 2561 int vfslocked, error; 2562 2563 snprintf(path, sizeof path, "/etc/firmware/%s.fw", name); 2564 2565 NDINIT(&nd, LOOKUP, NOFOLLOW | LOCKLEAF | MPSAFE, UIO_SYSSPACE, path, 2566 td); 2567 if ((error = namei(&nd)) != 0) 2568 goto fail1; 2569 2570 vfslocked = NDHASGIANT(&nd); 2571 2572 if ((error = VOP_GETATTR(nd.ni_vp, &va, td->td_ucred, td)) != 0) 2573 goto fail2; 2574 2575 /* limit firmware size to 256KB */ 2576 if (va.va_size > 256 * 1024) { 2577 error = E2BIG; 2578 goto fail2; 2579 } 2580 2581 if ((buf = malloc(va.va_size, M_DEVBUF, M_NOWAIT)) == NULL) { 2582 error = ENOMEM; 2583 goto fail2; 2584 } 2585 2586 /* read the whole image at once */ 2587 uio.uio_iov = &iov; 2588 uio.uio_iovcnt = 1; 2589 iov.iov_base = buf; 2590 iov.iov_len = va.va_size; 2591 uio.uio_offset = 0; 2592 uio.uio_resid = va.va_size; 2593 uio.uio_segflg = UIO_SYSSPACE; 2594 uio.uio_rw = UIO_READ; 2595 uio.uio_td = td; 2596 2597 if ((error = VOP_READ(nd.ni_vp, &uio, 0, NOCRED)) != 0) 2598 goto fail3; 2599 2600 *bufp = buf; 2601 *len = va.va_size; 2602 2603 vput(nd.ni_vp); 2604 VFS_UNLOCK_GIANT(vfslocked); 2605 2606 return 0; 2607 2608fail3: free(buf, M_DEVBUF); 2609fail2: vput(nd.ni_vp); 2610 VFS_UNLOCK_GIANT(vfslocked); 2611fail1: 2612 return error; 2613} 2614 2615static int 2616iwi_sysctl_stats(SYSCTL_HANDLER_ARGS) 2617{ 2618 struct iwi_softc *sc = arg1; 2619 uint32_t size, buf[128]; 2620 2621 if (!(sc->flags & IWI_FLAG_FW_INITED)) { 2622 memset(buf, 0, sizeof buf); 2623 return SYSCTL_OUT(req, buf, sizeof buf); 2624 } 2625 2626 size = min(CSR_READ_4(sc, IWI_CSR_TABLE0_SIZE), 128 - 1); 2627 CSR_READ_REGION_4(sc, IWI_CSR_TABLE0_BASE, &buf[1], size); 2628 2629 return SYSCTL_OUT(req, buf, sizeof buf); 2630} 2631 2632static int 2633iwi_sysctl_radio(SYSCTL_HANDLER_ARGS) 2634{ 2635 struct iwi_softc *sc = arg1; 2636 int val; 2637 2638 val = (CSR_READ_4(sc, IWI_CSR_IO) & IWI_IO_RADIO_ENABLED) ? 1 : 0; 2639 2640 return SYSCTL_OUT(req, &val, sizeof val); 2641} 2642