if_wpi.c revision 173976
1/*- 2 * Copyright (c) 2006,2007 3 * Damien Bergamini <damien.bergamini@free.fr> 4 * Benjamin Close <Benjamin.Close@clearchain.com> 5 * 6 * Permission to use, copy, modify, and distribute this software for any 7 * purpose with or without fee is hereby granted, provided that the above 8 * copyright notice and this permission notice appear in all copies. 9 * 10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 17 */ 18 19#define VERSION "20071102" 20 21#include <sys/cdefs.h> 22__FBSDID("$FreeBSD: head/sys/dev/wpi/if_wpi.c 173976 2007-11-27 08:58:32Z benjsc $"); 23 24/* 25 * Driver for Intel PRO/Wireless 3945ABG 802.11 network adapters. 26 * 27 * The 3945ABG network adapter doesn't use traditional hardware as 28 * many other adaptors do. Instead at run time the eeprom is set into a known 29 * state and told to load boot firmware. The boot firmware loads an init and a 30 * main binary firmware image into SRAM on the card via DMA. 31 * Once the firmware is loaded, the driver/hw then 32 * communicate by way of circular dma rings via the the SRAM to the firmware. 33 * 34 * There is 6 memory rings. 1 command ring, 1 rx data ring & 4 tx data rings. 35 * The 4 tx data rings allow for prioritization QoS. 36 * 37 * The rx data ring consists of 32 dma buffers. Two registers are used to 38 * indicate where in the ring the driver and the firmware are up to. The 39 * driver sets the initial read index (reg1) and the initial write index (reg2), 40 * the firmware updates the read index (reg1) on rx of a packet and fires an 41 * interrupt. The driver then processes the buffers starting at reg1 indicating 42 * to the firmware which buffers have been accessed by updating reg2. At the 43 * same time allocating new memory for the processed buffer. 44 * 45 * A similar thing happens with the tx rings. The difference is the firmware 46 * stop processing buffers once the queue is full and until confirmation 47 * of a successful transmition (tx_intr) has occurred. 48 * 49 * The command ring operates in the same manner as the tx queues. 50 * 51 * All communication direct to the card (ie eeprom) is classed as Stage1 52 * communication 53 * 54 * All communication via the firmware to the card is classed as State2. 55 * The firmware consists of 2 parts. A bootstrap firmware and a runtime 56 * firmware. The bootstrap firmware and runtime firmware are loaded 57 * from host memory via dma to the card then told to execute. From this point 58 * on the majority of communications between the driver and the card goes 59 * via the firmware. 60 */ 61 62#include <sys/param.h> 63#include <sys/sysctl.h> 64#include <sys/sockio.h> 65#include <sys/mbuf.h> 66#include <sys/kernel.h> 67#include <sys/socket.h> 68#include <sys/systm.h> 69#include <sys/malloc.h> 70#include <sys/queue.h> 71#include <sys/taskqueue.h> 72#include <sys/module.h> 73#include <sys/bus.h> 74#include <sys/endian.h> 75#include <sys/linker.h> 76#include <sys/firmware.h> 77 78#if (__FreeBSD_version > 700000) 79#define WPI_CURRENT 80#endif 81 82#include <machine/bus.h> 83#include <machine/resource.h> 84#ifndef WPI_CURRENT 85#include <machine/clock.h> 86#endif 87#include <sys/rman.h> 88 89#include <dev/pci/pcireg.h> 90#include <dev/pci/pcivar.h> 91 92#include <net/bpf.h> 93#include <net/if.h> 94#include <net/if_arp.h> 95#include <net/ethernet.h> 96#include <net/if_dl.h> 97#include <net/if_media.h> 98#include <net/if_types.h> 99 100#include <net80211/ieee80211_var.h> 101#include <net80211/ieee80211_radiotap.h> 102#include <net80211/ieee80211_regdomain.h> 103 104#include <netinet/in.h> 105#include <netinet/in_systm.h> 106#include <netinet/in_var.h> 107#include <netinet/ip.h> 108#include <netinet/if_ether.h> 109 110#include <dev/wpi/if_wpireg.h> 111#include <dev/wpi/if_wpivar.h> 112 113#define WPI_DEBUG 114 115#ifdef WPI_DEBUG 116#define DPRINTF(x) do { if (wpi_debug != 0) printf x; } while (0) 117#define DPRINTFN(n, x) do { if (wpi_debug & n) printf x; } while (0) 118 119enum { 120 WPI_DEBUG_UNUSED = 0x00000001, /* Unused */ 121 WPI_DEBUG_HW = 0x00000002, /* Stage 1 (eeprom) debugging */ 122 WPI_DEBUG_TX = 0x00000004, /* Stage 2 TX intrp debugging*/ 123 WPI_DEBUG_RX = 0x00000008, /* Stage 2 RX intrp debugging */ 124 WPI_DEBUG_CMD = 0x00000010, /* Stage 2 CMD intrp debugging*/ 125 WPI_DEBUG_FIRMWARE = 0x00000020, /* firmware(9) loading debug */ 126 WPI_DEBUG_DMA = 0x00000040, /* DMA (de)allocations/syncs */ 127 WPI_DEBUG_SCANNING = 0x00000080, /* Stage 2 Scanning debugging */ 128 WPI_DEBUG_NOTIFY = 0x00000100, /* State 2 Noftif intr debug */ 129 WPI_DEBUG_TEMP = 0x00000200, /* TXPower/Temp Calibration */ 130 WPI_DEBUG_OPS = 0x00000400, /* wpi_ops taskq debug */ 131 WPI_DEBUG_WATCHDOG = 0x00000800, /* Watch dog debug */ 132 WPI_DEBUG_ANY = 0xffffffff 133}; 134 135int wpi_debug = 0; 136SYSCTL_INT(_debug, OID_AUTO, wpi, CTLFLAG_RW, &wpi_debug, 0, "wpi debug level"); 137 138#else 139#define DPRINTF(x) 140#define DPRINTFN(n, x) 141#endif 142 143struct wpi_ident { 144 uint16_t vendor; 145 uint16_t device; 146 uint16_t subdevice; 147 const char *name; 148}; 149 150static const struct wpi_ident wpi_ident_table[] = { 151 /* The below entries support ABG regardless of the subid */ 152 { 0x8086, 0x4222, 0x0, "Intel(R) PRO/Wireless 3945ABG" }, 153 { 0x8086, 0x4227, 0x0, "Intel(R) PRO/Wireless 3945ABG" }, 154 /* The below entries only support BG */ 155 { 0x8086, 0x4222, 0x1005, "Intel(R) PRO/Wireless 3945AB" }, 156 { 0x8086, 0x4222, 0x1034, "Intel(R) PRO/Wireless 3945AB" }, 157 { 0x8086, 0x4222, 0x1014, "Intel(R) PRO/Wireless 3945AB" }, 158 { 0x8086, 0x4222, 0x1044, "Intel(R) PRO/Wireless 3945AB" }, 159 { 0, 0, 0, NULL } 160}; 161 162static int wpi_dma_contig_alloc(struct wpi_softc *, struct wpi_dma_info *, 163 void **, bus_size_t, bus_size_t, int); 164static void wpi_dma_contig_free(struct wpi_dma_info *); 165static void wpi_dma_map_addr(void *, bus_dma_segment_t *, int, int); 166static int wpi_alloc_shared(struct wpi_softc *); 167static void wpi_free_shared(struct wpi_softc *); 168static struct wpi_rbuf *wpi_alloc_rbuf(struct wpi_softc *); 169static void wpi_free_rbuf(void *, void *); 170static int wpi_alloc_rpool(struct wpi_softc *); 171static void wpi_free_rpool(struct wpi_softc *); 172static int wpi_alloc_rx_ring(struct wpi_softc *, struct wpi_rx_ring *); 173static void wpi_reset_rx_ring(struct wpi_softc *, struct wpi_rx_ring *); 174static void wpi_free_rx_ring(struct wpi_softc *, struct wpi_rx_ring *); 175static int wpi_alloc_tx_ring(struct wpi_softc *, struct wpi_tx_ring *, 176 int, int); 177static void wpi_reset_tx_ring(struct wpi_softc *, struct wpi_tx_ring *); 178static void wpi_free_tx_ring(struct wpi_softc *, struct wpi_tx_ring *); 179static struct ieee80211_node *wpi_node_alloc(struct ieee80211_node_table *); 180static int wpi_media_change(struct ifnet *); 181static int wpi_newstate(struct ieee80211com *, enum ieee80211_state, int); 182static void wpi_mem_lock(struct wpi_softc *); 183static void wpi_mem_unlock(struct wpi_softc *); 184static uint32_t wpi_mem_read(struct wpi_softc *, uint16_t); 185static void wpi_mem_write(struct wpi_softc *, uint16_t, uint32_t); 186static void wpi_mem_write_region_4(struct wpi_softc *, uint16_t, 187 const uint32_t *, int); 188static uint16_t wpi_read_prom_data(struct wpi_softc *, uint32_t, void *, int); 189static int wpi_alloc_fwmem(struct wpi_softc *); 190static void wpi_free_fwmem(struct wpi_softc *); 191static int wpi_load_firmware(struct wpi_softc *); 192static void wpi_unload_firmware(struct wpi_softc *); 193static int wpi_load_microcode(struct wpi_softc *, const uint8_t *, int); 194static void wpi_rx_intr(struct wpi_softc *, struct wpi_rx_desc *, 195 struct wpi_rx_data *); 196static void wpi_tx_intr(struct wpi_softc *, struct wpi_rx_desc *); 197static void wpi_cmd_intr(struct wpi_softc *, struct wpi_rx_desc *); 198static void wpi_notif_intr(struct wpi_softc *); 199static void wpi_intr(void *); 200static void wpi_ops(void *, int); 201static uint8_t wpi_plcp_signal(int); 202static int wpi_queue_cmd(struct wpi_softc *, int); 203static void wpi_tick(void *); 204#if 0 205static void wpi_radio_on(void *, int); 206static void wpi_radio_off(void *, int); 207#endif 208static int wpi_tx_data(struct wpi_softc *, struct mbuf *, 209 struct ieee80211_node *, int); 210static void wpi_start(struct ifnet *); 211static void wpi_scan_start(struct ieee80211com *); 212static void wpi_scan_end(struct ieee80211com *); 213static void wpi_set_channel(struct ieee80211com *); 214static void wpi_scan_curchan(struct ieee80211com *, unsigned long); 215static void wpi_scan_mindwell(struct ieee80211com *); 216static void wpi_watchdog(struct ifnet *); 217static int wpi_ioctl(struct ifnet *, u_long, caddr_t); 218static void wpi_restart(void *, int); 219static void wpi_read_eeprom(struct wpi_softc *); 220static void wpi_read_eeprom_channels(struct wpi_softc *, int); 221static void wpi_read_eeprom_group(struct wpi_softc *, int); 222static int wpi_cmd(struct wpi_softc *, int, const void *, int, int); 223static int wpi_wme_update(struct ieee80211com *); 224static int wpi_mrr_setup(struct wpi_softc *); 225static void wpi_set_led(struct wpi_softc *, uint8_t, uint8_t, uint8_t); 226static void wpi_enable_tsf(struct wpi_softc *, struct ieee80211_node *); 227#if 0 228static int wpi_setup_beacon(struct wpi_softc *, struct ieee80211_node *); 229#endif 230static int wpi_auth(struct wpi_softc *); 231static int wpi_scan(struct wpi_softc *); 232static int wpi_config(struct wpi_softc *); 233static void wpi_stop_master(struct wpi_softc *); 234static int wpi_power_up(struct wpi_softc *); 235static int wpi_reset(struct wpi_softc *); 236static void wpi_hw_config(struct wpi_softc *); 237static void wpi_init(void *); 238static void wpi_stop(struct wpi_softc *); 239static void wpi_stop_locked(struct wpi_softc *); 240static void wpi_iter_func(void *, struct ieee80211_node *); 241 242static void wpi_newassoc(struct ieee80211_node *, int); 243static int wpi_set_txpower(struct wpi_softc *, struct ieee80211_channel *, 244 int); 245static void wpi_calib_timeout(void *); 246static void wpi_power_calibration(struct wpi_softc *, int); 247static int wpi_get_power_index(struct wpi_softc *, 248 struct wpi_power_group *, struct ieee80211_channel *, int); 249static const char *wpi_cmd_str(int); 250static int wpi_probe(device_t); 251static int wpi_attach(device_t); 252static int wpi_detach(device_t); 253static int wpi_shutdown(device_t); 254static int wpi_suspend(device_t); 255static int wpi_resume(device_t); 256 257 258static device_method_t wpi_methods[] = { 259 /* Device interface */ 260 DEVMETHOD(device_probe, wpi_probe), 261 DEVMETHOD(device_attach, wpi_attach), 262 DEVMETHOD(device_detach, wpi_detach), 263 DEVMETHOD(device_shutdown, wpi_shutdown), 264 DEVMETHOD(device_suspend, wpi_suspend), 265 DEVMETHOD(device_resume, wpi_resume), 266 267 { 0, 0 } 268}; 269 270static driver_t wpi_driver = { 271 "wpi", 272 wpi_methods, 273 sizeof (struct wpi_softc) 274}; 275 276static devclass_t wpi_devclass; 277 278DRIVER_MODULE(wpi, pci, wpi_driver, wpi_devclass, 0, 0); 279 280static const uint8_t wpi_ridx_to_plcp[] = { 281 /* OFDM: IEEE Std 802.11a-1999, pp. 14 Table 80 */ 282 /* R1-R4 (ral/ural is R4-R1) */ 283 0xd, 0xf, 0x5, 0x7, 0x9, 0xb, 0x1, 0x3, 284 /* CCK: device-dependent */ 285 10, 20, 55, 110 286}; 287static const uint8_t wpi_ridx_to_rate[] = { 288 12, 18, 24, 36, 48, 72, 96, 108, /* OFDM */ 289 2, 4, 11, 22 /*CCK */ 290}; 291 292 293static int 294wpi_probe(device_t dev) 295{ 296 const struct wpi_ident *ident; 297 298 for (ident = wpi_ident_table; ident->name != NULL; ident++) { 299 if (pci_get_vendor(dev) == ident->vendor && 300 pci_get_device(dev) == ident->device) { 301 device_set_desc(dev, ident->name); 302 return 0; 303 } 304 } 305 return ENXIO; 306} 307 308/** 309 * Load the firmare image from disk to the allocated dma buffer. 310 * we also maintain the reference to the firmware pointer as there 311 * is times where we may need to reload the firmware but we are not 312 * in a context that can access the filesystem (ie taskq cause by restart) 313 * 314 * @return 0 on success, an errno on failure 315 */ 316static int 317wpi_load_firmware(struct wpi_softc *sc) 318{ 319#ifdef WPI_CURRENT 320 const struct firmware *fp ; 321#else 322 struct firmware *fp; 323#endif 324 struct wpi_dma_info *dma = &sc->fw_dma; 325 const struct wpi_firmware_hdr *hdr; 326 const uint8_t *itext, *idata, *rtext, *rdata, *btext; 327 uint32_t itextsz, idatasz, rtextsz, rdatasz, btextsz; 328 int error; 329 WPI_LOCK_DECL; 330 331 DPRINTFN(WPI_DEBUG_FIRMWARE, 332 ("Attempting Loading Firmware from wpi_fw module\n")); 333 334 WPI_UNLOCK(sc); 335 336 if (sc->fw_fp == NULL && (sc->fw_fp = firmware_get("wpifw")) == NULL) { 337 device_printf(sc->sc_dev, 338 "could not load firmware image 'wpifw'\n"); 339 error = ENOENT; 340 WPI_LOCK(sc); 341 goto fail; 342 } 343 344 fp = sc->fw_fp; 345 346 WPI_LOCK(sc); 347 348 /* Validate the firmware is minimum a particular version */ 349 if (fp->version < WPI_FW_MINVERSION) { 350 device_printf(sc->sc_dev, 351 "firmware version is too old. Need %d, got %d\n", 352 WPI_FW_MINVERSION, 353 fp->version); 354 error = ENXIO; 355 goto fail; 356 } 357 358 if (fp->datasize < sizeof (struct wpi_firmware_hdr)) { 359 device_printf(sc->sc_dev, 360 "firmware file too short: %zu bytes\n", fp->datasize); 361 error = ENXIO; 362 goto fail; 363 } 364 365 hdr = (const struct wpi_firmware_hdr *)fp->data; 366 367 /* | RUNTIME FIRMWARE | INIT FIRMWARE | BOOT FW | 368 |HDR|<--TEXT-->|<--DATA-->|<--TEXT-->|<--DATA-->|<--TEXT-->| */ 369 370 rtextsz = le32toh(hdr->rtextsz); 371 rdatasz = le32toh(hdr->rdatasz); 372 itextsz = le32toh(hdr->itextsz); 373 idatasz = le32toh(hdr->idatasz); 374 btextsz = le32toh(hdr->btextsz); 375 376 /* check that all firmware segments are present */ 377 if (fp->datasize < sizeof (struct wpi_firmware_hdr) + 378 rtextsz + rdatasz + itextsz + idatasz + btextsz) { 379 device_printf(sc->sc_dev, 380 "firmware file too short: %zu bytes\n", fp->datasize); 381 error = ENXIO; /* XXX appropriate error code? */ 382 goto fail; 383 } 384 385 /* get pointers to firmware segments */ 386 rtext = (const uint8_t *)(hdr + 1); 387 rdata = rtext + rtextsz; 388 itext = rdata + rdatasz; 389 idata = itext + itextsz; 390 btext = idata + idatasz; 391 392 DPRINTFN(WPI_DEBUG_FIRMWARE, 393 ("Firmware Version: Major %d, Minor %d, Driver %d, \n" 394 "runtime (text: %u, data: %u) init (text: %u, data %u) boot (text %u)\n", 395 (le32toh(hdr->version) & 0xff000000) >> 24, 396 (le32toh(hdr->version) & 0x00ff0000) >> 16, 397 (le32toh(hdr->version) & 0x0000ffff), 398 rtextsz, rdatasz, 399 itextsz, idatasz, btextsz)); 400 401 DPRINTFN(WPI_DEBUG_FIRMWARE,("rtext 0x%x\n", *(const uint32_t *)rtext)); 402 DPRINTFN(WPI_DEBUG_FIRMWARE,("rdata 0x%x\n", *(const uint32_t *)rdata)); 403 DPRINTFN(WPI_DEBUG_FIRMWARE,("itext 0x%x\n", *(const uint32_t *)itext)); 404 DPRINTFN(WPI_DEBUG_FIRMWARE,("idata 0x%x\n", *(const uint32_t *)idata)); 405 DPRINTFN(WPI_DEBUG_FIRMWARE,("btext 0x%x\n", *(const uint32_t *)btext)); 406 407 /* sanity checks */ 408 if (rtextsz > WPI_FW_MAIN_TEXT_MAXSZ || 409 rdatasz > WPI_FW_MAIN_DATA_MAXSZ || 410 itextsz > WPI_FW_INIT_TEXT_MAXSZ || 411 idatasz > WPI_FW_INIT_DATA_MAXSZ || 412 btextsz > WPI_FW_BOOT_TEXT_MAXSZ || 413 (btextsz & 3) != 0) { 414 device_printf(sc->sc_dev, "firmware invalid\n"); 415 error = EINVAL; 416 goto fail; 417 } 418 419 /* copy initialization images into pre-allocated DMA-safe memory */ 420 memcpy(dma->vaddr, idata, idatasz); 421 memcpy(dma->vaddr + WPI_FW_INIT_DATA_MAXSZ, itext, itextsz); 422 423 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 424 425 /* tell adapter where to find initialization images */ 426 wpi_mem_lock(sc); 427 wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr); 428 wpi_mem_write(sc, WPI_MEM_DATA_SIZE, idatasz); 429 wpi_mem_write(sc, WPI_MEM_TEXT_BASE, 430 dma->paddr + WPI_FW_INIT_DATA_MAXSZ); 431 wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, itextsz); 432 wpi_mem_unlock(sc); 433 434 /* load firmware boot code */ 435 if ((error = wpi_load_microcode(sc, btext, btextsz)) != 0) { 436 device_printf(sc->sc_dev, "Failed to load microcode\n"); 437 goto fail; 438 } 439 440 /* now press "execute" */ 441 WPI_WRITE(sc, WPI_RESET, 0); 442 443 /* wait at most one second for the first alive notification */ 444 if ((error = msleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) { 445 device_printf(sc->sc_dev, 446 "timeout waiting for adapter to initialize\n"); 447 goto fail; 448 } 449 450 /* copy runtime images into pre-allocated DMA-sage memory */ 451 memcpy(dma->vaddr, rdata, rdatasz); 452 memcpy(dma->vaddr + WPI_FW_MAIN_DATA_MAXSZ, rtext, rtextsz); 453 bus_dmamap_sync(dma->tag, dma->map, BUS_DMASYNC_PREWRITE); 454 455 /* tell adapter where to find runtime images */ 456 wpi_mem_lock(sc); 457 wpi_mem_write(sc, WPI_MEM_DATA_BASE, dma->paddr); 458 wpi_mem_write(sc, WPI_MEM_DATA_SIZE, rdatasz); 459 wpi_mem_write(sc, WPI_MEM_TEXT_BASE, 460 dma->paddr + WPI_FW_MAIN_DATA_MAXSZ); 461 wpi_mem_write(sc, WPI_MEM_TEXT_SIZE, WPI_FW_UPDATED | rtextsz); 462 wpi_mem_unlock(sc); 463 464 /* wait at most one second for the first alive notification */ 465 if ((error = msleep(sc, &sc->sc_mtx, PCATCH, "wpiinit", hz)) != 0) { 466 device_printf(sc->sc_dev, 467 "timeout waiting for adapter to initialize2\n"); 468 goto fail; 469 } 470 471 DPRINTFN(WPI_DEBUG_FIRMWARE, 472 ("Firmware loaded to driver successfully\n")); 473 return error; 474fail: 475 wpi_unload_firmware(sc); 476 return error; 477} 478 479/** 480 * Free the referenced firmware image 481 */ 482static void 483wpi_unload_firmware(struct wpi_softc *sc) 484{ 485 WPI_LOCK_DECL; 486 487 if (sc->fw_fp) { 488 WPI_UNLOCK(sc); 489 firmware_put(sc->fw_fp, FIRMWARE_UNLOAD); 490 WPI_LOCK(sc); 491 sc->fw_fp = NULL; 492 } 493} 494 495static int 496wpi_attach(device_t dev) 497{ 498 struct wpi_softc *sc = device_get_softc(dev); 499 struct ifnet *ifp; 500 struct ieee80211com *ic = &sc->sc_ic; 501 int ac, error, supportsa = 1; 502 uint32_t tmp; 503 const struct wpi_ident *ident; 504 505 sc->sc_dev = dev; 506 507 if (bootverbose || wpi_debug) 508 device_printf(sc->sc_dev,"Driver Revision %s\n", VERSION); 509 510 /* 511 * Some card's only support 802.11b/g not a, check to see if 512 * this is one such card. A 0x0 in the subdevice table indicates 513 * the entire subdevice range is to be ignored. 514 */ 515 for (ident = wpi_ident_table; ident->name != NULL; ident++) { 516 if (ident->subdevice && 517 pci_get_subdevice(dev) == ident->subdevice) { 518 supportsa = 0; 519 break; 520 } 521 } 522 523#if __FreeBSD_version >= 700000 524 /* 525 * Create the taskqueues used by the driver. Primarily 526 * sc_tq handles most the task 527 */ 528 sc->sc_tq = taskqueue_create("wpi_taskq", M_NOWAIT | M_ZERO, 529 taskqueue_thread_enqueue, &sc->sc_tq); 530 taskqueue_start_threads(&sc->sc_tq, 1, PI_NET, "%s taskq", 531 device_get_nameunit(dev)); 532 533 sc->sc_tq2 = taskqueue_create("wpi_taskq2", M_NOWAIT | M_ZERO, 534 taskqueue_thread_enqueue, &sc->sc_tq2); 535 taskqueue_start_threads(&sc->sc_tq2, 1, PI_NET, "%s taskq2", 536 device_get_nameunit(dev)); 537#else 538#error "Sorry, this driver is not yet ready for FreeBSD < 7.0" 539#endif 540 541 /* Create the tasks that can be queued */ 542#if 0 543 TASK_INIT(&sc->sc_radioontask, 0, wpi_radio_on, sc); 544 TASK_INIT(&sc->sc_radioofftask, 0, wpi_radio_off, sc); 545#endif 546 TASK_INIT(&sc->sc_opstask, 0, wpi_ops, sc); 547 TASK_INIT(&sc->sc_restarttask, 0, wpi_restart, sc); 548 549 WPI_LOCK_INIT(sc); 550 WPI_CMD_LOCK_INIT(sc); 551 552 callout_init_mtx(&sc->calib_to, &sc->sc_mtx, 0); 553 callout_init_mtx(&sc->watchdog_to, &sc->sc_mtx, 0); 554 555 if (pci_get_powerstate(dev) != PCI_POWERSTATE_D0) { 556 device_printf(dev, "chip is in D%d power mode " 557 "-- setting to D0\n", pci_get_powerstate(dev)); 558 pci_set_powerstate(dev, PCI_POWERSTATE_D0); 559 } 560 561 /* disable the retry timeout register */ 562 pci_write_config(dev, 0x41, 0, 1); 563 564 /* enable bus-mastering */ 565 pci_enable_busmaster(dev); 566 567 sc->mem_rid = PCIR_BAR(0); 568 sc->mem = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &sc->mem_rid, 569 RF_ACTIVE); 570 if (sc->mem == NULL) { 571 device_printf(dev, "could not allocate memory resource\n"); 572 error = ENOMEM; 573 goto fail; 574 } 575 576 sc->sc_st = rman_get_bustag(sc->mem); 577 sc->sc_sh = rman_get_bushandle(sc->mem); 578 579 sc->irq_rid = 0; 580 sc->irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irq_rid, 581 RF_ACTIVE | RF_SHAREABLE); 582 if (sc->irq == NULL) { 583 device_printf(dev, "could not allocate interrupt resource\n"); 584 error = ENOMEM; 585 goto fail; 586 } 587 588 /* 589 * Allocate DMA memory for firmware transfers. 590 */ 591 if ((error = wpi_alloc_fwmem(sc)) != 0) { 592 printf(": could not allocate firmware memory\n"); 593 error = ENOMEM; 594 goto fail; 595 } 596 597 /* 598 * Put adapter into a known state. 599 */ 600 if ((error = wpi_reset(sc)) != 0) { 601 device_printf(dev, "could not reset adapter\n"); 602 goto fail; 603 } 604 605 wpi_mem_lock(sc); 606 tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV); 607 if (bootverbose || wpi_debug) 608 device_printf(sc->sc_dev, "Hardware Revision (0x%X)\n", tmp); 609 610 wpi_mem_unlock(sc); 611 612 /* Allocate shared page */ 613 if ((error = wpi_alloc_shared(sc)) != 0) { 614 device_printf(dev, "could not allocate shared page\n"); 615 goto fail; 616 } 617 618 /* 619 * Allocate the receive buffer pool. The recieve buffers are 620 * WPI_RBUF_SIZE in length (3k) this is bigger than MCLBYTES 621 * hence we can't simply use a cluster and used mapped dma memory 622 * instead. 623 */ 624 if ((error = wpi_alloc_rpool(sc)) != 0) { 625 device_printf(dev, "could not allocate Rx buffers\n"); 626 goto fail; 627 } 628 629 /* tx data queues - 4 for QoS purposes */ 630 for (ac = 0; ac < WME_NUM_AC; ac++) { 631 error = wpi_alloc_tx_ring(sc, &sc->txq[ac], WPI_TX_RING_COUNT, ac); 632 if (error != 0) { 633 device_printf(dev, "could not allocate Tx ring %d\n",ac); 634 goto fail; 635 } 636 } 637 638 /* command queue to talk to the card's firmware */ 639 error = wpi_alloc_tx_ring(sc, &sc->cmdq, WPI_CMD_RING_COUNT, 4); 640 if (error != 0) { 641 device_printf(dev, "could not allocate command ring\n"); 642 goto fail; 643 } 644 645 /* receive data queue */ 646 error = wpi_alloc_rx_ring(sc, &sc->rxq); 647 if (error != 0) { 648 device_printf(dev, "could not allocate Rx ring\n"); 649 goto fail; 650 } 651 652 ifp = sc->sc_ifp = if_alloc(IFT_ETHER); 653 if (ifp == NULL) { 654 device_printf(dev, "can not if_alloc()\n"); 655 error = ENOMEM; 656 goto fail; 657 } 658 659 ic->ic_ifp = ifp; 660 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 661 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 662 ic->ic_state = IEEE80211_S_INIT; 663 664 /* set device capabilities */ 665 ic->ic_caps = 666 IEEE80211_C_MONITOR /* monitor mode supported */ 667 | IEEE80211_C_TXPMGT /* tx power management */ 668 | IEEE80211_C_SHSLOT /* short slot time supported */ 669 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 670 | IEEE80211_C_WPA /* 802.11i */ 671/* XXX looks like WME is partly supported? */ 672#if 0 673 | IEEE80211_C_IBSS /* IBSS mode support */ 674 | IEEE80211_C_BGSCAN /* capable of bg scanning */ 675 | IEEE80211_C_WME /* 802.11e */ 676 | IEEE80211_C_HOSTAP /* Host access point mode */ 677#endif 678 ; 679 680 /* 681 * Read in the eeprom and also setup the channels for 682 * net80211. We don't set the rates as net80211 does this for us 683 */ 684 wpi_read_eeprom(sc); 685 686 if (bootverbose || wpi_debug) { 687 device_printf(sc->sc_dev, "Regulatory Domain: %.4s\n", sc->domain); 688 device_printf(sc->sc_dev, "Hardware Type: %c\n", 689 sc->type > 1 ? 'B': '?'); 690 device_printf(sc->sc_dev, "Hardware Revision: %c\n", 691 ((le16toh(sc->rev) & 0xf0) == 0xd0) ? 'D': '?'); 692 device_printf(sc->sc_dev, "SKU %s support 802.11a\n", 693 supportsa ? "does" : "does not"); 694 695 /* XXX hw_config uses the PCIDEV for the Hardware rev. Must check 696 what sc->rev really represents - benjsc 20070615 */ 697 } 698 699 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 700 ifp->if_softc = sc; 701 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 702 ifp->if_init = wpi_init; 703 ifp->if_ioctl = wpi_ioctl; 704 ifp->if_start = wpi_start; 705 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 706 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 707 IFQ_SET_READY(&ifp->if_snd); 708 ieee80211_ifattach(ic); 709 710 /* override default methods */ 711 ic->ic_node_alloc = wpi_node_alloc; 712 ic->ic_newassoc = wpi_newassoc; 713 ic->ic_wme.wme_update = wpi_wme_update; 714 ic->ic_scan_start = wpi_scan_start; 715 ic->ic_scan_end = wpi_scan_end; 716 ic->ic_set_channel = wpi_set_channel; 717 ic->ic_scan_curchan = wpi_scan_curchan; 718 ic->ic_scan_mindwell = wpi_scan_mindwell; 719 720 /* override state transition machine */ 721 sc->sc_newstate = ic->ic_newstate; 722 ic->ic_newstate = wpi_newstate; 723 ieee80211_media_init(ic, wpi_media_change, ieee80211_media_status); 724 725 ieee80211_amrr_init(&sc->amrr, ic, 726 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD, 727 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD); 728 729 /* whilst ieee80211_ifattach will listen for ieee80211 frames, 730 * we also want to listen for the lower level radio frames 731 */ 732 bpfattach2(ifp, DLT_IEEE802_11_RADIO, 733 sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap), 734 &sc->sc_drvbpf); 735 736 sc->sc_rxtap_len = sizeof sc->sc_rxtap; 737 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 738 sc->sc_rxtap.wr_ihdr.it_present = htole32(WPI_RX_RADIOTAP_PRESENT); 739 740 sc->sc_txtap_len = sizeof sc->sc_txtap; 741 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 742 sc->sc_txtap.wt_ihdr.it_present = htole32(WPI_TX_RADIOTAP_PRESENT); 743 744 /* 745 * Hook our interrupt after all initialization is complete. 746 */ 747 error = bus_setup_intr(dev, sc->irq, INTR_TYPE_NET |INTR_MPSAFE , 748#ifdef WPI_CURRENT 749 NULL, 750#endif 751 wpi_intr, sc, &sc->sc_ih); 752 if (error != 0) { 753 device_printf(dev, "could not set up interrupt\n"); 754 goto fail; 755 } 756 757 ieee80211_announce(ic); 758#ifdef XXX_DEBUG 759 ieee80211_announce_channels(ic); 760#endif 761 762 return 0; 763 764fail: wpi_detach(dev); 765 return ENXIO; 766} 767 768static int 769wpi_detach(device_t dev) 770{ 771 struct wpi_softc *sc = device_get_softc(dev); 772 struct ieee80211com *ic = &sc->sc_ic; 773 struct ifnet *ifp = ic->ic_ifp; 774 int ac; 775 WPI_LOCK_DECL; 776 777 if (ifp != NULL) { 778 wpi_stop(sc); 779 callout_drain(&sc->watchdog_to); 780 callout_drain(&sc->calib_to); 781 bpfdetach(ifp); 782 ieee80211_ifdetach(ic); 783 } 784 785 WPI_LOCK(sc); 786 if (sc->txq[0].data_dmat) { 787 for (ac = 0; ac < WME_NUM_AC; ac++) 788 wpi_free_tx_ring(sc, &sc->txq[ac]); 789 790 wpi_free_tx_ring(sc, &sc->cmdq); 791 wpi_free_rx_ring(sc, &sc->rxq); 792 wpi_free_rpool(sc); 793 wpi_free_shared(sc); 794 } 795 796 if (sc->fw_fp != NULL) { 797 wpi_unload_firmware(sc); 798 } 799 800 if (sc->fw_dma.tag) 801 wpi_free_fwmem(sc); 802 WPI_UNLOCK(sc); 803 804 if (sc->irq != NULL) { 805 bus_teardown_intr(dev, sc->irq, sc->sc_ih); 806 bus_release_resource(dev, SYS_RES_IRQ, sc->irq_rid, sc->irq); 807 } 808 809 if (sc->mem != NULL) 810 bus_release_resource(dev, SYS_RES_MEMORY, sc->mem_rid, sc->mem); 811 812 if (ifp != NULL) 813 if_free(ifp); 814 815 taskqueue_free(sc->sc_tq); 816 taskqueue_free(sc->sc_tq2); 817 818 WPI_LOCK_DESTROY(sc); 819 WPI_CMD_LOCK_DESTROY(sc); 820 821 return 0; 822} 823 824static void 825wpi_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nsegs, int error) 826{ 827 if (error != 0) 828 return; 829 830 KASSERT(nsegs == 1, ("too many DMA segments, %d should be 1", nsegs)); 831 832 *(bus_addr_t *)arg = segs[0].ds_addr; 833} 834 835static int 836wpi_dma_contig_alloc(struct wpi_softc *sc, struct wpi_dma_info *dma, 837 void **kvap, bus_size_t size, bus_size_t alignment, int flags) 838{ 839 int error; 840 int count = 0; 841 842 DPRINTFN(WPI_DEBUG_DMA, 843 ("Size: %zd - alignement %zd\n", size, alignment)); 844 845 dma->size = size; 846 dma->tag = NULL; 847 848again: 849 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), alignment, 850 0, BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, 851 NULL, NULL, size, 852 1, size, flags, 853 NULL, NULL, &dma->tag); 854 if (error != 0) { 855 device_printf(sc->sc_dev, 856 "could not create shared page DMA tag\n"); 857 goto fail; 858 } 859 error = bus_dmamem_alloc(dma->tag, (void **)&dma->vaddr, 860 flags | BUS_DMA_ZERO, &dma->map); 861 if (error != 0) { 862 device_printf(sc->sc_dev, 863 "could not allocate shared page DMA memory\n"); 864 goto fail; 865 } 866 867 /** 868 * Sadly FreeBSD can't always align on a 16k boundary, hence we give it 869 * 10 attempts increasing the size of the allocation by 4k each time. 870 * This should eventually align us on a 16k boundary at the cost 871 * of chewing up dma memory 872 */ 873 if ((((uintptr_t)dma->vaddr) & (alignment-1)) && count < 10) { 874 DPRINTFN(WPI_DEBUG_DMA, 875 ("Memory Unaligned, trying again: %d\n", count++)); 876 wpi_dma_contig_free(dma); 877 size += 4096; 878 goto again; 879 } 880 881 DPRINTFN(WPI_DEBUG_DMA,("Memory, allocated & %s Aligned!\n", 882 count == 10 ? "FAILED" : "")); 883 if (count == 10) { 884 device_printf(sc->sc_dev, "Unable to align memory\n"); 885 error = ENOMEM; 886 goto fail; 887 } 888 889 error = bus_dmamap_load(dma->tag, dma->map, dma->vaddr, 890 size, wpi_dma_map_addr, &dma->paddr, flags); 891 892 if (error != 0) { 893 device_printf(sc->sc_dev, 894 "could not load shared page DMA map\n"); 895 goto fail; 896 } 897 898 if (kvap != NULL) 899 *kvap = dma->vaddr; 900 901 return 0; 902 903fail: 904 wpi_dma_contig_free(dma); 905 return error; 906} 907 908static void 909wpi_dma_contig_free(struct wpi_dma_info *dma) 910{ 911 if (dma->tag) { 912 if (dma->map != NULL) { 913 if (dma->paddr == 0) { 914 bus_dmamap_sync(dma->tag, dma->map, 915 BUS_DMASYNC_POSTREAD | BUS_DMASYNC_POSTWRITE); 916 bus_dmamap_unload(dma->tag, dma->map); 917 } 918 bus_dmamem_free(dma->tag, &dma->vaddr, dma->map); 919 } 920 bus_dma_tag_destroy(dma->tag); 921 } 922} 923 924/* 925 * Allocate a shared page between host and NIC. 926 */ 927static int 928wpi_alloc_shared(struct wpi_softc *sc) 929{ 930 int error; 931 932 error = wpi_dma_contig_alloc(sc, &sc->shared_dma, 933 (void **)&sc->shared, sizeof (struct wpi_shared), 934 PAGE_SIZE, 935 BUS_DMA_NOWAIT); 936 937 if (error != 0) { 938 device_printf(sc->sc_dev, 939 "could not allocate shared area DMA memory\n"); 940 } 941 942 return error; 943} 944 945static void 946wpi_free_shared(struct wpi_softc *sc) 947{ 948 wpi_dma_contig_free(&sc->shared_dma); 949} 950 951struct wpi_rbuf * 952wpi_alloc_rbuf(struct wpi_softc *sc) 953{ 954 struct wpi_rbuf *rbuf; 955 956 rbuf = SLIST_FIRST(&sc->rxq.freelist); 957 if (rbuf == NULL) 958 return NULL; 959 SLIST_REMOVE_HEAD(&sc->rxq.freelist, next); 960 return rbuf; 961} 962 963/* 964 * This is called automatically by the network stack when the mbuf to which our 965 * Rx buffer is attached is freed. 966 */ 967static void 968wpi_free_rbuf(void *buf, void *arg) 969{ 970 struct wpi_rbuf *rbuf = arg; 971 struct wpi_softc *sc = rbuf->sc; 972 WPI_LOCK_DECL; 973 974 WPI_LOCK(sc); 975 976 /* put the buffer back in the free list */ 977 SLIST_INSERT_HEAD(&sc->rxq.freelist, rbuf, next); 978 979 WPI_UNLOCK(sc); 980} 981 982static int 983wpi_alloc_rpool(struct wpi_softc *sc) 984{ 985 struct wpi_rx_ring *ring = &sc->rxq; 986 struct wpi_rbuf *rbuf; 987 int i, error; 988 989 /* allocate a big chunk of DMA'able memory.. */ 990 error = wpi_dma_contig_alloc(sc, &ring->buf_dma, NULL, 991 WPI_RBUF_COUNT * WPI_RBUF_SIZE, PAGE_SIZE, BUS_DMA_NOWAIT); 992 if (error != 0) { 993 device_printf(sc->sc_dev, 994 "could not allocate Rx buffers DMA memory\n"); 995 return error; 996 } 997 998 /* ..and split it into 3KB chunks */ 999 SLIST_INIT(&ring->freelist); 1000 for (i = 0; i < WPI_RBUF_COUNT; i++) { 1001 rbuf = &ring->rbuf[i]; 1002 1003 rbuf->sc = sc; /* backpointer for callbacks */ 1004 rbuf->vaddr = ring->buf_dma.vaddr + i * WPI_RBUF_SIZE; 1005 rbuf->paddr = ring->buf_dma.paddr + i * WPI_RBUF_SIZE; 1006 1007 SLIST_INSERT_HEAD(&ring->freelist, rbuf, next); 1008 } 1009 return 0; 1010} 1011 1012static void 1013wpi_free_rpool(struct wpi_softc *sc) 1014{ 1015 wpi_dma_contig_free(&sc->rxq.buf_dma); 1016} 1017 1018static int 1019wpi_alloc_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring) 1020{ 1021 1022 struct wpi_rx_data *data; 1023 struct wpi_rbuf *rbuf; 1024 int i, error; 1025 1026 ring->cur = 0; 1027 1028 error = wpi_dma_contig_alloc(sc, &ring->desc_dma, 1029 (void **)&ring->desc, WPI_RX_RING_COUNT * sizeof (uint32_t), 1030 WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT); 1031 1032 if (error != 0) { 1033 device_printf(sc->sc_dev, 1034 "could not allocate rx ring DMA memory\n"); 1035 goto fail; 1036 } 1037 1038 /* 1039 * Allocate Rx buffers. 1040 */ 1041 for (i = 0; i < WPI_RX_RING_COUNT; i++) { 1042 data = &ring->data[i]; 1043 1044 data->m = m_get(M_DONTWAIT, MT_HEADER); 1045 if (data->m == NULL) { 1046 device_printf(sc->sc_dev, 1047 "could not allocate rx mbuf\n"); 1048 error = ENOBUFS; 1049 goto fail; 1050 } 1051 1052 if ((rbuf = wpi_alloc_rbuf(sc)) == NULL) { 1053 m_freem(data->m); 1054 data->m = NULL; 1055 device_printf(sc->sc_dev, 1056 "could not allocate rx buffer\n"); 1057 error = ENOBUFS; 1058 goto fail; 1059 } 1060 1061 /* attach RxBuffer to mbuf */ 1062 MEXTADD(data->m, rbuf->vaddr, WPI_RBUF_SIZE,wpi_free_rbuf, 1063 rbuf,0,EXT_NET_DRV); 1064 1065 if ((data->m->m_flags & M_EXT) == 0) { 1066 m_freem(data->m); 1067 data->m = NULL; 1068 error = ENOBUFS; 1069 goto fail; 1070 } 1071 ring->desc[i] = htole32(rbuf->paddr); 1072 } 1073 1074 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 1075 BUS_DMASYNC_PREWRITE); 1076 1077 return 0; 1078 1079fail: 1080 wpi_free_rx_ring(sc, ring); 1081 return error; 1082} 1083 1084static void 1085wpi_reset_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring) 1086{ 1087 int ntries; 1088 1089 wpi_mem_lock(sc); 1090 1091 WPI_WRITE(sc, WPI_RX_CONFIG, 0); 1092 1093 for (ntries = 0; ntries < 100; ntries++) { 1094 if (WPI_READ(sc, WPI_RX_STATUS) & WPI_RX_IDLE) 1095 break; 1096 DELAY(10); 1097 } 1098 1099 wpi_mem_unlock(sc); 1100 1101#ifdef WPI_DEBUG 1102 if (ntries == 100 && wpi_debug > 0) 1103 device_printf(sc->sc_dev, "timeout resetting Rx ring\n"); 1104#endif 1105 1106 ring->cur = 0; 1107} 1108 1109static void 1110wpi_free_rx_ring(struct wpi_softc *sc, struct wpi_rx_ring *ring) 1111{ 1112 int i; 1113 1114 wpi_dma_contig_free(&ring->desc_dma); 1115 1116 for (i = 0; i < WPI_RX_RING_COUNT; i++) { 1117 if (ring->data[i].m != NULL) { 1118 m_freem(ring->data[i].m); 1119 ring->data[i].m = NULL; 1120 } 1121 } 1122} 1123 1124static int 1125wpi_alloc_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring, int count, 1126 int qid) 1127{ 1128 struct wpi_tx_data *data; 1129 int i, error; 1130 1131 ring->qid = qid; 1132 ring->count = count; 1133 ring->queued = 0; 1134 ring->cur = 0; 1135 ring->data = NULL; 1136 1137 error = wpi_dma_contig_alloc(sc, &ring->desc_dma, 1138 (void **)&ring->desc, count * sizeof (struct wpi_tx_desc), 1139 WPI_RING_DMA_ALIGN, BUS_DMA_NOWAIT); 1140 1141 if (error != 0) { 1142 device_printf(sc->sc_dev, "could not allocate tx dma memory\n"); 1143 goto fail; 1144 } 1145 1146 /* update shared page with ring's base address */ 1147 sc->shared->txbase[qid] = htole32(ring->desc_dma.paddr); 1148 1149 error = wpi_dma_contig_alloc(sc, &ring->cmd_dma, (void **)&ring->cmd, 1150 count * sizeof (struct wpi_tx_cmd), WPI_RING_DMA_ALIGN, 1151 BUS_DMA_NOWAIT); 1152 1153 if (error != 0) { 1154 device_printf(sc->sc_dev, 1155 "could not allocate tx command DMA memory\n"); 1156 goto fail; 1157 } 1158 1159 ring->data = malloc(count * sizeof (struct wpi_tx_data), M_DEVBUF, 1160 M_NOWAIT | M_ZERO); 1161 if (ring->data == NULL) { 1162 device_printf(sc->sc_dev, 1163 "could not allocate tx data slots\n"); 1164 goto fail; 1165 } 1166 1167 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 1168 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1169 WPI_MAX_SCATTER - 1, MCLBYTES, BUS_DMA_NOWAIT, NULL, NULL, 1170 &ring->data_dmat); 1171 if (error != 0) { 1172 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 1173 goto fail; 1174 } 1175 1176 for (i = 0; i < count; i++) { 1177 data = &ring->data[i]; 1178 1179 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 1180 if (error != 0) { 1181 device_printf(sc->sc_dev, 1182 "could not create tx buf DMA map\n"); 1183 goto fail; 1184 } 1185 bus_dmamap_sync(ring->data_dmat, data->map, 1186 BUS_DMASYNC_PREWRITE); 1187 } 1188 1189 return 0; 1190 1191fail: wpi_free_tx_ring(sc, ring); 1192 return error; 1193} 1194 1195static void 1196wpi_reset_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring) 1197{ 1198 struct wpi_tx_data *data; 1199 int i, ntries; 1200 1201 wpi_mem_lock(sc); 1202 1203 WPI_WRITE(sc, WPI_TX_CONFIG(ring->qid), 0); 1204 for (ntries = 0; ntries < 100; ntries++) { 1205 if (WPI_READ(sc, WPI_TX_STATUS) & WPI_TX_IDLE(ring->qid)) 1206 break; 1207 DELAY(10); 1208 } 1209#ifdef WPI_DEBUG 1210 if (ntries == 100 && wpi_debug > 0) { 1211 device_printf(sc->sc_dev, "timeout resetting Tx ring %d\n", 1212 ring->qid); 1213 } 1214#endif 1215 wpi_mem_unlock(sc); 1216 1217 for (i = 0; i < ring->count; i++) { 1218 data = &ring->data[i]; 1219 1220 if (data->m != NULL) { 1221 bus_dmamap_unload(ring->data_dmat, data->map); 1222 m_freem(data->m); 1223 data->m = NULL; 1224 } 1225 } 1226 1227 ring->queued = 0; 1228 ring->cur = 0; 1229} 1230 1231static void 1232wpi_free_tx_ring(struct wpi_softc *sc, struct wpi_tx_ring *ring) 1233{ 1234 struct wpi_tx_data *data; 1235 int i; 1236 1237 wpi_dma_contig_free(&ring->desc_dma); 1238 wpi_dma_contig_free(&ring->cmd_dma); 1239 1240 if (ring->data != NULL) { 1241 for (i = 0; i < ring->count; i++) { 1242 data = &ring->data[i]; 1243 1244 if (data->m != NULL) { 1245 bus_dmamap_sync(ring->data_dmat, data->map, 1246 BUS_DMASYNC_POSTWRITE); 1247 bus_dmamap_unload(ring->data_dmat, data->map); 1248 m_freem(data->m); 1249 data->m = NULL; 1250 } 1251 } 1252 free(ring->data, M_DEVBUF); 1253 } 1254 1255 if (ring->data_dmat != NULL) 1256 bus_dma_tag_destroy(ring->data_dmat); 1257} 1258 1259static int 1260wpi_shutdown(device_t dev) 1261{ 1262 struct wpi_softc *sc = device_get_softc(dev); 1263 WPI_LOCK_DECL; 1264 1265 WPI_LOCK(sc); 1266 wpi_stop_locked(sc); 1267 wpi_unload_firmware(sc); 1268 WPI_UNLOCK(sc); 1269 1270 return 0; 1271} 1272 1273static int 1274wpi_suspend(device_t dev) 1275{ 1276 struct wpi_softc *sc = device_get_softc(dev); 1277 1278 wpi_stop(sc); 1279 return 0; 1280} 1281 1282static int 1283wpi_resume(device_t dev) 1284{ 1285 struct wpi_softc *sc = device_get_softc(dev); 1286 struct ifnet *ifp = sc->sc_ic.ic_ifp; 1287 1288 pci_write_config(dev, 0x41, 0, 1); 1289 1290 if (ifp->if_flags & IFF_UP) { 1291 wpi_init(ifp->if_softc); 1292 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 1293 wpi_start(ifp); 1294 } 1295 return 0; 1296} 1297 1298/* ARGSUSED */ 1299static struct ieee80211_node * 1300wpi_node_alloc(struct ieee80211_node_table *ic) 1301{ 1302 struct wpi_node *wn; 1303 1304 wn = malloc(sizeof (struct wpi_node), M_80211_NODE, M_NOWAIT |M_ZERO); 1305 1306 return &wn->ni; 1307} 1308 1309static int 1310wpi_media_change(struct ifnet *ifp) 1311{ 1312 int error; 1313 1314 error = ieee80211_media_change(ifp); 1315 if (error != ENETRESET) 1316 return error; 1317 1318 if ((ifp->if_flags & IFF_UP) && (ifp->if_drv_flags & IFF_DRV_RUNNING)) 1319 wpi_init(ifp->if_softc); 1320 1321 return 0; 1322} 1323 1324/** 1325 * Called by net80211 when ever there is a change to 80211 state machine 1326 */ 1327static int 1328wpi_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg) 1329{ 1330 struct ifnet *ifp = ic->ic_ifp; 1331 struct wpi_softc *sc = ifp->if_softc; 1332 struct ieee80211_node *ni; 1333 int error; 1334 1335 callout_stop(&sc->calib_to); 1336 1337 switch (nstate) { 1338 case IEEE80211_S_SCAN: 1339 DPRINTF(("NEWSTATE:SCAN\n")); 1340 /* Scanning is handled in net80211 via the scan_start, 1341 * scan_end, scan_curchan functions. Hence all we do when 1342 * changing to the SCAN state is update the leds 1343 */ 1344 1345 /* make the link LED blink while we're scanning */ 1346 wpi_set_led(sc, WPI_LED_LINK, 20, 2); 1347 break; 1348 1349 case IEEE80211_S_ASSOC: 1350 DPRINTF(("NEWSTATE:ASSOC\n")); 1351 if (ic->ic_state != IEEE80211_S_RUN) 1352 break; 1353 /* FALLTHROUGH */ 1354 1355 case IEEE80211_S_AUTH: 1356 DPRINTF(("NEWSTATE:AUTH\n")); 1357 sc->flags |= WPI_FLAG_AUTH; 1358 sc->config.associd = 0; 1359 sc->config.filter &= ~htole32(WPI_FILTER_BSS); 1360 wpi_queue_cmd(sc,WPI_AUTH); 1361 DPRINTF(("END AUTH\n")); 1362 break; 1363 1364 case IEEE80211_S_RUN: 1365 DPRINTF(("NEWSTATE:RUN\n")); 1366 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 1367 /* link LED blinks while monitoring */ 1368 wpi_set_led(sc, WPI_LED_LINK, 5, 5); 1369 break; 1370 } 1371 1372#if 0 1373 if (ic->ic_opmode != IEEE80211_M_STA) { 1374 (void) wpi_auth(sc); /* XXX */ 1375 wpi_setup_beacon(sc, ic->ic_bss); 1376 } 1377#endif 1378 1379 ni = ic->ic_bss; 1380 wpi_enable_tsf(sc, ni); 1381 1382 /* update adapter's configuration */ 1383 sc->config.associd = htole16(ni->ni_associd & ~0xc000); 1384 /* short preamble/slot time are negotiated when associating */ 1385 sc->config.flags &= ~htole32(WPI_CONFIG_SHPREAMBLE | 1386 WPI_CONFIG_SHSLOT); 1387 if (ic->ic_flags & IEEE80211_F_SHSLOT) 1388 sc->config.flags |= htole32(WPI_CONFIG_SHSLOT); 1389 if (ic->ic_flags & IEEE80211_F_SHPREAMBLE) 1390 sc->config.flags |= htole32(WPI_CONFIG_SHPREAMBLE); 1391 sc->config.filter |= htole32(WPI_FILTER_BSS); 1392#if 0 1393 if (ic->ic_opmode != IEEE80211_M_STA) 1394 sc->config.filter |= htole32(WPI_FILTER_BEACON); 1395#endif 1396 1397/* XXX put somewhere HC_QOS_SUPPORT_ASSOC + HC_IBSS_START */ 1398 1399 DPRINTF(("config chan %d flags %x\n", sc->config.chan, 1400 sc->config.flags)); 1401 error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config, 1402 sizeof (struct wpi_config), 1); 1403 if (error != 0) { 1404 device_printf(sc->sc_dev, 1405 "could not update configuration\n"); 1406 return error; 1407 } 1408 1409 if ((error = wpi_set_txpower(sc, ic->ic_bss->ni_chan, 1)) != 0) { 1410 device_printf(sc->sc_dev, 1411 "could set txpower\n"); 1412 return error; 1413 } 1414 1415 if (ic->ic_opmode == IEEE80211_M_STA) { 1416 /* fake a join to init the tx rate */ 1417 wpi_newassoc(ic->ic_bss, 1); 1418 } 1419 1420 /* start automatic rate control timer */ 1421 callout_reset(&sc->calib_to, hz/2, wpi_calib_timeout, sc); 1422 1423 /* link LED always on while associated */ 1424 wpi_set_led(sc, WPI_LED_LINK, 0, 1); 1425 break; 1426 1427 case IEEE80211_S_INIT: 1428 DPRINTF(("NEWSTATE:INIT\n")); 1429 break; 1430 1431 default: 1432 break; 1433 } 1434 1435 return (*sc->sc_newstate)(ic, nstate, arg); 1436} 1437 1438/* 1439 * Grab exclusive access to NIC memory. 1440 */ 1441static void 1442wpi_mem_lock(struct wpi_softc *sc) 1443{ 1444 int ntries; 1445 uint32_t tmp; 1446 1447 tmp = WPI_READ(sc, WPI_GPIO_CTL); 1448 WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_MAC); 1449 1450 /* spin until we actually get the lock */ 1451 for (ntries = 0; ntries < 100; ntries++) { 1452 if ((WPI_READ(sc, WPI_GPIO_CTL) & 1453 (WPI_GPIO_CLOCK | WPI_GPIO_SLEEP)) == WPI_GPIO_CLOCK) 1454 break; 1455 DELAY(10); 1456 } 1457 if (ntries == 100) 1458 device_printf(sc->sc_dev, "could not lock memory\n"); 1459} 1460 1461/* 1462 * Release lock on NIC memory. 1463 */ 1464static void 1465wpi_mem_unlock(struct wpi_softc *sc) 1466{ 1467 uint32_t tmp = WPI_READ(sc, WPI_GPIO_CTL); 1468 WPI_WRITE(sc, WPI_GPIO_CTL, tmp & ~WPI_GPIO_MAC); 1469} 1470 1471static uint32_t 1472wpi_mem_read(struct wpi_softc *sc, uint16_t addr) 1473{ 1474 WPI_WRITE(sc, WPI_READ_MEM_ADDR, WPI_MEM_4 | addr); 1475 return WPI_READ(sc, WPI_READ_MEM_DATA); 1476} 1477 1478static void 1479wpi_mem_write(struct wpi_softc *sc, uint16_t addr, uint32_t data) 1480{ 1481 WPI_WRITE(sc, WPI_WRITE_MEM_ADDR, WPI_MEM_4 | addr); 1482 WPI_WRITE(sc, WPI_WRITE_MEM_DATA, data); 1483} 1484 1485static void 1486wpi_mem_write_region_4(struct wpi_softc *sc, uint16_t addr, 1487 const uint32_t *data, int wlen) 1488{ 1489 for (; wlen > 0; wlen--, data++, addr+=4) 1490 wpi_mem_write(sc, addr, *data); 1491} 1492 1493/* 1494 * Read data from the EEPROM. We access EEPROM through the MAC instead of 1495 * using the traditional bit-bang method. Data is read up until len bytes have 1496 * been obtained. 1497 */ 1498static uint16_t 1499wpi_read_prom_data(struct wpi_softc *sc, uint32_t addr, void *data, int len) 1500{ 1501 int ntries; 1502 uint32_t val; 1503 uint8_t *out = data; 1504 1505 wpi_mem_lock(sc); 1506 1507 for (; len > 0; len -= 2, addr++) { 1508 WPI_WRITE(sc, WPI_EEPROM_CTL, addr << 2); 1509 1510 for (ntries = 0; ntries < 10; ntries++) { 1511 if ((val = WPI_READ(sc, WPI_EEPROM_CTL)) & WPI_EEPROM_READY) 1512 break; 1513 DELAY(5); 1514 } 1515 1516 if (ntries == 10) { 1517 device_printf(sc->sc_dev, "could not read EEPROM\n"); 1518 return ETIMEDOUT; 1519 } 1520 1521 *out++= val >> 16; 1522 if (len > 1) 1523 *out ++= val >> 24; 1524 } 1525 1526 wpi_mem_unlock(sc); 1527 1528 return 0; 1529} 1530 1531/* 1532 * The firmware text and data segments are transferred to the NIC using DMA. 1533 * The driver just copies the firmware into DMA-safe memory and tells the NIC 1534 * where to find it. Once the NIC has copied the firmware into its internal 1535 * memory, we can free our local copy in the driver. 1536 */ 1537static int 1538wpi_load_microcode(struct wpi_softc *sc, const uint8_t *fw, int size) 1539{ 1540 int error, ntries; 1541 1542 DPRINTFN(WPI_DEBUG_HW,("Loading microcode size 0x%x\n", size)); 1543 1544 size /= sizeof(uint32_t); 1545 1546 wpi_mem_lock(sc); 1547 1548 wpi_mem_write_region_4(sc, WPI_MEM_UCODE_BASE, 1549 (const uint32_t *)fw, size); 1550 1551 wpi_mem_write(sc, WPI_MEM_UCODE_SRC, 0); 1552 wpi_mem_write(sc, WPI_MEM_UCODE_DST, WPI_FW_TEXT); 1553 wpi_mem_write(sc, WPI_MEM_UCODE_SIZE, size); 1554 1555 /* run microcode */ 1556 wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_RUN); 1557 1558 /* wait while the adapter is busy copying the firmware */ 1559 for (error = 0, ntries = 0; ntries < 1000; ntries++) { 1560 uint32_t status = WPI_READ(sc, WPI_TX_STATUS); 1561 DPRINTFN(WPI_DEBUG_HW, 1562 ("firmware status=0x%x, val=0x%x, result=0x%x\n", status, 1563 WPI_TX_IDLE(6), status & WPI_TX_IDLE(6))); 1564 if (status & WPI_TX_IDLE(6)) { 1565 DPRINTFN(WPI_DEBUG_HW, 1566 ("Status Match! - ntries = %d\n", ntries)); 1567 break; 1568 } 1569 DELAY(10); 1570 } 1571 if (ntries == 1000) { 1572 device_printf(sc->sc_dev, "timeout transferring firmware\n"); 1573 error = ETIMEDOUT; 1574 } 1575 1576 /* start the microcode executing */ 1577 wpi_mem_write(sc, WPI_MEM_UCODE_CTL, WPI_UC_ENABLE); 1578 1579 wpi_mem_unlock(sc); 1580 1581 return (error); 1582} 1583 1584static void 1585wpi_rx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc, 1586 struct wpi_rx_data *data) 1587{ 1588 struct ieee80211com *ic = &sc->sc_ic; 1589 struct ifnet *ifp = ic->ic_ifp; 1590 struct wpi_rx_ring *ring = &sc->rxq; 1591 struct wpi_rx_stat *stat; 1592 struct wpi_rx_head *head; 1593 struct wpi_rx_tail *tail; 1594 struct wpi_rbuf *rbuf; 1595 struct ieee80211_frame *wh; 1596 struct ieee80211_node *ni; 1597 struct mbuf *m, *mnew; 1598 WPI_LOCK_DECL; 1599 1600 stat = (struct wpi_rx_stat *)(desc + 1); 1601 1602 if (stat->len > WPI_STAT_MAXLEN) { 1603 device_printf(sc->sc_dev, "invalid rx statistic header\n"); 1604 ifp->if_ierrors++; 1605 return; 1606 } 1607 1608 head = (struct wpi_rx_head *)((caddr_t)(stat + 1) + stat->len); 1609 tail = (struct wpi_rx_tail *)((caddr_t)(head + 1) + le16toh(head->len)); 1610 1611 DPRINTFN(WPI_DEBUG_RX, ("rx intr: idx=%d len=%d stat len=%d rssi=%d " 1612 "rate=%x chan=%d tstamp=%ju\n", ring->cur, le32toh(desc->len), 1613 le16toh(head->len), (int8_t)stat->rssi, head->rate, head->chan, 1614 (uintmax_t)le64toh(tail->tstamp))); 1615 1616 m = data->m; 1617 1618 /* finalize mbuf */ 1619 m->m_pkthdr.rcvif = ifp; 1620 m->m_data = (caddr_t)(head + 1); 1621 m->m_pkthdr.len = m->m_len = le16toh(head->len); 1622 1623 if ((rbuf = SLIST_FIRST(&sc->rxq.freelist)) != NULL) { 1624 mnew = m_gethdr(M_DONTWAIT,MT_DATA); 1625 if (mnew == NULL) { 1626 ifp->if_ierrors++; 1627 return; 1628 } 1629 1630 /* attach Rx buffer to mbuf */ 1631 MEXTADD(mnew,rbuf->vaddr,WPI_RBUF_SIZE, wpi_free_rbuf, rbuf, 0, 1632 EXT_NET_DRV); 1633 SLIST_REMOVE_HEAD(&sc->rxq.freelist, next); 1634 data->m = mnew; 1635 1636 /* update Rx descriptor */ 1637 ring->desc[ring->cur] = htole32(rbuf->paddr); 1638 } else { 1639 /* no free rbufs, copy frame */ 1640 m = m_dup(m, M_DONTWAIT); 1641 if (m == NULL) { 1642 /* no free mbufs either, drop frame */ 1643 ifp->if_ierrors++; 1644 return; 1645 } 1646 } 1647 1648#ifndef WPI_CURRENT 1649 if (sc->sc_drvbpf != NULL) { 1650#else 1651 if (bpf_peers_present(sc->sc_drvbpf)) { 1652#endif 1653 struct wpi_rx_radiotap_header *tap = &sc->sc_rxtap; 1654 1655 tap->wr_flags = 0; 1656 tap->wr_chan_freq = 1657 htole16(ic->ic_channels[head->chan].ic_freq); 1658 tap->wr_chan_flags = 1659 htole16(ic->ic_channels[head->chan].ic_flags); 1660 tap->wr_dbm_antsignal = (int8_t)(stat->rssi - WPI_RSSI_OFFSET); 1661 tap->wr_dbm_antnoise = (int8_t)le16toh(stat->noise); 1662 tap->wr_tsft = tail->tstamp; 1663 tap->wr_antenna = (le16toh(head->flags) >> 4) & 0xf; 1664 switch (head->rate) { 1665 /* CCK rates */ 1666 case 10: tap->wr_rate = 2; break; 1667 case 20: tap->wr_rate = 4; break; 1668 case 55: tap->wr_rate = 11; break; 1669 case 110: tap->wr_rate = 22; break; 1670 /* OFDM rates */ 1671 case 0xd: tap->wr_rate = 12; break; 1672 case 0xf: tap->wr_rate = 18; break; 1673 case 0x5: tap->wr_rate = 24; break; 1674 case 0x7: tap->wr_rate = 36; break; 1675 case 0x9: tap->wr_rate = 48; break; 1676 case 0xb: tap->wr_rate = 72; break; 1677 case 0x1: tap->wr_rate = 96; break; 1678 case 0x3: tap->wr_rate = 108; break; 1679 /* unknown rate: should not happen */ 1680 default: tap->wr_rate = 0; 1681 } 1682 if (le16toh(head->flags) & 0x4) 1683 tap->wr_flags |= IEEE80211_RADIOTAP_F_SHORTPRE; 1684 1685 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_rxtap_len, m); 1686 } 1687 1688 wh = mtod(m, struct ieee80211_frame *); 1689 WPI_UNLOCK(sc); 1690 1691 /* XXX frame length > sizeof(struct ieee80211_frame_min)? */ 1692 /* grab a reference to the source node */ 1693 ni = ieee80211_find_rxnode(ic, (struct ieee80211_frame_min *)wh); 1694 1695 /* send the frame to the 802.11 layer */ 1696 ieee80211_input(ic, m, ni, stat->rssi, 0, 0); 1697 1698 /* release node reference */ 1699 ieee80211_free_node(ni); 1700 WPI_LOCK(sc); 1701} 1702 1703static void 1704wpi_tx_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc) 1705{ 1706 struct ifnet *ifp = sc->sc_ic.ic_ifp; 1707 struct wpi_tx_ring *ring = &sc->txq[desc->qid & 0x3]; 1708 struct wpi_tx_data *txdata = &ring->data[desc->idx]; 1709 struct wpi_tx_stat *stat = (struct wpi_tx_stat *)(desc + 1); 1710 struct wpi_node *wn = (struct wpi_node *)txdata->ni; 1711 1712 DPRINTFN(WPI_DEBUG_TX, ("tx done: qid=%d idx=%d retries=%d nkill=%d " 1713 "rate=%x duration=%d status=%x\n", desc->qid, desc->idx, 1714 stat->ntries, stat->nkill, stat->rate, le32toh(stat->duration), 1715 le32toh(stat->status))); 1716 1717 /* 1718 * Update rate control statistics for the node. 1719 * XXX we should not count mgmt frames since they're always sent at 1720 * the lowest available bit-rate. 1721 * XXX frames w/o ACK shouldn't be used either 1722 */ 1723 wn->amn.amn_txcnt++; 1724 if (stat->ntries > 0) { 1725 DPRINTFN(3, ("%d retries\n", stat->ntries)); 1726 wn->amn.amn_retrycnt++; 1727 } 1728 1729 /* XXX oerrors should only count errors !maxtries */ 1730 if ((le32toh(stat->status) & 0xff) != 1) 1731 ifp->if_oerrors++; 1732 else 1733 ifp->if_opackets++; 1734 1735 bus_dmamap_sync(ring->data_dmat, txdata->map, BUS_DMASYNC_POSTWRITE); 1736 bus_dmamap_unload(ring->data_dmat, txdata->map); 1737 /* XXX handle M_TXCB? */ 1738 m_freem(txdata->m); 1739 txdata->m = NULL; 1740 ieee80211_free_node(txdata->ni); 1741 txdata->ni = NULL; 1742 1743 ring->queued--; 1744 1745 sc->sc_tx_timer = 0; 1746 sc->watchdog_cnt = 0; 1747 callout_stop(&sc->watchdog_to); 1748 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1749 wpi_start(ifp); 1750} 1751 1752static void 1753wpi_cmd_intr(struct wpi_softc *sc, struct wpi_rx_desc *desc) 1754{ 1755 struct wpi_tx_ring *ring = &sc->cmdq; 1756 struct wpi_tx_data *data; 1757 1758 DPRINTFN(WPI_DEBUG_CMD, ("cmd notification qid=%x idx=%d flags=%x " 1759 "type=%s len=%d\n", desc->qid, desc->idx, 1760 desc->flags, wpi_cmd_str(desc->type), 1761 le32toh(desc->len))); 1762 1763 if ((desc->qid & 7) != 4) 1764 return; /* not a command ack */ 1765 1766 data = &ring->data[desc->idx]; 1767 1768 /* if the command was mapped in a mbuf, free it */ 1769 if (data->m != NULL) { 1770 bus_dmamap_unload(ring->data_dmat, data->map); 1771 m_freem(data->m); 1772 data->m = NULL; 1773 } 1774 1775 sc->flags &= ~WPI_FLAG_BUSY; 1776 wakeup(&ring->cmd[desc->idx]); 1777} 1778 1779static void 1780wpi_notif_intr(struct wpi_softc *sc) 1781{ 1782 struct ieee80211com *ic = &sc->sc_ic; 1783 struct wpi_rx_desc *desc; 1784 struct wpi_rx_data *data; 1785 uint32_t hw; 1786 1787 hw = le32toh(sc->shared->next); 1788 while (sc->rxq.cur != hw) { 1789 data = &sc->rxq.data[sc->rxq.cur]; 1790 desc = (void *)data->m->m_ext.ext_buf; 1791 1792 DPRINTFN(WPI_DEBUG_NOTIFY, 1793 ("notify qid=%x idx=%d flags=%x type=%d len=%d\n", 1794 desc->qid, 1795 desc->idx, 1796 desc->flags, 1797 desc->type, 1798 le32toh(desc->len))); 1799 1800 if (!(desc->qid & 0x80)) /* reply to a command */ 1801 wpi_cmd_intr(sc, desc); 1802 1803 switch (desc->type) { 1804 case WPI_RX_DONE: 1805 /* a 802.11 frame was received */ 1806 wpi_rx_intr(sc, desc, data); 1807 break; 1808 1809 case WPI_TX_DONE: 1810 /* a 802.11 frame has been transmitted */ 1811 wpi_tx_intr(sc, desc); 1812 break; 1813 1814 case WPI_UC_READY: 1815 { 1816 struct wpi_ucode_info *uc = 1817 (struct wpi_ucode_info *)(desc + 1); 1818 1819 /* the microcontroller is ready */ 1820 DPRINTF(("microcode alive notification version %x " 1821 "alive %x\n", le32toh(uc->version), 1822 le32toh(uc->valid))); 1823 1824 if (le32toh(uc->valid) != 1) { 1825 device_printf(sc->sc_dev, 1826 "microcontroller initialization failed\n"); 1827 wpi_stop_locked(sc); 1828 } 1829 break; 1830 } 1831 case WPI_STATE_CHANGED: 1832 { 1833 uint32_t *status = (uint32_t *)(desc + 1); 1834 1835 /* enabled/disabled notification */ 1836 DPRINTF(("state changed to %x\n", le32toh(*status))); 1837 1838 if (le32toh(*status) & 1) { 1839 device_printf(sc->sc_dev, 1840 "Radio transmitter is switched off\n"); 1841 sc->flags |= WPI_FLAG_HW_RADIO_OFF; 1842 break; 1843 } 1844 sc->flags &= ~WPI_FLAG_HW_RADIO_OFF; 1845 break; 1846 } 1847 case WPI_START_SCAN: 1848 { 1849 struct wpi_start_scan *scan = 1850 (struct wpi_start_scan *)(desc + 1); 1851 1852 DPRINTFN(WPI_DEBUG_SCANNING, 1853 ("scanning channel %d status %x\n", 1854 scan->chan, le32toh(scan->status))); 1855 1856 /* fix current channel */ 1857 ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan]; 1858 break; 1859 } 1860 case WPI_STOP_SCAN: 1861 { 1862 struct wpi_stop_scan *scan = 1863 (struct wpi_stop_scan *)(desc + 1); 1864 1865 DPRINTFN(WPI_DEBUG_SCANNING, 1866 ("scan finished nchan=%d status=%d chan=%d\n", 1867 scan->nchan, scan->status, scan->chan)); 1868 1869 wpi_queue_cmd(sc, WPI_SCAN_NEXT); 1870 break; 1871 } 1872 case WPI_MISSED_BEACON: 1873 { 1874 struct wpi_missed_beacon *beacon = 1875 (struct wpi_missed_beacon *)(desc + 1); 1876 1877 if (le32toh(beacon->consecutive) >= ic->ic_bmissthreshold) { 1878 DPRINTF(("Beacon miss: %u >= %u\n", 1879 le32toh(beacon->consecutive), 1880 ic->ic_bmissthreshold)); 1881 ieee80211_beacon_miss(ic); 1882 } 1883 } 1884 } 1885 1886 sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT; 1887 } 1888 1889 /* tell the firmware what we have processed */ 1890 hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1; 1891 WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7); 1892 1893} 1894 1895static void 1896wpi_intr(void *arg) 1897{ 1898 struct wpi_softc *sc = arg; 1899 uint32_t r; 1900 WPI_LOCK_DECL; 1901 1902 WPI_LOCK(sc); 1903 1904 r = WPI_READ(sc, WPI_INTR); 1905 if (r == 0 || r == 0xffffffff) { 1906 WPI_UNLOCK(sc); 1907 return; 1908 } 1909 1910 /* disable interrupts */ 1911 WPI_WRITE(sc, WPI_MASK, 0); 1912 /* ack interrupts */ 1913 WPI_WRITE(sc, WPI_INTR, r); 1914 1915 if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) { 1916 device_printf(sc->sc_dev, "fatal firmware error\n"); 1917 DPRINTFN(6,("(%s)\n", (r & WPI_SW_ERROR) ? "(Software Error)" : 1918 "(Hardware Error)")); 1919 taskqueue_enqueue(sc->sc_tq2, &sc->sc_restarttask); 1920 sc->flags &= ~WPI_FLAG_BUSY; 1921 WPI_UNLOCK(sc); 1922 return; 1923 } 1924 1925 if (r & WPI_RX_INTR) 1926 wpi_notif_intr(sc); 1927 1928 if (r & WPI_ALIVE_INTR) /* firmware initialized */ 1929 wakeup(sc); 1930 1931 /* re-enable interrupts */ 1932 if (sc->sc_ifp->if_flags & IFF_UP) 1933 WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK); 1934 1935 WPI_UNLOCK(sc); 1936} 1937 1938static uint8_t 1939wpi_plcp_signal(int rate) 1940{ 1941 switch (rate) { 1942 /* CCK rates (returned values are device-dependent) */ 1943 case 2: return 10; 1944 case 4: return 20; 1945 case 11: return 55; 1946 case 22: return 110; 1947 1948 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1949 /* R1-R4 (ral/ural is R4-R1) */ 1950 case 12: return 0xd; 1951 case 18: return 0xf; 1952 case 24: return 0x5; 1953 case 36: return 0x7; 1954 case 48: return 0x9; 1955 case 72: return 0xb; 1956 case 96: return 0x1; 1957 case 108: return 0x3; 1958 1959 /* unsupported rates (should not get there) */ 1960 default: return 0; 1961 } 1962} 1963 1964/* quickly determine if a given rate is CCK or OFDM */ 1965#define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22) 1966 1967/* 1968 * Construct the data packet for a transmit buffer and acutally put 1969 * the buffer onto the transmit ring, kicking the card to process the 1970 * the buffer. 1971 */ 1972static int 1973wpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1974 int ac) 1975{ 1976 struct ieee80211com *ic = &sc->sc_ic; 1977 struct wpi_tx_ring *ring = &sc->txq[ac]; 1978 struct wpi_tx_desc *desc; 1979 struct wpi_tx_data *data; 1980 struct wpi_tx_cmd *cmd; 1981 struct wpi_cmd_data *tx; 1982 struct ieee80211_frame *wh; 1983 struct ieee80211_key *k; 1984 const struct chanAccParams *cap; 1985 struct mbuf *mnew; 1986 int i, error, nsegs, rate, hdrlen, noack = 0; 1987 bus_dma_segment_t segs[WPI_MAX_SCATTER]; 1988 1989 desc = &ring->desc[ring->cur]; 1990 data = &ring->data[ring->cur]; 1991 1992 wh = mtod(m0, struct ieee80211_frame *); 1993 1994 if (IEEE80211_QOS_HAS_SEQ(wh)) { 1995 hdrlen = sizeof (struct ieee80211_qosframe); 1996 cap = &ic->ic_wme.wme_chanParams; 1997 noack = cap->cap_wmeParams[ac].wmep_noackPolicy; 1998 } else 1999 hdrlen = sizeof (struct ieee80211_frame); 2000 2001 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 2002 if ((k = ieee80211_crypto_encap(ic, ni, m0)) == NULL) { 2003 m_freem(m0); 2004 return ENOBUFS; 2005 } 2006 2007 /* packet header may have moved, reset our local pointer */ 2008 wh = mtod(m0, struct ieee80211_frame *); 2009 } 2010 2011 /* pickup a rate */ 2012 if (IEEE80211_IS_MULTICAST(wh->i_addr1)|| 2013 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 2014 IEEE80211_FC0_TYPE_MGT)) { 2015 /* 2016 * mgmt/multicast frames are sent at the lowest available 2017 * bit-rate 2018 */ 2019 rate = ni->ni_rates.rs_rates[0]; 2020 } else { 2021 if (ic->ic_fixed_rate != -1) { 2022 rate = ic->ic_sup_rates[ic->ic_curmode]. 2023 rs_rates[ic->ic_fixed_rate]; 2024 } else 2025 rate = ni->ni_rates.rs_rates[ni->ni_txrate]; 2026 } 2027 rate &= IEEE80211_RATE_VAL; 2028 2029#ifndef WPI_CURRENT 2030 if (sc->sc_drvbpf != NULL) { 2031#else 2032 if (bpf_peers_present(sc->sc_drvbpf)) { 2033#endif 2034 2035 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap; 2036 2037 tap->wt_flags = 0; 2038 tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq); 2039 tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags); 2040 tap->wt_rate = rate; 2041 tap->wt_hwqueue = ac; 2042 if (wh->i_fc[1] & IEEE80211_FC1_WEP) 2043 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP; 2044 2045 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 2046 } 2047 2048 cmd = &ring->cmd[ring->cur]; 2049 cmd->code = WPI_CMD_TX_DATA; 2050 cmd->flags = 0; 2051 cmd->qid = ring->qid; 2052 cmd->idx = ring->cur; 2053 2054 tx = (struct wpi_cmd_data *)cmd->data; 2055 tx->flags = 0; 2056 2057 if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) { 2058 tx->flags |= htole32(WPI_TX_NEED_ACK); 2059 } else if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold) { 2060 tx->flags |= htole32(WPI_TX_NEED_RTS | WPI_TX_FULL_TXOP); 2061 } 2062 2063 tx->flags |= htole32(WPI_TX_AUTO_SEQ); 2064 2065 tx->id = IEEE80211_IS_MULTICAST(wh->i_addr1) ? WPI_ID_BROADCAST : 2066 WPI_ID_BSS; 2067 2068 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 2069 IEEE80211_FC0_TYPE_MGT) { 2070 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 2071 /* tell h/w to set timestamp in probe responses */ 2072 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 2073 tx->flags |= htole32(WPI_TX_INSERT_TSTAMP); 2074 2075 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ || 2076 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) 2077 tx->timeout = htole16(3); 2078 else 2079 tx->timeout = htole16(2); 2080 } else 2081 tx->timeout = htole16(0); 2082 2083 tx->rate = wpi_plcp_signal(rate); 2084 2085 /* be very persistant at sending frames out */ 2086 tx->rts_ntries = 7; 2087 tx->data_ntries = 15; 2088 2089 tx->ofdm_mask = 0xff; 2090 tx->cck_mask = 0x0f; 2091 tx->lifetime = htole32(WPI_LIFETIME_INFINITE); 2092 2093 tx->len = htole16(m0->m_pkthdr.len); 2094 2095 /* save and trim IEEE802.11 header */ 2096 m_copydata(m0, 0, hdrlen, (caddr_t)&tx->wh); 2097 m_adj(m0, hdrlen); 2098 2099 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m0, segs, 2100 &nsegs, BUS_DMA_NOWAIT); 2101 if (error != 0 && error != EFBIG) { 2102 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 2103 error); 2104 m_freem(m0); 2105 return error; 2106 } 2107 if (error != 0) { 2108 /* XXX use ath_defrag */ 2109 mnew = m_defrag(m0, M_DONTWAIT); 2110 if (mnew == NULL) { 2111 device_printf(sc->sc_dev, 2112 "could not defragment mbuf\n"); 2113 m_freem(m0); 2114 return ENOBUFS; 2115 } 2116 m0 = mnew; 2117 2118 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, 2119 m0, segs, &nsegs, BUS_DMA_NOWAIT); 2120 if (error != 0) { 2121 device_printf(sc->sc_dev, 2122 "could not map mbuf (error %d)\n", error); 2123 m_freem(m0); 2124 return error; 2125 } 2126 } 2127 2128 data->m = m0; 2129 data->ni = ni; 2130 2131 DPRINTFN(WPI_DEBUG_TX, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n", 2132 ring->qid, ring->cur, m0->m_pkthdr.len, nsegs)); 2133 2134 /* first scatter/gather segment is used by the tx data command */ 2135 desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2136 (1 + nsegs) << 24); 2137 desc->segs[0].addr = htole32(ring->cmd_dma.paddr + 2138 ring->cur * sizeof (struct wpi_tx_cmd)); 2139 desc->segs[0].len = htole32(4 + sizeof (struct wpi_cmd_data)); 2140 for (i = 1; i <= nsegs; i++) { 2141 desc->segs[i].addr = htole32(segs[i - 1].ds_addr); 2142 desc->segs[i].len = htole32(segs[i - 1].ds_len); 2143 } 2144 2145 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 2146 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 2147 BUS_DMASYNC_PREWRITE); 2148 2149 ring->queued++; 2150 2151 /* kick ring */ 2152 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT; 2153 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2154 2155 return 0; 2156} 2157 2158/** 2159 * Process data waiting to be sent on the IFNET output queue 2160 */ 2161static void 2162wpi_start(struct ifnet *ifp) 2163{ 2164 struct wpi_softc *sc = ifp->if_softc; 2165 struct ieee80211com *ic = &sc->sc_ic; 2166 struct ieee80211_node *ni; 2167 struct ether_header *eh; 2168 struct mbuf *m0; 2169 int ac; 2170 WPI_LOCK_DECL; 2171 2172 WPI_LOCK(sc); 2173 2174 for (;;) { 2175 IF_POLL(&ic->ic_mgtq, m0); 2176 if (m0 != NULL) { 2177 IF_DEQUEUE(&ic->ic_mgtq, m0); 2178 2179 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif; 2180 m0->m_pkthdr.rcvif = NULL; 2181 2182 /* management frames go into ring 0 */ 2183 if (sc->txq[0].queued > sc->txq[0].count - 8) { 2184 ifp->if_oerrors++; 2185 continue; 2186 } 2187 2188 if (wpi_tx_data(sc, m0, ni, 0) != 0) { 2189 ifp->if_oerrors++; 2190 break; 2191 } 2192 } else { 2193 if (ic->ic_state != IEEE80211_S_RUN) 2194 break; 2195 2196 IFQ_POLL(&ifp->if_snd, m0); 2197 if (m0 == NULL) 2198 break; 2199 2200 /* 2201 * Cancel any background scan. 2202 */ 2203 if (ic->ic_flags & IEEE80211_F_SCAN) 2204 ieee80211_cancel_scan(ic); 2205 2206 if (m0->m_len < sizeof (*eh) && 2207 (m0 = m_pullup(m0, sizeof (*eh))) != NULL) { 2208 ifp->if_oerrors++; 2209 continue; 2210 } 2211 eh = mtod(m0, struct ether_header *); 2212 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 2213 if (ni == NULL) { 2214 m_freem(m0); 2215 ifp->if_oerrors++; 2216 continue; 2217 } 2218 2219 /* classify mbuf so we can find which tx ring to use */ 2220 if (ieee80211_classify(ic, m0, ni) != 0) { 2221 m_freem(m0); 2222 ieee80211_free_node(ni); 2223 ifp->if_oerrors++; 2224 continue; 2225 } 2226 2227 /* no QoS encapsulation for EAPOL frames */ 2228 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ? 2229 M_WME_GETAC(m0) : WME_AC_BE; 2230 2231 if (sc->txq[ac].queued > sc->txq[ac].count - 8) { 2232 /* there is no place left in this ring */ 2233 IFQ_DRV_PREPEND(&ifp->if_snd, m0); 2234 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2235 break; 2236 } 2237 2238 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 2239 BPF_MTAP(ifp, m0); 2240 2241 m0 = ieee80211_encap(ic, m0, ni); 2242 if (m0 == NULL) { 2243 ieee80211_free_node(ni); 2244 ifp->if_oerrors++; 2245 continue; 2246 } 2247 2248#ifndef WPI_CURRENT 2249 if (ic->ic_rawbpf != NULL) 2250#else 2251 if (bpf_peers_present(ic->ic_rawbpf)) 2252#endif 2253 bpf_mtap(ic->ic_rawbpf, m0); 2254 2255 if (wpi_tx_data(sc, m0, ni, ac) != 0) { 2256 ieee80211_free_node(ni); 2257 ifp->if_oerrors++; 2258 break; 2259 } 2260 } 2261 2262 sc->sc_tx_timer = 5; 2263 sc->watchdog_cnt = 5; 2264 ic->ic_lastdata = ticks; 2265 } 2266 2267 WPI_UNLOCK(sc); 2268} 2269 2270static void 2271wpi_watchdog(struct ifnet *ifp) 2272{ 2273 struct wpi_softc *sc = ifp->if_softc; 2274 WPI_LOCK_DECL; 2275 2276 WPI_LOCK(sc); 2277 2278 DPRINTFN(WPI_DEBUG_WATCHDOG, ("watchdog_cnt: %d\n", sc->watchdog_cnt)); 2279 2280 if (sc->watchdog_cnt == 0 || --sc->watchdog_cnt) 2281 goto done; 2282 2283 if (--sc->sc_tx_timer != 0) { 2284 device_printf(sc->sc_dev,"device timeout\n"); 2285 ifp->if_oerrors++; 2286 taskqueue_enqueue(sc->sc_tq2, &sc->sc_restarttask); 2287 } 2288done: 2289 WPI_UNLOCK(sc); 2290} 2291 2292static int 2293wpi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2294{ 2295 struct wpi_softc *sc = ifp->if_softc; 2296 struct ieee80211com *ic = &sc->sc_ic; 2297 int error = 0; 2298 WPI_LOCK_DECL; 2299 2300 WPI_LOCK(sc); 2301 2302 switch (cmd) { 2303 case SIOCSIFFLAGS: 2304 if ((ifp->if_flags & IFF_UP)) { 2305 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 2306 wpi_init(sc); 2307 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2308 wpi_stop_locked(sc); 2309 break; 2310 default: 2311 WPI_UNLOCK(sc); 2312 error = ieee80211_ioctl(ic, cmd, data); 2313 WPI_LOCK(sc); 2314 } 2315 2316 if (error == ENETRESET) { 2317 if ((ifp->if_flags & IFF_UP) && 2318 (ifp->if_drv_flags & IFF_DRV_RUNNING) && 2319 ic->ic_roaming != IEEE80211_ROAMING_MANUAL) 2320 wpi_init(sc); 2321 error = 0; 2322 } 2323 2324 WPI_UNLOCK(sc); 2325 2326 return error; 2327} 2328 2329/* 2330 * Extract various information from EEPROM. 2331 */ 2332static void 2333wpi_read_eeprom(struct wpi_softc *sc) 2334{ 2335 struct ieee80211com *ic = &sc->sc_ic; 2336 int i; 2337 2338 /* read the hardware capabilities, revision and SKU type */ 2339 wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap,1); 2340 wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev,2); 2341 wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1); 2342 2343 /* read the regulatory domain */ 2344 wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain, 4); 2345 2346 /* read in the hw MAC address */ 2347 wpi_read_prom_data(sc, WPI_EEPROM_MAC, ic->ic_myaddr, 6); 2348 2349 /* read the list of authorized channels */ 2350 for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++) 2351 wpi_read_eeprom_channels(sc,i); 2352 2353 /* read the power level calibration info for each group */ 2354 for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++) 2355 wpi_read_eeprom_group(sc,i); 2356} 2357 2358/* 2359 * Send a command to the firmware. 2360 */ 2361static int 2362wpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async) 2363{ 2364 struct wpi_tx_ring *ring = &sc->cmdq; 2365 struct wpi_tx_desc *desc; 2366 struct wpi_tx_cmd *cmd; 2367 2368#ifdef WPI_DEBUG 2369 if (!async) { 2370 WPI_LOCK_ASSERT(sc); 2371 } 2372#endif 2373 2374 DPRINTFN(WPI_DEBUG_CMD,("wpi_cmd %d size %d async %d\n", code, size, 2375 async)); 2376 2377 if (sc->flags & WPI_FLAG_BUSY) { 2378 device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n", 2379 __func__, code); 2380 return EAGAIN; 2381 } 2382 sc->flags|= WPI_FLAG_BUSY; 2383 2384 KASSERT(size <= sizeof cmd->data, ("command %d too large: %d bytes", 2385 code, size)); 2386 2387 desc = &ring->desc[ring->cur]; 2388 cmd = &ring->cmd[ring->cur]; 2389 2390 cmd->code = code; 2391 cmd->flags = 0; 2392 cmd->qid = ring->qid; 2393 cmd->idx = ring->cur; 2394 memcpy(cmd->data, buf, size); 2395 2396 desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24); 2397 desc->segs[0].addr = htole32(ring->cmd_dma.paddr + 2398 ring->cur * sizeof (struct wpi_tx_cmd)); 2399 desc->segs[0].len = htole32(4 + size); 2400 2401 /* kick cmd ring */ 2402 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT; 2403 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2404 2405 if (async) { 2406 sc->flags &= ~ WPI_FLAG_BUSY; 2407 return 0; 2408 } 2409 2410 return msleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz); 2411} 2412 2413static int 2414wpi_wme_update(struct ieee80211com *ic) 2415{ 2416#define WPI_EXP2(v) htole16((1 << (v)) - 1) 2417#define WPI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v)) 2418 struct wpi_softc *sc = ic->ic_ifp->if_softc; 2419 const struct wmeParams *wmep; 2420 struct wpi_wme_setup wme; 2421 int ac; 2422 2423 /* don't override default WME values if WME is not actually enabled */ 2424 if (!(ic->ic_flags & IEEE80211_F_WME)) 2425 return 0; 2426 2427 wme.flags = 0; 2428 for (ac = 0; ac < WME_NUM_AC; ac++) { 2429 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac]; 2430 wme.ac[ac].aifsn = wmep->wmep_aifsn; 2431 wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin); 2432 wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax); 2433 wme.ac[ac].txop = WPI_USEC(wmep->wmep_txopLimit); 2434 2435 DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d " 2436 "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin, 2437 wme.ac[ac].cwmax, wme.ac[ac].txop)); 2438 } 2439 2440 return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1); 2441#undef WPI_USEC 2442#undef WPI_EXP2 2443} 2444 2445/* 2446 * Configure h/w multi-rate retries. 2447 */ 2448static int 2449wpi_mrr_setup(struct wpi_softc *sc) 2450{ 2451 struct ieee80211com *ic = &sc->sc_ic; 2452 struct wpi_mrr_setup mrr; 2453 int i, error; 2454 2455 memset(&mrr, 0, sizeof (struct wpi_mrr_setup)); 2456 2457 /* CCK rates (not used with 802.11a) */ 2458 for (i = WPI_CCK1; i <= WPI_CCK11; i++) { 2459 mrr.rates[i].flags = 0; 2460 mrr.rates[i].signal = wpi_ridx_to_plcp[i]; 2461 /* fallback to the immediate lower CCK rate (if any) */ 2462 mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1; 2463 /* try one time at this rate before falling back to "next" */ 2464 mrr.rates[i].ntries = 1; 2465 } 2466 2467 /* OFDM rates (not used with 802.11b) */ 2468 for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) { 2469 mrr.rates[i].flags = 0; 2470 mrr.rates[i].signal = wpi_ridx_to_plcp[i]; 2471 /* fallback to the immediate lower OFDM rate (if any) */ 2472 /* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */ 2473 mrr.rates[i].next = (i == WPI_OFDM6) ? 2474 ((ic->ic_curmode == IEEE80211_MODE_11A) ? 2475 WPI_OFDM6 : WPI_CCK2) : 2476 i - 1; 2477 /* try one time at this rate before falling back to "next" */ 2478 mrr.rates[i].ntries = 1; 2479 } 2480 2481 /* setup MRR for control frames */ 2482 mrr.which = htole32(WPI_MRR_CTL); 2483 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0); 2484 if (error != 0) { 2485 device_printf(sc->sc_dev, 2486 "could not setup MRR for control frames\n"); 2487 return error; 2488 } 2489 2490 /* setup MRR for data frames */ 2491 mrr.which = htole32(WPI_MRR_DATA); 2492 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0); 2493 if (error != 0) { 2494 device_printf(sc->sc_dev, 2495 "could not setup MRR for data frames\n"); 2496 return error; 2497 } 2498 2499 return 0; 2500} 2501 2502static void 2503wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on) 2504{ 2505 struct wpi_cmd_led led; 2506 2507 led.which = which; 2508 led.unit = htole32(100000); /* on/off in unit of 100ms */ 2509 led.off = off; 2510 led.on = on; 2511 2512 (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1); 2513} 2514 2515static void 2516wpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni) 2517{ 2518 struct wpi_cmd_tsf tsf; 2519 uint64_t val, mod; 2520 2521 memset(&tsf, 0, sizeof tsf); 2522 memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8); 2523 tsf.bintval = htole16(ni->ni_intval); 2524 tsf.lintval = htole16(10); 2525 2526 /* compute remaining time until next beacon */ 2527 val = (uint64_t)ni->ni_intval * 1024; /* msec -> usec */ 2528 mod = le64toh(tsf.tstamp) % val; 2529 tsf.binitval = htole32((uint32_t)(val - mod)); 2530 2531 if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0) 2532 device_printf(sc->sc_dev, "could not enable TSF\n"); 2533} 2534 2535#if 0 2536/* 2537 * Build a beacon frame that the firmware will broadcast periodically in 2538 * IBSS or HostAP modes. 2539 */ 2540static int 2541wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni) 2542{ 2543 struct ieee80211com *ic = &sc->sc_ic; 2544 struct wpi_tx_ring *ring = &sc->cmdq; 2545 struct wpi_tx_desc *desc; 2546 struct wpi_tx_data *data; 2547 struct wpi_tx_cmd *cmd; 2548 struct wpi_cmd_beacon *bcn; 2549 struct ieee80211_beacon_offsets bo; 2550 struct mbuf *m0; 2551 bus_addr_t physaddr; 2552 int error; 2553 2554 desc = &ring->desc[ring->cur]; 2555 data = &ring->data[ring->cur]; 2556 2557 m0 = ieee80211_beacon_alloc(ic, ni, &bo); 2558 if (m0 == NULL) { 2559 device_printf(sc->sc_dev, "could not allocate beacon frame\n"); 2560 return ENOMEM; 2561 } 2562 2563 cmd = &ring->cmd[ring->cur]; 2564 cmd->code = WPI_CMD_SET_BEACON; 2565 cmd->flags = 0; 2566 cmd->qid = ring->qid; 2567 cmd->idx = ring->cur; 2568 2569 bcn = (struct wpi_cmd_beacon *)cmd->data; 2570 memset(bcn, 0, sizeof (struct wpi_cmd_beacon)); 2571 bcn->id = WPI_ID_BROADCAST; 2572 bcn->ofdm_mask = 0xff; 2573 bcn->cck_mask = 0x0f; 2574 bcn->lifetime = htole32(WPI_LIFETIME_INFINITE); 2575 bcn->len = htole16(m0->m_pkthdr.len); 2576 bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ? 2577 wpi_plcp_signal(12) : wpi_plcp_signal(2); 2578 bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP); 2579 2580 /* save and trim IEEE802.11 header */ 2581 m_copydata(m0, 0, sizeof (struct ieee80211_frame), (caddr_t)&bcn->wh); 2582 m_adj(m0, sizeof (struct ieee80211_frame)); 2583 2584 /* assume beacon frame is contiguous */ 2585 error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m0, void *), 2586 m0->m_pkthdr.len, wpi_dma_map_addr, &physaddr, 0); 2587 if (error != 0) { 2588 device_printf(sc->sc_dev, "could not map beacon\n"); 2589 m_freem(m0); 2590 return error; 2591 } 2592 2593 data->m = m0; 2594 2595 /* first scatter/gather segment is used by the beacon command */ 2596 desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24); 2597 desc->segs[0].addr = htole32(ring->cmd_dma.paddr + 2598 ring->cur * sizeof (struct wpi_tx_cmd)); 2599 desc->segs[0].len = htole32(4 + sizeof (struct wpi_cmd_beacon)); 2600 desc->segs[1].addr = htole32(physaddr); 2601 desc->segs[1].len = htole32(m0->m_pkthdr.len); 2602 2603 /* kick cmd ring */ 2604 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT; 2605 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2606 2607 return 0; 2608} 2609#endif 2610 2611static int 2612wpi_auth(struct wpi_softc *sc) 2613{ 2614 struct ieee80211com *ic = &sc->sc_ic; 2615 struct ieee80211_node *ni = ic->ic_bss; 2616 struct wpi_node_info node; 2617 int error; 2618 2619 /* update adapter's configuration */ 2620 IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid); 2621 sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 2622 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) { 2623 sc->config.flags |= htole32(WPI_CONFIG_AUTO | 2624 WPI_CONFIG_24GHZ); 2625 } 2626 switch (ic->ic_curmode) { 2627 case IEEE80211_MODE_11A: 2628 sc->config.cck_mask = 0; 2629 sc->config.ofdm_mask = 0x15; 2630 break; 2631 case IEEE80211_MODE_11B: 2632 sc->config.cck_mask = 0x03; 2633 sc->config.ofdm_mask = 0; 2634 break; 2635 default: /* assume 802.11b/g */ 2636 sc->config.cck_mask = 0x0f; 2637 sc->config.ofdm_mask = 0x15; 2638 } 2639 2640 DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan, 2641 sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask)); 2642 error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config, 2643 sizeof (struct wpi_config), 1); 2644 if (error != 0) { 2645 device_printf(sc->sc_dev, "could not configure\n"); 2646 return error; 2647 } 2648 2649 /* configuration has changed, set Tx power accordingly */ 2650 if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) { 2651 device_printf(sc->sc_dev, "could not set Tx power\n"); 2652 return error; 2653 } 2654 2655 /* add default node */ 2656 memset(&node, 0, sizeof node); 2657 IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid); 2658 node.id = WPI_ID_BSS; 2659 node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ? 2660 wpi_plcp_signal(12) : wpi_plcp_signal(2); 2661 node.action = htole32(WPI_ACTION_SET_RATE); 2662 node.antenna = WPI_ANTENNA_BOTH; 2663 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 2664 if (error != 0) { 2665 device_printf(sc->sc_dev, "could not add BSS node\n"); 2666 return error; 2667 } 2668 2669 sc->flags &= ~WPI_FLAG_AUTH; 2670 2671 return 0; 2672} 2673 2674/* 2675 * Send a scan request to the firmware. Since this command is huge, we map it 2676 * into a mbufcluster instead of using the pre-allocated set of commands. Note, 2677 * much of this code is similar to that in wpi_cmd but because we must manually 2678 * construct the probe & channels, we duplicate what's needed here. XXX In the 2679 * future, this function should be modified to use wpi_cmd to help cleanup the 2680 * code base. 2681 */ 2682static int 2683wpi_scan(struct wpi_softc *sc) 2684{ 2685 struct ieee80211com *ic = &sc->sc_ic; 2686 struct wpi_tx_ring *ring = &sc->cmdq; 2687 struct wpi_tx_desc *desc; 2688 struct wpi_tx_data *data; 2689 struct wpi_tx_cmd *cmd; 2690 struct wpi_scan_hdr *hdr; 2691 struct wpi_scan_chan *chan; 2692 struct ieee80211_frame *wh; 2693 struct ieee80211_rateset *rs; 2694 struct ieee80211_channel *c; 2695 enum ieee80211_phymode mode; 2696 uint8_t *frm; 2697 int nrates, pktlen, error; 2698 bus_addr_t physaddr; 2699 struct ifnet *ifp = ic->ic_ifp; 2700 2701 desc = &ring->desc[ring->cur]; 2702 data = &ring->data[ring->cur]; 2703 2704 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 2705 if (data->m == NULL) { 2706 device_printf(sc->sc_dev, 2707 "could not allocate mbuf for scan command\n"); 2708 return ENOMEM; 2709 } 2710 2711 cmd = mtod(data->m, struct wpi_tx_cmd *); 2712 cmd->code = WPI_CMD_SCAN; 2713 cmd->flags = 0; 2714 cmd->qid = ring->qid; 2715 cmd->idx = ring->cur; 2716 2717 hdr = (struct wpi_scan_hdr *)cmd->data; 2718 memset(hdr, 0, sizeof(struct wpi_scan_hdr)); 2719 2720 /* 2721 * Move to the next channel if no packets are received within 5 msecs 2722 * after sending the probe request (this helps to reduce the duration 2723 * of active scans). 2724 */ 2725 hdr->quiet = htole16(5); 2726 hdr->threshold = htole16(1); 2727 2728 if (IEEE80211_IS_CHAN_A(ic->ic_curchan)) { 2729 /* send probe requests at 6Mbps */ 2730 hdr->tx.rate = wpi_ridx_to_plcp[WPI_OFDM6]; 2731 2732 /* Enable crc checking */ 2733 hdr->promotion = htole16(1); 2734 } else { 2735 hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO); 2736 /* send probe requests at 1Mbps */ 2737 hdr->tx.rate = wpi_ridx_to_plcp[WPI_CCK1]; 2738 } 2739 hdr->tx.id = WPI_ID_BROADCAST; 2740 hdr->tx.lifetime = htole32(WPI_LIFETIME_INFINITE); 2741 hdr->tx.flags = htole32(WPI_TX_AUTO_SEQ); 2742 2743 /*XXX Need to cater for multiple essids */ 2744 memset(&hdr->scan_essids[0], 0, 4 * sizeof(hdr->scan_essids[0])); 2745 hdr->scan_essids[0].id = IEEE80211_ELEMID_SSID; 2746 hdr->scan_essids[0].esslen = ic->ic_des_ssid[0].len; 2747 memcpy(hdr->scan_essids[0].essid, ic->ic_des_ssid[0].ssid, 2748 ic->ic_des_ssid[0].len); 2749 2750 if (wpi_debug & WPI_DEBUG_SCANNING) { 2751 printf("Scanning Essid: "); 2752 ieee80211_print_essid(ic->ic_des_ssid[0].ssid, 2753 ic->ic_des_ssid[0].len); 2754 printf("\n"); 2755 } 2756 2757 /* 2758 * Build a probe request frame. Most of the following code is a 2759 * copy & paste of what is done in net80211. 2760 */ 2761 wh = (struct ieee80211_frame *)&hdr->scan_essids[4]; 2762 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 2763 IEEE80211_FC0_SUBTYPE_PROBE_REQ; 2764 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2765 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 2766 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 2767 IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr); 2768 *(u_int16_t *)&wh->i_dur[0] = 0; /* filled by h/w */ 2769 *(u_int16_t *)&wh->i_seq[0] = 0; /* filled by h/w */ 2770 2771 frm = (uint8_t *)(wh + 1); 2772 2773 /* add essid IE, the hardware will fill this in for us */ 2774 *frm++ = IEEE80211_ELEMID_SSID; 2775 *frm++ = 0; 2776 2777 mode = ieee80211_chan2mode(ic->ic_curchan); 2778 rs = &ic->ic_sup_rates[mode]; 2779 2780 /* add supported rates IE */ 2781 *frm++ = IEEE80211_ELEMID_RATES; 2782 nrates = rs->rs_nrates; 2783 if (nrates > IEEE80211_RATE_SIZE) 2784 nrates = IEEE80211_RATE_SIZE; 2785 *frm++ = nrates; 2786 memcpy(frm, rs->rs_rates, nrates); 2787 frm += nrates; 2788 2789 /* add supported xrates IE */ 2790 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 2791 nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 2792 *frm++ = IEEE80211_ELEMID_XRATES; 2793 *frm++ = nrates; 2794 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 2795 frm += nrates; 2796 } 2797 2798 /* setup length of probe request */ 2799 hdr->tx.len = htole16(frm - (uint8_t *)wh); 2800 2801 /* 2802 * Construct information about the channel that we 2803 * want to scan. The firmware expects this to be directly 2804 * after the scan probe request 2805 */ 2806 c = ic->ic_curchan; 2807 chan = (struct wpi_scan_chan *)frm; 2808 chan->chan = ieee80211_chan2ieee(ic, c); 2809 chan->flags = 0; 2810 if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) { 2811 chan->flags |= WPI_CHAN_ACTIVE; 2812 if (ic->ic_des_ssid[0].len != 0) 2813 chan->flags |= WPI_CHAN_DIRECT; 2814 } 2815 chan->gain_dsp = 0x6e; /* Default level */ 2816 if (IEEE80211_IS_CHAN_5GHZ(c)) { 2817 chan->active = htole16(10); 2818 chan->passive = htole16(sc->maxdwell); 2819 chan->gain_radio = 0x3b; 2820 } else { 2821 chan->active = htole16(20); 2822 chan->passive = htole16(sc->maxdwell); 2823 chan->gain_radio = 0x28; 2824 } 2825 2826 DPRINTFN(WPI_DEBUG_SCANNING, 2827 ("Scanning %u Passive: %d\n", 2828 chan->chan, 2829 c->ic_flags & IEEE80211_CHAN_PASSIVE)); 2830 2831 hdr->nchan++; 2832 chan++; 2833 2834 frm += sizeof (struct wpi_scan_chan); 2835#if 0 2836 // XXX All Channels.... 2837 for (c = &ic->ic_channels[1]; 2838 c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) { 2839 if ((c->ic_flags & ic->ic_curchan->ic_flags) != ic->ic_curchan->ic_flags) 2840 continue; 2841 2842 chan->chan = ieee80211_chan2ieee(ic, c); 2843 chan->flags = 0; 2844 if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) { 2845 chan->flags |= WPI_CHAN_ACTIVE; 2846 if (ic->ic_des_ssid[0].len != 0) 2847 chan->flags |= WPI_CHAN_DIRECT; 2848 } 2849 chan->gain_dsp = 0x6e; /* Default level */ 2850 if (IEEE80211_IS_CHAN_5GHZ(c)) { 2851 chan->active = htole16(10); 2852 chan->passive = htole16(110); 2853 chan->gain_radio = 0x3b; 2854 } else { 2855 chan->active = htole16(20); 2856 chan->passive = htole16(120); 2857 chan->gain_radio = 0x28; 2858 } 2859 2860 DPRINTFN(WPI_DEBUG_SCANNING, 2861 ("Scanning %u Passive: %d\n", 2862 chan->chan, 2863 c->ic_flags & IEEE80211_CHAN_PASSIVE)); 2864 2865 hdr->nchan++; 2866 chan++; 2867 2868 frm += sizeof (struct wpi_scan_chan); 2869 } 2870#endif 2871 2872 hdr->len = htole16(frm - (uint8_t *)hdr); 2873 pktlen = frm - (uint8_t *)cmd; 2874 2875 error = bus_dmamap_load(ring->data_dmat, data->map, cmd, pktlen, 2876 wpi_dma_map_addr, &physaddr, BUS_DMA_NOWAIT); 2877 if (error != 0) { 2878 device_printf(sc->sc_dev, "could not map scan command\n"); 2879 m_freem(data->m); 2880 data->m = NULL; 2881 return error; 2882 } 2883 2884 desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24); 2885 desc->segs[0].addr = htole32(physaddr); 2886 desc->segs[0].len = htole32(pktlen); 2887 2888 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 2889 BUS_DMASYNC_PREWRITE); 2890 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 2891 2892 /* kick cmd ring */ 2893 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT; 2894 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2895 2896 return 0; /* will be notified async. of failure/success */ 2897} 2898 2899/** 2900 * Configure the card to listen to a particular channel, this transisions the 2901 * card in to being able to receive frames from remote devices. 2902 */ 2903static int 2904wpi_config(struct wpi_softc *sc) 2905{ 2906 struct ieee80211com *ic = &sc->sc_ic; 2907 struct ifnet *ifp = ic->ic_ifp; 2908 struct wpi_power power; 2909 struct wpi_bluetooth bluetooth; 2910 struct wpi_node_info node; 2911 int error; 2912 2913 /* set power mode */ 2914 memset(&power, 0, sizeof power); 2915 power.flags = htole32(WPI_POWER_CAM|0x8); 2916 error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0); 2917 if (error != 0) { 2918 device_printf(sc->sc_dev, "could not set power mode\n"); 2919 return error; 2920 } 2921 2922 /* configure bluetooth coexistence */ 2923 memset(&bluetooth, 0, sizeof bluetooth); 2924 bluetooth.flags = 3; 2925 bluetooth.lead = 0xaa; 2926 bluetooth.kill = 1; 2927 error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth, 2928 0); 2929 if (error != 0) { 2930 device_printf(sc->sc_dev, 2931 "could not configure bluetooth coexistence\n"); 2932 return error; 2933 } 2934 2935 /* configure adapter */ 2936 memset(&sc->config, 0, sizeof (struct wpi_config)); 2937 IEEE80211_ADDR_COPY(sc->config.myaddr, ic->ic_myaddr); 2938 /*set default channel*/ 2939 sc->config.chan = htole16(ieee80211_chan2ieee(ic, ic->ic_curchan)); 2940 sc->config.flags = htole32(WPI_CONFIG_TSF); 2941 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) { 2942 sc->config.flags |= htole32(WPI_CONFIG_AUTO | 2943 WPI_CONFIG_24GHZ); 2944 } 2945 sc->config.filter = 0; 2946 switch (ic->ic_opmode) { 2947 case IEEE80211_M_STA: 2948 case IEEE80211_M_WDS: /* No know setup, use STA for now */ 2949 sc->config.mode = WPI_MODE_STA; 2950 sc->config.filter |= htole32(WPI_FILTER_MULTICAST); 2951 break; 2952 case IEEE80211_M_IBSS: 2953 case IEEE80211_M_AHDEMO: 2954 sc->config.mode = WPI_MODE_IBSS; 2955 sc->config.filter |= htole32(WPI_FILTER_BEACON | 2956 WPI_FILTER_MULTICAST); 2957 break; 2958 case IEEE80211_M_HOSTAP: 2959 sc->config.mode = WPI_MODE_HOSTAP; 2960 break; 2961 case IEEE80211_M_MONITOR: 2962 sc->config.mode = WPI_MODE_MONITOR; 2963 sc->config.filter |= htole32(WPI_FILTER_MULTICAST | 2964 WPI_FILTER_CTL | WPI_FILTER_PROMISC); 2965 break; 2966 } 2967 sc->config.cck_mask = 0x0f; /* not yet negotiated */ 2968 sc->config.ofdm_mask = 0xff; /* not yet negotiated */ 2969 error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config, 2970 sizeof (struct wpi_config), 0); 2971 if (error != 0) { 2972 device_printf(sc->sc_dev, "configure command failed\n"); 2973 return error; 2974 } 2975 2976 /* configuration has changed, set Tx power accordingly */ 2977 if ((error = wpi_set_txpower(sc, ic->ic_curchan,0)) != 0) { 2978 device_printf(sc->sc_dev, "could not set Tx power\n"); 2979 return error; 2980 } 2981 2982 /* add broadcast node */ 2983 memset(&node, 0, sizeof node); 2984 IEEE80211_ADDR_COPY(node.bssid, ifp->if_broadcastaddr); 2985 node.id = WPI_ID_BROADCAST; 2986 node.rate = wpi_plcp_signal(2); 2987 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0); 2988 if (error != 0) { 2989 device_printf(sc->sc_dev, "could not add broadcast node\n"); 2990 return error; 2991 } 2992 2993 /* Setup rate scalling */ 2994 error = wpi_mrr_setup(sc); 2995 if (error != 0) { 2996 device_printf(sc->sc_dev, "could not setup MRR\n"); 2997 return error; 2998 } 2999 3000 return 0; 3001} 3002 3003static void 3004wpi_stop_master(struct wpi_softc *sc) 3005{ 3006 uint32_t tmp; 3007 int ntries; 3008 3009 DPRINTFN(WPI_DEBUG_HW,("Disabling Firmware execution\n")); 3010 3011 tmp = WPI_READ(sc, WPI_RESET); 3012 WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER | WPI_NEVO_RESET); 3013 3014 tmp = WPI_READ(sc, WPI_GPIO_CTL); 3015 if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP) 3016 return; /* already asleep */ 3017 3018 for (ntries = 0; ntries < 100; ntries++) { 3019 if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED) 3020 break; 3021 DELAY(10); 3022 } 3023 if (ntries == 100) { 3024 device_printf(sc->sc_dev, "timeout waiting for master\n"); 3025 } 3026} 3027 3028static int 3029wpi_power_up(struct wpi_softc *sc) 3030{ 3031 uint32_t tmp; 3032 int ntries; 3033 3034 wpi_mem_lock(sc); 3035 tmp = wpi_mem_read(sc, WPI_MEM_POWER); 3036 wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000); 3037 wpi_mem_unlock(sc); 3038 3039 for (ntries = 0; ntries < 5000; ntries++) { 3040 if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED) 3041 break; 3042 DELAY(10); 3043 } 3044 if (ntries == 5000) { 3045 device_printf(sc->sc_dev, 3046 "timeout waiting for NIC to power up\n"); 3047 return ETIMEDOUT; 3048 } 3049 return 0; 3050} 3051 3052static int 3053wpi_reset(struct wpi_softc *sc) 3054{ 3055 uint32_t tmp; 3056 int ntries; 3057 3058 DPRINTFN(WPI_DEBUG_HW, 3059 ("Resetting the card - clearing any uploaded firmware\n")); 3060 3061 /* clear any pending interrupts */ 3062 WPI_WRITE(sc, WPI_INTR, 0xffffffff); 3063 3064 tmp = WPI_READ(sc, WPI_PLL_CTL); 3065 WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT); 3066 3067 tmp = WPI_READ(sc, WPI_CHICKEN); 3068 WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS); 3069 3070 tmp = WPI_READ(sc, WPI_GPIO_CTL); 3071 WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT); 3072 3073 /* wait for clock stabilization */ 3074 for (ntries = 0; ntries < 25000; ntries++) { 3075 if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK) 3076 break; 3077 DELAY(10); 3078 } 3079 if (ntries == 25000) { 3080 device_printf(sc->sc_dev, 3081 "timeout waiting for clock stabilization\n"); 3082 return ETIMEDOUT; 3083 } 3084 3085 /* initialize EEPROM */ 3086 tmp = WPI_READ(sc, WPI_EEPROM_STATUS); 3087 3088 if ((tmp & WPI_EEPROM_VERSION) == 0) { 3089 device_printf(sc->sc_dev, "EEPROM not found\n"); 3090 return EIO; 3091 } 3092 WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED); 3093 3094 return 0; 3095} 3096 3097static void 3098wpi_hw_config(struct wpi_softc *sc) 3099{ 3100 uint32_t rev, hw; 3101 3102 /* voodoo from the Linux "driver".. */ 3103 hw = WPI_READ(sc, WPI_HWCONFIG); 3104 3105 rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1); 3106 if ((rev & 0xc0) == 0x40) 3107 hw |= WPI_HW_ALM_MB; 3108 else if (!(rev & 0x80)) 3109 hw |= WPI_HW_ALM_MM; 3110 3111 if (sc->cap == 0x80) 3112 hw |= WPI_HW_SKU_MRC; 3113 3114 hw &= ~WPI_HW_REV_D; 3115 if ((le16toh(sc->rev) & 0xf0) == 0xd0) 3116 hw |= WPI_HW_REV_D; 3117 3118 if (sc->type > 1) 3119 hw |= WPI_HW_TYPE_B; 3120 3121 WPI_WRITE(sc, WPI_HWCONFIG, hw); 3122} 3123 3124static void 3125wpi_init(void *arg) 3126{ 3127 struct wpi_softc *sc = arg; 3128 struct ieee80211com *ic = &sc->sc_ic; 3129 struct ifnet *ifp = ic->ic_ifp; 3130 uint32_t tmp; 3131 int ntries, error, qid; 3132 WPI_LOCK_DECL; 3133 3134 WPI_LOCK(sc); 3135 3136 wpi_stop_locked(sc); 3137 (void)wpi_reset(sc); 3138 3139 wpi_mem_lock(sc); 3140 wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00); 3141 DELAY(20); 3142 tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV); 3143 wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800); 3144 wpi_mem_unlock(sc); 3145 3146 (void)wpi_power_up(sc); 3147 wpi_hw_config(sc); 3148 3149 /* init Rx ring */ 3150 wpi_mem_lock(sc); 3151 WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr); 3152 WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr + 3153 offsetof(struct wpi_shared, next)); 3154 WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7); 3155 WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010); 3156 wpi_mem_unlock(sc); 3157 3158 /* init Tx rings */ 3159 wpi_mem_lock(sc); 3160 wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */ 3161 wpi_mem_write(sc, WPI_MEM_RA, 1); /* enable RA0 */ 3162 wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */ 3163 wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000); 3164 wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002); 3165 wpi_mem_write(sc, WPI_MEM_MAGIC4, 4); 3166 wpi_mem_write(sc, WPI_MEM_MAGIC5, 5); 3167 3168 WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr); 3169 WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5); 3170 3171 for (qid = 0; qid < 6; qid++) { 3172 WPI_WRITE(sc, WPI_TX_CTL(qid), 0); 3173 WPI_WRITE(sc, WPI_TX_BASE(qid), 0); 3174 WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008); 3175 } 3176 wpi_mem_unlock(sc); 3177 3178 /* clear "radio off" and "disable command" bits (reversed logic) */ 3179 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF); 3180 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD); 3181 sc->flags &= ~WPI_FLAG_HW_RADIO_OFF; 3182 3183 /* clear any pending interrupts */ 3184 WPI_WRITE(sc, WPI_INTR, 0xffffffff); 3185 3186 /* enable interrupts */ 3187 WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK); 3188 3189 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF); 3190 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF); 3191 3192 if ((error = wpi_load_firmware(sc)) != 0) { 3193 device_printf(sc->sc_dev, 3194 "A problem occurred loading the firmware to the driver\n"); 3195 return; 3196 } 3197 3198 /* At this point the firmware is up and running. If the hardware 3199 * RF switch is turned off thermal calibration will fail, though 3200 * the card is still happy to continue to accept commands, catch 3201 * this case and record the hw is disabled. 3202 */ 3203 wpi_mem_lock(sc); 3204 tmp = wpi_mem_read(sc, WPI_MEM_HW_RADIO_OFF); 3205 wpi_mem_unlock(sc); 3206 3207 if (!(tmp & 0x1)) { 3208 sc->flags |= WPI_FLAG_HW_RADIO_OFF; 3209 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3210 ifp->if_drv_flags |= IFF_DRV_RUNNING; 3211 device_printf(sc->sc_dev,"Radio Transmitter is switched off\n"); 3212 return; 3213 } 3214 3215 /* wait for thermal sensors to calibrate */ 3216 for (ntries = 0; ntries < 1000; ntries++) { 3217 if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0) 3218 break; 3219 DELAY(10); 3220 } 3221 3222 if (ntries == 1000) { 3223 device_printf(sc->sc_dev, 3224 "timeout waiting for thermal sensors calibration\n"); 3225 error = ETIMEDOUT; 3226 return; 3227 } 3228 DPRINTFN(WPI_DEBUG_TEMP,("temperature %d\n", sc->temp)); 3229 3230 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3231 ifp->if_drv_flags |= IFF_DRV_RUNNING; 3232 callout_reset(&sc->watchdog_to, hz, wpi_tick, sc); 3233 WPI_UNLOCK(sc); 3234 3235 if (ic->ic_opmode == IEEE80211_M_MONITOR) 3236 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 3237 else if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL) 3238 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 3239} 3240 3241static void 3242wpi_stop(struct wpi_softc *sc) 3243{ 3244 WPI_LOCK_DECL; 3245 3246 WPI_LOCK(sc); 3247 wpi_stop_locked(sc); 3248 WPI_UNLOCK(sc); 3249 3250} 3251static void 3252wpi_stop_locked(struct wpi_softc *sc) 3253 3254{ 3255 struct ieee80211com *ic = &sc->sc_ic; 3256 struct ifnet *ifp = ic->ic_ifp; 3257 uint32_t tmp; 3258 int ac; 3259 3260 sc->watchdog_cnt = sc->sc_tx_timer = 0; 3261 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 3262 3263 /* disable interrupts */ 3264 WPI_WRITE(sc, WPI_MASK, 0); 3265 WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK); 3266 WPI_WRITE(sc, WPI_INTR_STATUS, 0xff); 3267 WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000); 3268 3269 /* Clear any commands left in the command buffer */ 3270 memset(sc->sc_cmd, 0, sizeof(sc->sc_cmd)); 3271 3272 wpi_mem_lock(sc); 3273 wpi_mem_write(sc, WPI_MEM_MODE, 0); 3274 wpi_mem_unlock(sc); 3275 3276 /* reset all Tx rings */ 3277 for (ac = 0; ac < 4; ac++) 3278 wpi_reset_tx_ring(sc, &sc->txq[ac]); 3279 wpi_reset_tx_ring(sc, &sc->cmdq); 3280 3281 /* reset Rx ring */ 3282 wpi_reset_rx_ring(sc, &sc->rxq); 3283 3284 wpi_mem_lock(sc); 3285 wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200); 3286 wpi_mem_unlock(sc); 3287 3288 DELAY(5); 3289 3290 wpi_stop_master(sc); 3291 3292 tmp = WPI_READ(sc, WPI_RESET); 3293 WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET); 3294 sc->flags &= ~WPI_FLAG_BUSY; 3295 3296 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 3297} 3298 3299static void 3300wpi_iter_func(void *arg, struct ieee80211_node *ni) 3301{ 3302 struct wpi_softc *sc = arg; 3303 struct wpi_node *wn = (struct wpi_node *)ni; 3304 3305 ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn); 3306} 3307 3308static void 3309wpi_newassoc(struct ieee80211_node *ni, int isnew) 3310{ 3311 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc; 3312 int i; 3313 3314 ieee80211_amrr_node_init(&sc->amrr, &((struct wpi_node *)ni)->amn); 3315 3316 for (i = ni->ni_rates.rs_nrates - 1; 3317 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72; 3318 i--); 3319 ni->ni_txrate = i; 3320} 3321 3322static void 3323wpi_calib_timeout(void *arg) 3324{ 3325 struct wpi_softc *sc = arg; 3326 struct ieee80211com *ic = &sc->sc_ic; 3327 int temp; 3328 WPI_LOCK_DECL; 3329 3330 /* automatic rate control triggered every 500ms */ 3331 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) { 3332 WPI_LOCK(sc); 3333 if (ic->ic_opmode == IEEE80211_M_STA) 3334 wpi_iter_func(sc, ic->ic_bss); 3335 else 3336 ieee80211_iterate_nodes(&ic->ic_sta, wpi_iter_func, sc); 3337 WPI_UNLOCK(sc); 3338 } 3339 3340 /* update sensor data */ 3341 temp = (int)WPI_READ(sc, WPI_TEMPERATURE); 3342 DPRINTFN(WPI_DEBUG_TEMP,("Temp in calibration is: %d\n", temp)); 3343#if 0 3344 //XXX Used by OpenBSD Sensor Framework 3345 sc->sensor.value = temp + 260; 3346#endif 3347 3348 /* automatic power calibration every 60s */ 3349 if (++sc->calib_cnt >= 120) { 3350 wpi_power_calibration(sc, temp); 3351 sc->calib_cnt = 0; 3352 } 3353 3354 callout_reset(&sc->calib_to, hz/2, wpi_calib_timeout, sc); 3355} 3356 3357/* 3358 * This function is called periodically (every 60 seconds) to adjust output 3359 * power to temperature changes. 3360 */ 3361static void 3362wpi_power_calibration(struct wpi_softc *sc, int temp) 3363{ 3364 /* sanity-check read value */ 3365 if (temp < -260 || temp > 25) { 3366 /* this can't be correct, ignore */ 3367 DPRINTFN(WPI_DEBUG_TEMP, 3368 ("out-of-range temperature reported: %d\n", temp)); 3369 return; 3370 } 3371 3372 DPRINTFN(WPI_DEBUG_TEMP,("temperature %d->%d\n", sc->temp, temp)); 3373 3374 /* adjust Tx power if need be */ 3375 if (abs(temp - sc->temp) <= 6) 3376 return; 3377 3378 sc->temp = temp; 3379 3380 if (wpi_set_txpower(sc, sc->sc_ic.ic_bss->ni_chan,1) != 0) { 3381 /* just warn, too bad for the automatic calibration... */ 3382 device_printf(sc->sc_dev,"could not adjust Tx power\n"); 3383 } 3384} 3385 3386/** 3387 * Read the eeprom to find out what channels are valid for the given 3388 * band and update net80211 with what we find. 3389 */ 3390static void 3391wpi_read_eeprom_channels(struct wpi_softc *sc, int n) 3392{ 3393 struct ieee80211com *ic = &sc->sc_ic; 3394 const struct wpi_chan_band *band = &wpi_bands[n]; 3395 struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND]; 3396 int chan, i, offset, passive; 3397 3398 wpi_read_prom_data(sc, band->addr, channels, 3399 band->nchan * sizeof (struct wpi_eeprom_chan)); 3400 3401 for (i = 0; i < band->nchan; i++) { 3402 if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) { 3403 DPRINTFN(WPI_DEBUG_HW, 3404 ("Channel Not Valid: %d, band %d\n", 3405 band->chan[i],n)); 3406 continue; 3407 } 3408 3409 passive = 0; 3410 chan = band->chan[i]; 3411 offset = ic->ic_nchans; 3412 3413 /* is active scan allowed on this channel? */ 3414 if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) { 3415 passive = IEEE80211_CHAN_PASSIVE; 3416 } 3417 3418 if (n == 0) { /* 2GHz band */ 3419 ic->ic_channels[offset].ic_ieee = chan; 3420 ic->ic_channels[offset].ic_freq = 3421 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ); 3422 ic->ic_channels[offset].ic_flags = IEEE80211_CHAN_B | passive; 3423 offset++; 3424 ic->ic_channels[offset].ic_ieee = chan; 3425 ic->ic_channels[offset].ic_freq = 3426 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ); 3427 ic->ic_channels[offset].ic_flags = IEEE80211_CHAN_G | passive; 3428 offset++; 3429 3430 } else { /* 5GHz band */ 3431 /* 3432 * Some 3945ABG adapters support channels 7, 8, 11 3433 * and 12 in the 2GHz *and* 5GHz bands. 3434 * Because of limitations in our net80211(9) stack, 3435 * we can't support these channels in 5GHz band. 3436 * XXX not true; just need to map to proper frequency 3437 */ 3438 if (chan <= 14) 3439 continue; 3440 3441 ic->ic_channels[offset].ic_ieee = chan; 3442 ic->ic_channels[offset].ic_freq = 3443 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ); 3444 ic->ic_channels[offset].ic_flags = IEEE80211_CHAN_A | passive; 3445 offset++; 3446 } 3447 3448 /* save maximum allowed power for this channel */ 3449 sc->maxpwr[chan] = channels[i].maxpwr; 3450 3451 ic->ic_nchans = offset; 3452 3453#if 0 3454 // XXX We can probably use this an get rid of maxpwr - ben 20070617 3455 ic->ic_channels[chan].ic_maxpower = channels[i].maxpwr; 3456 //ic->ic_channels[chan].ic_minpower... 3457 //ic->ic_channels[chan].ic_maxregtxpower... 3458#endif 3459 3460 DPRINTF(("adding chan %d flags=0x%x maxpwr=%d, offset %d\n", 3461 chan, channels[i].flags, sc->maxpwr[chan], offset)); 3462 } 3463} 3464 3465static void 3466wpi_read_eeprom_group(struct wpi_softc *sc, int n) 3467{ 3468 struct wpi_power_group *group = &sc->groups[n]; 3469 struct wpi_eeprom_group rgroup; 3470 int i; 3471 3472 wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup, 3473 sizeof rgroup); 3474 3475 /* save power group information */ 3476 group->chan = rgroup.chan; 3477 group->maxpwr = rgroup.maxpwr; 3478 /* temperature at which the samples were taken */ 3479 group->temp = (int16_t)le16toh(rgroup.temp); 3480 3481 DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n, 3482 group->chan, group->maxpwr, group->temp)); 3483 3484 for (i = 0; i < WPI_SAMPLES_COUNT; i++) { 3485 group->samples[i].index = rgroup.samples[i].index; 3486 group->samples[i].power = rgroup.samples[i].power; 3487 3488 DPRINTF(("\tsample %d: index=%d power=%d\n", i, 3489 group->samples[i].index, group->samples[i].power)); 3490 } 3491} 3492 3493/* 3494 * Update Tx power to match what is defined for channel `c'. 3495 */ 3496static int 3497wpi_set_txpower(struct wpi_softc *sc, struct ieee80211_channel *c, int async) 3498{ 3499 struct ieee80211com *ic = &sc->sc_ic; 3500 struct wpi_power_group *group; 3501 struct wpi_cmd_txpower txpower; 3502 u_int chan; 3503 int i; 3504 3505 /* get channel number */ 3506 chan = ieee80211_chan2ieee(ic, c); 3507 3508 /* find the power group to which this channel belongs */ 3509 if (IEEE80211_IS_CHAN_5GHZ(c)) { 3510 for (group = &sc->groups[1]; group < &sc->groups[4]; group++) 3511 if (chan <= group->chan) 3512 break; 3513 } else 3514 group = &sc->groups[0]; 3515 3516 memset(&txpower, 0, sizeof txpower); 3517 txpower.band = IEEE80211_IS_CHAN_5GHZ(c) ? 0 : 1; 3518 txpower.channel = htole16(chan); 3519 3520 /* set Tx power for all OFDM and CCK rates */ 3521 for (i = 0; i <= 11 ; i++) { 3522 /* retrieve Tx power for this channel/rate combination */ 3523 int idx = wpi_get_power_index(sc, group, c, 3524 wpi_ridx_to_rate[i]); 3525 3526 txpower.rates[i].rate = wpi_ridx_to_plcp[i]; 3527 3528 if (IEEE80211_IS_CHAN_5GHZ(c)) { 3529 txpower.rates[i].gain_radio = wpi_rf_gain_5ghz[idx]; 3530 txpower.rates[i].gain_dsp = wpi_dsp_gain_5ghz[idx]; 3531 } else { 3532 txpower.rates[i].gain_radio = wpi_rf_gain_2ghz[idx]; 3533 txpower.rates[i].gain_dsp = wpi_dsp_gain_2ghz[idx]; 3534 } 3535 DPRINTFN(WPI_DEBUG_TEMP,("chan %d/rate %d: power index %d\n", 3536 chan, wpi_ridx_to_rate[i], idx)); 3537 } 3538 3539 return wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, async); 3540} 3541 3542/* 3543 * Determine Tx power index for a given channel/rate combination. 3544 * This takes into account the regulatory information from EEPROM and the 3545 * current temperature. 3546 */ 3547static int 3548wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group, 3549 struct ieee80211_channel *c, int rate) 3550{ 3551/* fixed-point arithmetic division using a n-bit fractional part */ 3552#define fdivround(a, b, n) \ 3553 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n)) 3554 3555/* linear interpolation */ 3556#define interpolate(x, x1, y1, x2, y2, n) \ 3557 ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n)) 3558 3559 struct ieee80211com *ic = &sc->sc_ic; 3560 struct wpi_power_sample *sample; 3561 int pwr, idx; 3562 u_int chan; 3563 3564 /* get channel number */ 3565 chan = ieee80211_chan2ieee(ic, c); 3566 3567 /* default power is group's maximum power - 3dB */ 3568 pwr = group->maxpwr / 2; 3569 3570 /* decrease power for highest OFDM rates to reduce distortion */ 3571 switch (rate) { 3572 case 72: /* 36Mb/s */ 3573 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 : 5; 3574 break; 3575 case 96: /* 48Mb/s */ 3576 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10; 3577 break; 3578 case 108: /* 54Mb/s */ 3579 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12; 3580 break; 3581 } 3582 3583 /* never exceed channel's maximum allowed Tx power */ 3584 pwr = min(pwr, sc->maxpwr[chan]); 3585 3586 /* retrieve power index into gain tables from samples */ 3587 for (sample = group->samples; sample < &group->samples[3]; sample++) 3588 if (pwr > sample[1].power) 3589 break; 3590 /* fixed-point linear interpolation using a 19-bit fractional part */ 3591 idx = interpolate(pwr, sample[0].power, sample[0].index, 3592 sample[1].power, sample[1].index, 19); 3593 3594 /* 3595 * Adjust power index based on current temperature 3596 * - if colder than factory-calibrated: decreate output power 3597 * - if warmer than factory-calibrated: increase output power 3598 */ 3599 idx -= (sc->temp - group->temp) * 11 / 100; 3600 3601 /* decrease power for CCK rates (-5dB) */ 3602 if (!WPI_RATE_IS_OFDM(rate)) 3603 idx += 10; 3604 3605 /* keep power index in a valid range */ 3606 if (idx < 0) 3607 return 0; 3608 if (idx > WPI_MAX_PWR_INDEX) 3609 return WPI_MAX_PWR_INDEX; 3610 return idx; 3611 3612#undef interpolate 3613#undef fdivround 3614} 3615 3616#if 0 3617static void 3618wpi_radio_on(void *arg, int pending) 3619{ 3620 struct wpi_softc *sc = arg; 3621 3622 device_printf(sc->sc_dev, "radio turned on\n"); 3623} 3624 3625static void 3626wpi_radio_off(void *arg, int pending) 3627{ 3628 struct wpi_softc *sc = arg; 3629 3630 device_printf(sc->sc_dev, "radio turned off\n"); 3631} 3632#endif 3633 3634/** 3635 * Called by net80211 framework to indicate that a scan 3636 * is starting. This function doesn't actually do the scan, 3637 * wpi_scan_curchan starts things off. This function is more 3638 * of an early warning from the framework we should get ready 3639 * for the scan. 3640 */ 3641static void 3642wpi_scan_start(struct ieee80211com *ic) 3643{ 3644 struct ifnet *ifp = ic->ic_ifp; 3645 struct wpi_softc *sc = ifp->if_softc; 3646 3647 wpi_queue_cmd(sc, WPI_SCAN_START); 3648} 3649 3650/** 3651 * Called by the net80211 framework, indicates that the 3652 * scan has ended. If there is a scan in progress on the card 3653 * then it should be aborted. 3654 */ 3655static void 3656wpi_scan_end(struct ieee80211com *ic) 3657{ 3658 struct ifnet *ifp = ic->ic_ifp; 3659 struct wpi_softc *sc = ifp->if_softc; 3660 3661 wpi_queue_cmd(sc, WPI_SCAN_STOP); 3662} 3663 3664/** 3665 * Called by the net80211 framework to indicate to the driver 3666 * that the channel should be changed 3667 */ 3668static void 3669wpi_set_channel(struct ieee80211com *ic) 3670{ 3671 struct ifnet *ifp = ic->ic_ifp; 3672 struct wpi_softc *sc = ifp->if_softc; 3673 3674 wpi_queue_cmd(sc, WPI_SET_CHAN); 3675} 3676 3677/** 3678 * Called by net80211 to indicate that we need to scan the current 3679 * channel. The channel is previously be set via the wpi_set_channel 3680 * callback. 3681 */ 3682static void 3683wpi_scan_curchan(struct ieee80211com *ic, unsigned long maxdwell) 3684{ 3685 struct ifnet *ifp = ic->ic_ifp; 3686 struct wpi_softc *sc = ifp->if_softc; 3687 3688 sc->maxdwell = maxdwell; 3689 3690 wpi_queue_cmd(sc, WPI_SCAN_CURCHAN); 3691} 3692 3693/** 3694 * Called by the net80211 framework to indicate 3695 * the minimum dwell time has been met, terminate the scan. 3696 * We don't actually terminate the scan as the firmware will notify 3697 * us when it's finished and we have no way to interrupt it. 3698 */ 3699static void 3700wpi_scan_mindwell(struct ieee80211com *ic) 3701{ 3702 /* NB: don't try to abort scan; wait for firmware to finish */ 3703} 3704 3705/** 3706 * The ops function is called to perform some actual work. 3707 * because we can't sleep from any of the ic callbacks, we queue an 3708 * op task with wpi_queue_cmd and have the taskqueue process that task. 3709 * The task that gets cued is a op task, which ends up calling this function. 3710 */ 3711static void 3712wpi_ops(void *arg, int pending) 3713{ 3714 struct wpi_softc *sc = arg; 3715 struct ieee80211com *ic = &sc->sc_ic; 3716 WPI_LOCK_DECL; 3717 int cmd; 3718 3719again: 3720 WPI_CMD_LOCK(sc); 3721 cmd = sc->sc_cmd[sc->sc_cmd_cur]; 3722 3723 if (cmd == 0) { 3724 /* No more commands to process */ 3725 WPI_CMD_UNLOCK(sc); 3726 return; 3727 } 3728 sc->sc_cmd[sc->sc_cmd_cur] = 0; /* free the slot */ 3729 sc->sc_cmd_cur = (sc->sc_cmd_cur + 1) % WPI_CMD_MAXOPS; 3730 WPI_CMD_UNLOCK(sc); 3731 WPI_LOCK(sc); 3732 3733 if (!(sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) { 3734 WPI_UNLOCK(sc); 3735 return; 3736 } 3737 3738 { 3739 const char *name[]={"SCAN_START", "SCAN_CURCHAN",0,"STOP",0,0,0,"CHAN", 3740 0,0,0,0,0,0,"AUTH",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"NEXT"}; 3741 DPRINTFN(WPI_DEBUG_OPS,("wpi_ops: command: %d %s\n", cmd, name[cmd-1])); 3742 } 3743 3744 switch (cmd) { 3745 case WPI_SCAN_START: 3746 if (sc->flags & WPI_FLAG_HW_RADIO_OFF) { 3747 DPRINTF(("HERER\n")); 3748 ieee80211_cancel_scan(ic); 3749 } else 3750 sc->flags |= WPI_FLAG_SCANNING; 3751 break; 3752 3753 case WPI_SCAN_STOP: 3754 sc->flags &= ~WPI_FLAG_SCANNING; 3755 break; 3756 3757 case WPI_SCAN_NEXT: 3758 DPRINTF(("NEXT\n")); 3759 WPI_UNLOCK(sc); 3760 ieee80211_scan_next(ic); 3761 WPI_LOCK(sc); 3762 break; 3763 3764 case WPI_SCAN_CURCHAN: 3765 if (wpi_scan(sc)) 3766 ieee80211_cancel_scan(ic); 3767 break; 3768 3769 case WPI_SET_CHAN: 3770 if (sc->flags&WPI_FLAG_AUTH) { 3771 DPRINTF(("Authenticating, not changing channel\n")); 3772 break; 3773 } 3774 if (wpi_config(sc)) { 3775 DPRINTF(("Scan cancelled\n")); 3776 WPI_UNLOCK(sc); 3777 ieee80211_cancel_scan(ic); 3778 WPI_LOCK(sc); 3779 sc->flags &= ~WPI_FLAG_SCANNING; 3780 wpi_restart(sc,0); 3781 WPI_UNLOCK(sc); 3782 return; 3783 } 3784 break; 3785 3786 case WPI_AUTH: 3787 if (wpi_auth(sc) != 0) { 3788 device_printf(sc->sc_dev, 3789 "could not send authentication request\n"); 3790 wpi_stop_locked(sc); 3791 WPI_UNLOCK(sc); 3792 return; 3793 } 3794 WPI_UNLOCK(sc); 3795 ieee80211_node_authorize(ic->ic_bss); 3796 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1); 3797 WPI_LOCK(sc); 3798 break; 3799 } 3800 WPI_UNLOCK(sc); 3801 3802 /* Take another pass */ 3803 goto again; 3804} 3805 3806/** 3807 * queue a command for later execution in a different thread. 3808 * This is needed as the net80211 callbacks do not allow 3809 * sleeping, since we need to sleep to confirm commands have 3810 * been processed by the firmware, we must defer execution to 3811 * a sleep enabled thread. 3812 */ 3813static int 3814wpi_queue_cmd(struct wpi_softc *sc, int cmd) 3815{ 3816 WPI_CMD_LOCK(sc); 3817 3818 if (sc->sc_cmd[sc->sc_cmd_next] != 0) { 3819 WPI_CMD_UNLOCK(sc); 3820 DPRINTF(("%s: command %d dropped\n", __func__, cmd)); 3821 return (EBUSY); 3822 } 3823 3824 sc->sc_cmd[sc->sc_cmd_next] = cmd; 3825 sc->sc_cmd_next = (sc->sc_cmd_next + 1) % WPI_CMD_MAXOPS; 3826 3827 taskqueue_enqueue(sc->sc_tq, &sc->sc_opstask); 3828 3829 WPI_CMD_UNLOCK(sc); 3830 3831 return 0; 3832} 3833 3834static void 3835wpi_restart(void * arg, int pending) 3836{ 3837#if 0 3838 struct wpi_softc *sc = arg; 3839 struct ieee80211com *ic = &sc->sc_ic; 3840 WPI_LOCK_DECL; 3841 3842 DPRINTF(("Device failed, restarting device\n")); 3843 WPI_LOCK(sc); 3844 wpi_stop(sc); 3845 wpi_init(sc); 3846 WPI_UNLOCK(sc); 3847 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 3848#endif 3849} 3850 3851/* 3852 * Allocate DMA-safe memory for firmware transfer. 3853 */ 3854static int 3855wpi_alloc_fwmem(struct wpi_softc *sc) 3856{ 3857 /* allocate enough contiguous space to store text and data */ 3858 return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL, 3859 WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ, 1, 3860 BUS_DMA_NOWAIT); 3861} 3862 3863static void 3864wpi_free_fwmem(struct wpi_softc *sc) 3865{ 3866 wpi_dma_contig_free(&sc->fw_dma); 3867} 3868 3869/** 3870 * Called every second, wpi_tick used by the watch dog timer 3871 * to check that the card is still alive 3872 */ 3873static void 3874wpi_tick(void *arg) 3875{ 3876 struct wpi_softc *sc = arg; 3877 3878 DPRINTFN(WPI_DEBUG_WATCHDOG,("Watchdog: tick\n")); 3879 3880 wpi_watchdog(sc->sc_ifp); 3881 callout_reset(&sc->watchdog_to, hz, wpi_tick, sc); 3882} 3883 3884#ifdef WPI_DEBUG 3885static const char *wpi_cmd_str(int cmd) 3886{ 3887 switch(cmd) { 3888 case WPI_DISABLE_CMD: return "WPI_DISABLE_CMD"; 3889 case WPI_CMD_CONFIGURE: return "WPI_CMD_CONFIGURE"; 3890 case WPI_CMD_ASSOCIATE: return "WPI_CMD_ASSOCIATE"; 3891 case WPI_CMD_SET_WME: return "WPI_CMD_SET_WME"; 3892 case WPI_CMD_TSF: return "WPI_CMD_TSF"; 3893 case WPI_CMD_ADD_NODE: return "WPI_CMD_ADD_NODE"; 3894 case WPI_CMD_TX_DATA: return "WPI_CMD_TX_DATA"; 3895 case WPI_CMD_MRR_SETUP: return "WPI_CMD_MRR_SETUP"; 3896 case WPI_CMD_SET_LED: return "WPI_CMD_SET_LED"; 3897 case WPI_CMD_SET_POWER_MODE: return "WPI_CMD_SET_POWER_MODE"; 3898 case WPI_CMD_SCAN: return "WPI_CMD_SCAN"; 3899 case WPI_CMD_SET_BEACON:return "WPI_CMD_SET_BEACON"; 3900 case WPI_CMD_TXPOWER: return "WPI_CMD_TXPOWER"; 3901 case WPI_CMD_BLUETOOTH: return "WPI_CMD_BLUETOOTH"; 3902 3903 default: 3904 KASSERT(1, ("Unknown Command: %d\n", cmd)); 3905 return "UNKNOWN CMD"; // Make the compiler happy 3906 } 3907} 3908#endif 3909 3910MODULE_DEPEND(wpi, pci, 1, 1, 1); 3911MODULE_DEPEND(wpi, wlan, 1, 1, 1); 3912MODULE_DEPEND(wpi, firmware, 1, 1, 1); 3913MODULE_DEPEND(wpi, wlan_amrr, 1, 1, 1); 3914