if_wpi.c revision 173585
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 173585 2007-11-13 16:12:59Z rink $"); 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 /* XXX beacon miss handling? */ 1804 switch (desc->type) { 1805 case WPI_RX_DONE: 1806 /* a 802.11 frame was received */ 1807 wpi_rx_intr(sc, desc, data); 1808 break; 1809 1810 case WPI_TX_DONE: 1811 /* a 802.11 frame has been transmitted */ 1812 wpi_tx_intr(sc, desc); 1813 break; 1814 1815 case WPI_UC_READY: 1816 { 1817 struct wpi_ucode_info *uc = 1818 (struct wpi_ucode_info *)(desc + 1); 1819 1820 /* the microcontroller is ready */ 1821 DPRINTF(("microcode alive notification version %x " 1822 "alive %x\n", le32toh(uc->version), 1823 le32toh(uc->valid))); 1824 1825 if (le32toh(uc->valid) != 1) { 1826 device_printf(sc->sc_dev, 1827 "microcontroller initialization failed\n"); 1828 wpi_stop_locked(sc); 1829 } 1830 break; 1831 } 1832 case WPI_STATE_CHANGED: 1833 { 1834 uint32_t *status = (uint32_t *)(desc + 1); 1835 1836 /* enabled/disabled notification */ 1837 DPRINTF(("state changed to %x\n", le32toh(*status))); 1838 1839 if (le32toh(*status) & 1) { 1840 device_printf(sc->sc_dev, 1841 "Radio transmitter is switched off\n"); 1842 sc->flags |= WPI_FLAG_HW_RADIO_OFF; 1843 break; 1844 } 1845 sc->flags &= ~WPI_FLAG_HW_RADIO_OFF; 1846 break; 1847 } 1848 case WPI_START_SCAN: 1849 { 1850 struct wpi_start_scan *scan = 1851 (struct wpi_start_scan *)(desc + 1); 1852 1853 DPRINTFN(WPI_DEBUG_SCANNING, 1854 ("scanning channel %d status %x\n", 1855 scan->chan, le32toh(scan->status))); 1856 1857 /* fix current channel */ 1858 ic->ic_bss->ni_chan = &ic->ic_channels[scan->chan]; 1859 break; 1860 } 1861 case WPI_STOP_SCAN: 1862 { 1863 struct wpi_stop_scan *scan = 1864 (struct wpi_stop_scan *)(desc + 1); 1865 1866 DPRINTFN(WPI_DEBUG_SCANNING, 1867 ("scan finished nchan=%d status=%d chan=%d\n", 1868 scan->nchan, scan->status, scan->chan)); 1869 1870 wpi_queue_cmd(sc, WPI_SCAN_NEXT); 1871 break; 1872 } 1873 } 1874 1875 sc->rxq.cur = (sc->rxq.cur + 1) % WPI_RX_RING_COUNT; 1876 } 1877 1878 /* tell the firmware what we have processed */ 1879 hw = (hw == 0) ? WPI_RX_RING_COUNT - 1 : hw - 1; 1880 WPI_WRITE(sc, WPI_RX_WIDX, hw & ~7); 1881 1882} 1883 1884static void 1885wpi_intr(void *arg) 1886{ 1887 struct wpi_softc *sc = arg; 1888 uint32_t r; 1889 WPI_LOCK_DECL; 1890 1891 WPI_LOCK(sc); 1892 1893 r = WPI_READ(sc, WPI_INTR); 1894 if (r == 0 || r == 0xffffffff) { 1895 WPI_UNLOCK(sc); 1896 return; 1897 } 1898 1899 /* disable interrupts */ 1900 WPI_WRITE(sc, WPI_MASK, 0); 1901 /* ack interrupts */ 1902 WPI_WRITE(sc, WPI_INTR, r); 1903 1904 if (r & (WPI_SW_ERROR | WPI_HW_ERROR)) { 1905 device_printf(sc->sc_dev, "fatal firmware error\n"); 1906 DPRINTFN(6,("(%s)\n", (r & WPI_SW_ERROR) ? "(Software Error)" : 1907 "(Hardware Error)")); 1908 taskqueue_enqueue(sc->sc_tq2, &sc->sc_restarttask); 1909 sc->flags &= ~WPI_FLAG_BUSY; 1910 WPI_UNLOCK(sc); 1911 return; 1912 } 1913 1914 if (r & WPI_RX_INTR) 1915 wpi_notif_intr(sc); 1916 1917 if (r & WPI_ALIVE_INTR) /* firmware initialized */ 1918 wakeup(sc); 1919 1920 /* re-enable interrupts */ 1921 if (sc->sc_ifp->if_flags & IFF_UP) 1922 WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK); 1923 1924 WPI_UNLOCK(sc); 1925} 1926 1927static uint8_t 1928wpi_plcp_signal(int rate) 1929{ 1930 switch (rate) { 1931 /* CCK rates (returned values are device-dependent) */ 1932 case 2: return 10; 1933 case 4: return 20; 1934 case 11: return 55; 1935 case 22: return 110; 1936 1937 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1938 /* R1-R4 (ral/ural is R4-R1) */ 1939 case 12: return 0xd; 1940 case 18: return 0xf; 1941 case 24: return 0x5; 1942 case 36: return 0x7; 1943 case 48: return 0x9; 1944 case 72: return 0xb; 1945 case 96: return 0x1; 1946 case 108: return 0x3; 1947 1948 /* unsupported rates (should not get there) */ 1949 default: return 0; 1950 } 1951} 1952 1953/* quickly determine if a given rate is CCK or OFDM */ 1954#define WPI_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22) 1955 1956/* 1957 * Construct the data packet for a transmit buffer and acutally put 1958 * the buffer onto the transmit ring, kicking the card to process the 1959 * the buffer. 1960 */ 1961static int 1962wpi_tx_data(struct wpi_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1963 int ac) 1964{ 1965 struct ieee80211com *ic = &sc->sc_ic; 1966 struct wpi_tx_ring *ring = &sc->txq[ac]; 1967 struct wpi_tx_desc *desc; 1968 struct wpi_tx_data *data; 1969 struct wpi_tx_cmd *cmd; 1970 struct wpi_cmd_data *tx; 1971 struct ieee80211_frame *wh; 1972 struct ieee80211_key *k; 1973 const struct chanAccParams *cap; 1974 struct mbuf *mnew; 1975 int i, error, nsegs, rate, hdrlen, noack = 0; 1976 bus_dma_segment_t segs[WPI_MAX_SCATTER]; 1977 1978 desc = &ring->desc[ring->cur]; 1979 data = &ring->data[ring->cur]; 1980 1981 wh = mtod(m0, struct ieee80211_frame *); 1982 1983 if (IEEE80211_QOS_HAS_SEQ(wh)) { 1984 hdrlen = sizeof (struct ieee80211_qosframe); 1985 cap = &ic->ic_wme.wme_chanParams; 1986 noack = cap->cap_wmeParams[ac].wmep_noackPolicy; 1987 } else 1988 hdrlen = sizeof (struct ieee80211_frame); 1989 1990 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1991 if ((k = ieee80211_crypto_encap(ic, ni, m0)) == NULL) { 1992 m_freem(m0); 1993 return ENOBUFS; 1994 } 1995 1996 /* packet header may have moved, reset our local pointer */ 1997 wh = mtod(m0, struct ieee80211_frame *); 1998 } 1999 2000 /* pickup a rate */ 2001 if (IEEE80211_IS_MULTICAST(wh->i_addr1)|| 2002 ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 2003 IEEE80211_FC0_TYPE_MGT)) { 2004 /* 2005 * mgmt/multicast frames are sent at the lowest available 2006 * bit-rate 2007 */ 2008 rate = ni->ni_rates.rs_rates[0]; 2009 } else { 2010 if (ic->ic_fixed_rate != -1) { 2011 rate = ic->ic_sup_rates[ic->ic_curmode]. 2012 rs_rates[ic->ic_fixed_rate]; 2013 } else 2014 rate = ni->ni_rates.rs_rates[ni->ni_txrate]; 2015 } 2016 rate &= IEEE80211_RATE_VAL; 2017 2018#ifndef WPI_CURRENT 2019 if (sc->sc_drvbpf != NULL) { 2020#else 2021 if (bpf_peers_present(sc->sc_drvbpf)) { 2022#endif 2023 2024 struct wpi_tx_radiotap_header *tap = &sc->sc_txtap; 2025 2026 tap->wt_flags = 0; 2027 tap->wt_chan_freq = htole16(ni->ni_chan->ic_freq); 2028 tap->wt_chan_flags = htole16(ni->ni_chan->ic_flags); 2029 tap->wt_rate = rate; 2030 tap->wt_hwqueue = ac; 2031 if (wh->i_fc[1] & IEEE80211_FC1_WEP) 2032 tap->wt_flags |= IEEE80211_RADIOTAP_F_WEP; 2033 2034 bpf_mtap2(sc->sc_drvbpf, tap, sc->sc_txtap_len, m0); 2035 } 2036 2037 cmd = &ring->cmd[ring->cur]; 2038 cmd->code = WPI_CMD_TX_DATA; 2039 cmd->flags = 0; 2040 cmd->qid = ring->qid; 2041 cmd->idx = ring->cur; 2042 2043 tx = (struct wpi_cmd_data *)cmd->data; 2044 tx->flags = 0; 2045 2046 if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) { 2047 tx->flags |= htole32(WPI_TX_NEED_ACK); 2048 } else if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > ic->ic_rtsthreshold) { 2049 tx->flags |= htole32(WPI_TX_NEED_RTS | WPI_TX_FULL_TXOP); 2050 } 2051 2052 tx->flags |= htole32(WPI_TX_AUTO_SEQ); 2053 2054 tx->id = IEEE80211_IS_MULTICAST(wh->i_addr1) ? WPI_ID_BROADCAST : 2055 WPI_ID_BSS; 2056 2057 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 2058 IEEE80211_FC0_TYPE_MGT) { 2059 uint8_t subtype = wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK; 2060 /* tell h/w to set timestamp in probe responses */ 2061 if (subtype == IEEE80211_FC0_SUBTYPE_PROBE_RESP) 2062 tx->flags |= htole32(WPI_TX_INSERT_TSTAMP); 2063 2064 if (subtype == IEEE80211_FC0_SUBTYPE_ASSOC_REQ || 2065 subtype == IEEE80211_FC0_SUBTYPE_REASSOC_REQ) 2066 tx->timeout = htole16(3); 2067 else 2068 tx->timeout = htole16(2); 2069 } else 2070 tx->timeout = htole16(0); 2071 2072 tx->rate = wpi_plcp_signal(rate); 2073 2074 /* be very persistant at sending frames out */ 2075 tx->rts_ntries = 7; 2076 tx->data_ntries = 15; 2077 2078 tx->ofdm_mask = 0xff; 2079 tx->cck_mask = 0x0f; 2080 tx->lifetime = htole32(WPI_LIFETIME_INFINITE); 2081 2082 tx->len = htole16(m0->m_pkthdr.len); 2083 2084 /* save and trim IEEE802.11 header */ 2085 m_copydata(m0, 0, hdrlen, (caddr_t)&tx->wh); 2086 m_adj(m0, hdrlen); 2087 2088 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, m0, segs, 2089 &nsegs, BUS_DMA_NOWAIT); 2090 if (error != 0 && error != EFBIG) { 2091 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 2092 error); 2093 m_freem(m0); 2094 return error; 2095 } 2096 if (error != 0) { 2097 /* XXX use ath_defrag */ 2098 mnew = m_defrag(m0, M_DONTWAIT); 2099 if (mnew == NULL) { 2100 device_printf(sc->sc_dev, 2101 "could not defragment mbuf\n"); 2102 m_freem(m0); 2103 return ENOBUFS; 2104 } 2105 m0 = mnew; 2106 2107 error = bus_dmamap_load_mbuf_sg(ring->data_dmat, data->map, 2108 m0, segs, &nsegs, BUS_DMA_NOWAIT); 2109 if (error != 0) { 2110 device_printf(sc->sc_dev, 2111 "could not map mbuf (error %d)\n", error); 2112 m_freem(m0); 2113 return error; 2114 } 2115 } 2116 2117 data->m = m0; 2118 data->ni = ni; 2119 2120 DPRINTFN(WPI_DEBUG_TX, ("sending data: qid=%d idx=%d len=%d nsegs=%d\n", 2121 ring->qid, ring->cur, m0->m_pkthdr.len, nsegs)); 2122 2123 /* first scatter/gather segment is used by the tx data command */ 2124 desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2125 (1 + nsegs) << 24); 2126 desc->segs[0].addr = htole32(ring->cmd_dma.paddr + 2127 ring->cur * sizeof (struct wpi_tx_cmd)); 2128 desc->segs[0].len = htole32(4 + sizeof (struct wpi_cmd_data)); 2129 for (i = 1; i <= nsegs; i++) { 2130 desc->segs[i].addr = htole32(segs[i - 1].ds_addr); 2131 desc->segs[i].len = htole32(segs[i - 1].ds_len); 2132 } 2133 2134 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 2135 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 2136 BUS_DMASYNC_PREWRITE); 2137 2138 ring->queued++; 2139 2140 /* kick ring */ 2141 ring->cur = (ring->cur + 1) % WPI_TX_RING_COUNT; 2142 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2143 2144 return 0; 2145} 2146 2147/** 2148 * Process data waiting to be sent on the IFNET output queue 2149 */ 2150static void 2151wpi_start(struct ifnet *ifp) 2152{ 2153 struct wpi_softc *sc = ifp->if_softc; 2154 struct ieee80211com *ic = &sc->sc_ic; 2155 struct ieee80211_node *ni; 2156 struct ether_header *eh; 2157 struct mbuf *m0; 2158 int ac; 2159 WPI_LOCK_DECL; 2160 2161 WPI_LOCK(sc); 2162 2163 for (;;) { 2164 IF_POLL(&ic->ic_mgtq, m0); 2165 if (m0 != NULL) { 2166 IF_DEQUEUE(&ic->ic_mgtq, m0); 2167 2168 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif; 2169 m0->m_pkthdr.rcvif = NULL; 2170 2171 /* management frames go into ring 0 */ 2172 if (sc->txq[0].queued > sc->txq[0].count - 8) { 2173 ifp->if_oerrors++; 2174 continue; 2175 } 2176 2177 if (wpi_tx_data(sc, m0, ni, 0) != 0) { 2178 ifp->if_oerrors++; 2179 break; 2180 } 2181 } else { 2182 if (ic->ic_state != IEEE80211_S_RUN) 2183 break; 2184 2185 IFQ_POLL(&ifp->if_snd, m0); 2186 if (m0 == NULL) 2187 break; 2188 2189 /* 2190 * Cancel any background scan. 2191 */ 2192 if (ic->ic_flags & IEEE80211_F_SCAN) 2193 ieee80211_cancel_scan(ic); 2194 2195 if (m0->m_len < sizeof (*eh) && 2196 (m0 = m_pullup(m0, sizeof (*eh))) != NULL) { 2197 ifp->if_oerrors++; 2198 continue; 2199 } 2200 eh = mtod(m0, struct ether_header *); 2201 ni = ieee80211_find_txnode(ic, eh->ether_dhost); 2202 if (ni == NULL) { 2203 m_freem(m0); 2204 ifp->if_oerrors++; 2205 continue; 2206 } 2207 2208 /* classify mbuf so we can find which tx ring to use */ 2209 if (ieee80211_classify(ic, m0, ni) != 0) { 2210 m_freem(m0); 2211 ieee80211_free_node(ni); 2212 ifp->if_oerrors++; 2213 continue; 2214 } 2215 2216 /* no QoS encapsulation for EAPOL frames */ 2217 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ? 2218 M_WME_GETAC(m0) : WME_AC_BE; 2219 2220 if (sc->txq[ac].queued > sc->txq[ac].count - 8) { 2221 /* there is no place left in this ring */ 2222 IFQ_DRV_PREPEND(&ifp->if_snd, m0); 2223 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2224 break; 2225 } 2226 2227 IFQ_DRV_DEQUEUE(&ifp->if_snd, m0); 2228 BPF_MTAP(ifp, m0); 2229 2230 m0 = ieee80211_encap(ic, m0, ni); 2231 if (m0 == NULL) { 2232 ieee80211_free_node(ni); 2233 ifp->if_oerrors++; 2234 continue; 2235 } 2236 2237#ifndef WPI_CURRENT 2238 if (ic->ic_rawbpf != NULL) 2239#else 2240 if (bpf_peers_present(ic->ic_rawbpf)) 2241#endif 2242 bpf_mtap(ic->ic_rawbpf, m0); 2243 2244 if (wpi_tx_data(sc, m0, ni, ac) != 0) { 2245 ieee80211_free_node(ni); 2246 ifp->if_oerrors++; 2247 break; 2248 } 2249 } 2250 2251 sc->sc_tx_timer = 5; 2252 sc->watchdog_cnt = 5; 2253 ic->ic_lastdata = ticks; 2254 } 2255 2256 WPI_UNLOCK(sc); 2257} 2258 2259static void 2260wpi_watchdog(struct ifnet *ifp) 2261{ 2262 struct wpi_softc *sc = ifp->if_softc; 2263 WPI_LOCK_DECL; 2264 2265 WPI_LOCK(sc); 2266 2267 DPRINTFN(WPI_DEBUG_WATCHDOG, ("watchdog_cnt: %d\n", sc->watchdog_cnt)); 2268 2269 if (sc->watchdog_cnt == 0 || --sc->watchdog_cnt) 2270 goto done; 2271 2272 if (--sc->sc_tx_timer != 0) { 2273 device_printf(sc->sc_dev,"device timeout\n"); 2274 ifp->if_oerrors++; 2275 taskqueue_enqueue(sc->sc_tq2, &sc->sc_restarttask); 2276 } 2277done: 2278 WPI_UNLOCK(sc); 2279} 2280 2281static int 2282wpi_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2283{ 2284 struct wpi_softc *sc = ifp->if_softc; 2285 struct ieee80211com *ic = &sc->sc_ic; 2286 int error = 0; 2287 WPI_LOCK_DECL; 2288 2289 WPI_LOCK(sc); 2290 2291 switch (cmd) { 2292 case SIOCSIFFLAGS: 2293 if ((ifp->if_flags & IFF_UP)) { 2294 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) 2295 wpi_init(sc); 2296 } else if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2297 wpi_stop_locked(sc); 2298 break; 2299 default: 2300 WPI_UNLOCK(sc); 2301 error = ieee80211_ioctl(ic, cmd, data); 2302 WPI_LOCK(sc); 2303 } 2304 2305 if (error == ENETRESET) { 2306 if ((ifp->if_flags & IFF_UP) && 2307 (ifp->if_drv_flags & IFF_DRV_RUNNING) && 2308 ic->ic_roaming != IEEE80211_ROAMING_MANUAL) 2309 wpi_init(sc); 2310 error = 0; 2311 } 2312 2313 WPI_UNLOCK(sc); 2314 2315 return error; 2316} 2317 2318/* 2319 * Extract various information from EEPROM. 2320 */ 2321static void 2322wpi_read_eeprom(struct wpi_softc *sc) 2323{ 2324 struct ieee80211com *ic = &sc->sc_ic; 2325 int i; 2326 2327 /* read the hardware capabilities, revision and SKU type */ 2328 wpi_read_prom_data(sc, WPI_EEPROM_CAPABILITIES, &sc->cap,1); 2329 wpi_read_prom_data(sc, WPI_EEPROM_REVISION, &sc->rev,2); 2330 wpi_read_prom_data(sc, WPI_EEPROM_TYPE, &sc->type, 1); 2331 2332 /* read the regulatory domain */ 2333 wpi_read_prom_data(sc, WPI_EEPROM_DOMAIN, sc->domain, 4); 2334 2335 /* read in the hw MAC address */ 2336 wpi_read_prom_data(sc, WPI_EEPROM_MAC, ic->ic_myaddr, 6); 2337 2338 /* read the list of authorized channels */ 2339 for (i = 0; i < WPI_CHAN_BANDS_COUNT; i++) 2340 wpi_read_eeprom_channels(sc,i); 2341 2342 /* read the power level calibration info for each group */ 2343 for (i = 0; i < WPI_POWER_GROUPS_COUNT; i++) 2344 wpi_read_eeprom_group(sc,i); 2345} 2346 2347/* 2348 * Send a command to the firmware. 2349 */ 2350static int 2351wpi_cmd(struct wpi_softc *sc, int code, const void *buf, int size, int async) 2352{ 2353 struct wpi_tx_ring *ring = &sc->cmdq; 2354 struct wpi_tx_desc *desc; 2355 struct wpi_tx_cmd *cmd; 2356 2357#ifdef WPI_DEBUG 2358 if (!async) { 2359 WPI_LOCK_ASSERT(sc); 2360 } 2361#endif 2362 2363 DPRINTFN(WPI_DEBUG_CMD,("wpi_cmd %d size %d async %d\n", code, size, 2364 async)); 2365 2366 if (sc->flags & WPI_FLAG_BUSY) { 2367 device_printf(sc->sc_dev, "%s: cmd %d not sent, busy\n", 2368 __func__, code); 2369 return EAGAIN; 2370 } 2371 sc->flags|= WPI_FLAG_BUSY; 2372 2373 KASSERT(size <= sizeof cmd->data, ("command %d too large: %d bytes", 2374 code, size)); 2375 2376 desc = &ring->desc[ring->cur]; 2377 cmd = &ring->cmd[ring->cur]; 2378 2379 cmd->code = code; 2380 cmd->flags = 0; 2381 cmd->qid = ring->qid; 2382 cmd->idx = ring->cur; 2383 memcpy(cmd->data, buf, size); 2384 2385 desc->flags = htole32(WPI_PAD32(size) << 28 | 1 << 24); 2386 desc->segs[0].addr = htole32(ring->cmd_dma.paddr + 2387 ring->cur * sizeof (struct wpi_tx_cmd)); 2388 desc->segs[0].len = htole32(4 + size); 2389 2390 /* kick cmd ring */ 2391 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT; 2392 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2393 2394 if (async) { 2395 sc->flags &= ~ WPI_FLAG_BUSY; 2396 return 0; 2397 } 2398 2399 return msleep(cmd, &sc->sc_mtx, PCATCH, "wpicmd", hz); 2400} 2401 2402static int 2403wpi_wme_update(struct ieee80211com *ic) 2404{ 2405#define WPI_EXP2(v) htole16((1 << (v)) - 1) 2406#define WPI_USEC(v) htole16(IEEE80211_TXOP_TO_US(v)) 2407 struct wpi_softc *sc = ic->ic_ifp->if_softc; 2408 const struct wmeParams *wmep; 2409 struct wpi_wme_setup wme; 2410 int ac; 2411 2412 /* don't override default WME values if WME is not actually enabled */ 2413 if (!(ic->ic_flags & IEEE80211_F_WME)) 2414 return 0; 2415 2416 wme.flags = 0; 2417 for (ac = 0; ac < WME_NUM_AC; ac++) { 2418 wmep = &ic->ic_wme.wme_chanParams.cap_wmeParams[ac]; 2419 wme.ac[ac].aifsn = wmep->wmep_aifsn; 2420 wme.ac[ac].cwmin = WPI_EXP2(wmep->wmep_logcwmin); 2421 wme.ac[ac].cwmax = WPI_EXP2(wmep->wmep_logcwmax); 2422 wme.ac[ac].txop = WPI_USEC(wmep->wmep_txopLimit); 2423 2424 DPRINTF(("setting WME for queue %d aifsn=%d cwmin=%d cwmax=%d " 2425 "txop=%d\n", ac, wme.ac[ac].aifsn, wme.ac[ac].cwmin, 2426 wme.ac[ac].cwmax, wme.ac[ac].txop)); 2427 } 2428 2429 return wpi_cmd(sc, WPI_CMD_SET_WME, &wme, sizeof wme, 1); 2430#undef WPI_USEC 2431#undef WPI_EXP2 2432} 2433 2434/* 2435 * Configure h/w multi-rate retries. 2436 */ 2437static int 2438wpi_mrr_setup(struct wpi_softc *sc) 2439{ 2440 struct ieee80211com *ic = &sc->sc_ic; 2441 struct wpi_mrr_setup mrr; 2442 int i, error; 2443 2444 memset(&mrr, 0, sizeof (struct wpi_mrr_setup)); 2445 2446 /* CCK rates (not used with 802.11a) */ 2447 for (i = WPI_CCK1; i <= WPI_CCK11; i++) { 2448 mrr.rates[i].flags = 0; 2449 mrr.rates[i].signal = wpi_ridx_to_plcp[i]; 2450 /* fallback to the immediate lower CCK rate (if any) */ 2451 mrr.rates[i].next = (i == WPI_CCK1) ? WPI_CCK1 : i - 1; 2452 /* try one time at this rate before falling back to "next" */ 2453 mrr.rates[i].ntries = 1; 2454 } 2455 2456 /* OFDM rates (not used with 802.11b) */ 2457 for (i = WPI_OFDM6; i <= WPI_OFDM54; i++) { 2458 mrr.rates[i].flags = 0; 2459 mrr.rates[i].signal = wpi_ridx_to_plcp[i]; 2460 /* fallback to the immediate lower OFDM rate (if any) */ 2461 /* we allow fallback from OFDM/6 to CCK/2 in 11b/g mode */ 2462 mrr.rates[i].next = (i == WPI_OFDM6) ? 2463 ((ic->ic_curmode == IEEE80211_MODE_11A) ? 2464 WPI_OFDM6 : WPI_CCK2) : 2465 i - 1; 2466 /* try one time at this rate before falling back to "next" */ 2467 mrr.rates[i].ntries = 1; 2468 } 2469 2470 /* setup MRR for control frames */ 2471 mrr.which = htole32(WPI_MRR_CTL); 2472 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0); 2473 if (error != 0) { 2474 device_printf(sc->sc_dev, 2475 "could not setup MRR for control frames\n"); 2476 return error; 2477 } 2478 2479 /* setup MRR for data frames */ 2480 mrr.which = htole32(WPI_MRR_DATA); 2481 error = wpi_cmd(sc, WPI_CMD_MRR_SETUP, &mrr, sizeof mrr, 0); 2482 if (error != 0) { 2483 device_printf(sc->sc_dev, 2484 "could not setup MRR for data frames\n"); 2485 return error; 2486 } 2487 2488 return 0; 2489} 2490 2491static void 2492wpi_set_led(struct wpi_softc *sc, uint8_t which, uint8_t off, uint8_t on) 2493{ 2494 struct wpi_cmd_led led; 2495 2496 led.which = which; 2497 led.unit = htole32(100000); /* on/off in unit of 100ms */ 2498 led.off = off; 2499 led.on = on; 2500 2501 (void)wpi_cmd(sc, WPI_CMD_SET_LED, &led, sizeof led, 1); 2502} 2503 2504static void 2505wpi_enable_tsf(struct wpi_softc *sc, struct ieee80211_node *ni) 2506{ 2507 struct wpi_cmd_tsf tsf; 2508 uint64_t val, mod; 2509 2510 memset(&tsf, 0, sizeof tsf); 2511 memcpy(&tsf.tstamp, ni->ni_tstamp.data, 8); 2512 tsf.bintval = htole16(ni->ni_intval); 2513 tsf.lintval = htole16(10); 2514 2515 /* compute remaining time until next beacon */ 2516 val = (uint64_t)ni->ni_intval * 1024; /* msec -> usec */ 2517 mod = le64toh(tsf.tstamp) % val; 2518 tsf.binitval = htole32((uint32_t)(val - mod)); 2519 2520 if (wpi_cmd(sc, WPI_CMD_TSF, &tsf, sizeof tsf, 1) != 0) 2521 device_printf(sc->sc_dev, "could not enable TSF\n"); 2522} 2523 2524#if 0 2525/* 2526 * Build a beacon frame that the firmware will broadcast periodically in 2527 * IBSS or HostAP modes. 2528 */ 2529static int 2530wpi_setup_beacon(struct wpi_softc *sc, struct ieee80211_node *ni) 2531{ 2532 struct ieee80211com *ic = &sc->sc_ic; 2533 struct wpi_tx_ring *ring = &sc->cmdq; 2534 struct wpi_tx_desc *desc; 2535 struct wpi_tx_data *data; 2536 struct wpi_tx_cmd *cmd; 2537 struct wpi_cmd_beacon *bcn; 2538 struct ieee80211_beacon_offsets bo; 2539 struct mbuf *m0; 2540 bus_addr_t physaddr; 2541 int error; 2542 2543 desc = &ring->desc[ring->cur]; 2544 data = &ring->data[ring->cur]; 2545 2546 m0 = ieee80211_beacon_alloc(ic, ni, &bo); 2547 if (m0 == NULL) { 2548 device_printf(sc->sc_dev, "could not allocate beacon frame\n"); 2549 return ENOMEM; 2550 } 2551 2552 cmd = &ring->cmd[ring->cur]; 2553 cmd->code = WPI_CMD_SET_BEACON; 2554 cmd->flags = 0; 2555 cmd->qid = ring->qid; 2556 cmd->idx = ring->cur; 2557 2558 bcn = (struct wpi_cmd_beacon *)cmd->data; 2559 memset(bcn, 0, sizeof (struct wpi_cmd_beacon)); 2560 bcn->id = WPI_ID_BROADCAST; 2561 bcn->ofdm_mask = 0xff; 2562 bcn->cck_mask = 0x0f; 2563 bcn->lifetime = htole32(WPI_LIFETIME_INFINITE); 2564 bcn->len = htole16(m0->m_pkthdr.len); 2565 bcn->rate = (ic->ic_curmode == IEEE80211_MODE_11A) ? 2566 wpi_plcp_signal(12) : wpi_plcp_signal(2); 2567 bcn->flags = htole32(WPI_TX_AUTO_SEQ | WPI_TX_INSERT_TSTAMP); 2568 2569 /* save and trim IEEE802.11 header */ 2570 m_copydata(m0, 0, sizeof (struct ieee80211_frame), (caddr_t)&bcn->wh); 2571 m_adj(m0, sizeof (struct ieee80211_frame)); 2572 2573 /* assume beacon frame is contiguous */ 2574 error = bus_dmamap_load(ring->data_dmat, data->map, mtod(m0, void *), 2575 m0->m_pkthdr.len, wpi_dma_map_addr, &physaddr, 0); 2576 if (error != 0) { 2577 device_printf(sc->sc_dev, "could not map beacon\n"); 2578 m_freem(m0); 2579 return error; 2580 } 2581 2582 data->m = m0; 2583 2584 /* first scatter/gather segment is used by the beacon command */ 2585 desc->flags = htole32(WPI_PAD32(m0->m_pkthdr.len) << 28 | 2 << 24); 2586 desc->segs[0].addr = htole32(ring->cmd_dma.paddr + 2587 ring->cur * sizeof (struct wpi_tx_cmd)); 2588 desc->segs[0].len = htole32(4 + sizeof (struct wpi_cmd_beacon)); 2589 desc->segs[1].addr = htole32(physaddr); 2590 desc->segs[1].len = htole32(m0->m_pkthdr.len); 2591 2592 /* kick cmd ring */ 2593 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT; 2594 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2595 2596 return 0; 2597} 2598#endif 2599 2600static int 2601wpi_auth(struct wpi_softc *sc) 2602{ 2603 struct ieee80211com *ic = &sc->sc_ic; 2604 struct ieee80211_node *ni = ic->ic_bss; 2605 struct wpi_node_info node; 2606 int error; 2607 2608 /* update adapter's configuration */ 2609 IEEE80211_ADDR_COPY(sc->config.bssid, ni->ni_bssid); 2610 sc->config.chan = ieee80211_chan2ieee(ic, ni->ni_chan); 2611 if (IEEE80211_IS_CHAN_2GHZ(ni->ni_chan)) { 2612 sc->config.flags |= htole32(WPI_CONFIG_AUTO | 2613 WPI_CONFIG_24GHZ); 2614 } 2615 switch (ic->ic_curmode) { 2616 case IEEE80211_MODE_11A: 2617 sc->config.cck_mask = 0; 2618 sc->config.ofdm_mask = 0x15; 2619 break; 2620 case IEEE80211_MODE_11B: 2621 sc->config.cck_mask = 0x03; 2622 sc->config.ofdm_mask = 0; 2623 break; 2624 default: /* assume 802.11b/g */ 2625 sc->config.cck_mask = 0x0f; 2626 sc->config.ofdm_mask = 0x15; 2627 } 2628 2629 DPRINTF(("config chan %d flags %x cck %x ofdm %x\n", sc->config.chan, 2630 sc->config.flags, sc->config.cck_mask, sc->config.ofdm_mask)); 2631 error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config, 2632 sizeof (struct wpi_config), 1); 2633 if (error != 0) { 2634 device_printf(sc->sc_dev, "could not configure\n"); 2635 return error; 2636 } 2637 2638 /* configuration has changed, set Tx power accordingly */ 2639 if ((error = wpi_set_txpower(sc, ni->ni_chan, 1)) != 0) { 2640 device_printf(sc->sc_dev, "could not set Tx power\n"); 2641 return error; 2642 } 2643 2644 /* add default node */ 2645 memset(&node, 0, sizeof node); 2646 IEEE80211_ADDR_COPY(node.bssid, ni->ni_bssid); 2647 node.id = WPI_ID_BSS; 2648 node.rate = (ic->ic_curmode == IEEE80211_MODE_11A) ? 2649 wpi_plcp_signal(12) : wpi_plcp_signal(2); 2650 node.action = htole32(WPI_ACTION_SET_RATE); 2651 node.antenna = WPI_ANTENNA_BOTH; 2652 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 1); 2653 if (error != 0) { 2654 device_printf(sc->sc_dev, "could not add BSS node\n"); 2655 return error; 2656 } 2657 2658 sc->flags &= ~WPI_FLAG_AUTH; 2659 2660 return 0; 2661} 2662 2663/* 2664 * Send a scan request to the firmware. Since this command is huge, we map it 2665 * into a mbufcluster instead of using the pre-allocated set of commands. Note, 2666 * much of this code is similar to that in wpi_cmd but because we must manually 2667 * construct the probe & channels, we duplicate what's needed here. XXX In the 2668 * future, this function should be modified to use wpi_cmd to help cleanup the 2669 * code base. 2670 */ 2671static int 2672wpi_scan(struct wpi_softc *sc) 2673{ 2674 struct ieee80211com *ic = &sc->sc_ic; 2675 struct wpi_tx_ring *ring = &sc->cmdq; 2676 struct wpi_tx_desc *desc; 2677 struct wpi_tx_data *data; 2678 struct wpi_tx_cmd *cmd; 2679 struct wpi_scan_hdr *hdr; 2680 struct wpi_scan_chan *chan; 2681 struct ieee80211_frame *wh; 2682 struct ieee80211_rateset *rs; 2683 struct ieee80211_channel *c; 2684 enum ieee80211_phymode mode; 2685 uint8_t *frm; 2686 int nrates, pktlen, error; 2687 bus_addr_t physaddr; 2688 struct ifnet *ifp = ic->ic_ifp; 2689 2690 desc = &ring->desc[ring->cur]; 2691 data = &ring->data[ring->cur]; 2692 2693 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 2694 if (data->m == NULL) { 2695 device_printf(sc->sc_dev, 2696 "could not allocate mbuf for scan command\n"); 2697 return ENOMEM; 2698 } 2699 2700 cmd = mtod(data->m, struct wpi_tx_cmd *); 2701 cmd->code = WPI_CMD_SCAN; 2702 cmd->flags = 0; 2703 cmd->qid = ring->qid; 2704 cmd->idx = ring->cur; 2705 2706 hdr = (struct wpi_scan_hdr *)cmd->data; 2707 memset(hdr, 0, sizeof(struct wpi_scan_hdr)); 2708 2709 /* 2710 * Move to the next channel if no packets are received within 5 msecs 2711 * after sending the probe request (this helps to reduce the duration 2712 * of active scans). 2713 */ 2714 hdr->quiet = htole16(5); 2715 hdr->threshold = htole16(1); 2716 2717 if (IEEE80211_IS_CHAN_A(ic->ic_curchan)) { 2718 /* send probe requests at 6Mbps */ 2719 hdr->tx.rate = wpi_ridx_to_plcp[WPI_OFDM6]; 2720 2721 /* Enable crc checking */ 2722 hdr->promotion = htole16(1); 2723 } else { 2724 hdr->flags = htole32(WPI_CONFIG_24GHZ | WPI_CONFIG_AUTO); 2725 /* send probe requests at 1Mbps */ 2726 hdr->tx.rate = wpi_ridx_to_plcp[WPI_CCK1]; 2727 } 2728 hdr->tx.id = WPI_ID_BROADCAST; 2729 hdr->tx.lifetime = htole32(WPI_LIFETIME_INFINITE); 2730 hdr->tx.flags = htole32(WPI_TX_AUTO_SEQ); 2731 2732 /*XXX Need to cater for multiple essids */ 2733 memset(&hdr->scan_essids[0], 0, 4 * sizeof(hdr->scan_essids[0])); 2734 hdr->scan_essids[0].id = IEEE80211_ELEMID_SSID; 2735 hdr->scan_essids[0].esslen = ic->ic_des_ssid[0].len; 2736 memcpy(hdr->scan_essids[0].essid, ic->ic_des_ssid[0].ssid, 2737 ic->ic_des_ssid[0].len); 2738 2739 if (wpi_debug & WPI_DEBUG_SCANNING) { 2740 printf("Scanning Essid: "); 2741 ieee80211_print_essid(ic->ic_des_ssid[0].ssid, 2742 ic->ic_des_ssid[0].len); 2743 printf("\n"); 2744 } 2745 2746 /* 2747 * Build a probe request frame. Most of the following code is a 2748 * copy & paste of what is done in net80211. 2749 */ 2750 wh = (struct ieee80211_frame *)&hdr->scan_essids[4]; 2751 wh->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_MGT | 2752 IEEE80211_FC0_SUBTYPE_PROBE_REQ; 2753 wh->i_fc[1] = IEEE80211_FC1_DIR_NODS; 2754 IEEE80211_ADDR_COPY(wh->i_addr1, ifp->if_broadcastaddr); 2755 IEEE80211_ADDR_COPY(wh->i_addr2, ic->ic_myaddr); 2756 IEEE80211_ADDR_COPY(wh->i_addr3, ifp->if_broadcastaddr); 2757 *(u_int16_t *)&wh->i_dur[0] = 0; /* filled by h/w */ 2758 *(u_int16_t *)&wh->i_seq[0] = 0; /* filled by h/w */ 2759 2760 frm = (uint8_t *)(wh + 1); 2761 2762 /* add essid IE, the hardware will fill this in for us */ 2763 *frm++ = IEEE80211_ELEMID_SSID; 2764 *frm++ = 0; 2765 2766 mode = ieee80211_chan2mode(ic->ic_curchan); 2767 rs = &ic->ic_sup_rates[mode]; 2768 2769 /* add supported rates IE */ 2770 *frm++ = IEEE80211_ELEMID_RATES; 2771 nrates = rs->rs_nrates; 2772 if (nrates > IEEE80211_RATE_SIZE) 2773 nrates = IEEE80211_RATE_SIZE; 2774 *frm++ = nrates; 2775 memcpy(frm, rs->rs_rates, nrates); 2776 frm += nrates; 2777 2778 /* add supported xrates IE */ 2779 if (rs->rs_nrates > IEEE80211_RATE_SIZE) { 2780 nrates = rs->rs_nrates - IEEE80211_RATE_SIZE; 2781 *frm++ = IEEE80211_ELEMID_XRATES; 2782 *frm++ = nrates; 2783 memcpy(frm, rs->rs_rates + IEEE80211_RATE_SIZE, nrates); 2784 frm += nrates; 2785 } 2786 2787 /* setup length of probe request */ 2788 hdr->tx.len = htole16(frm - (uint8_t *)wh); 2789 2790 /* 2791 * Construct information about the channel that we 2792 * want to scan. The firmware expects this to be directly 2793 * after the scan probe request 2794 */ 2795 c = ic->ic_curchan; 2796 chan = (struct wpi_scan_chan *)frm; 2797 chan->chan = ieee80211_chan2ieee(ic, c); 2798 chan->flags = 0; 2799 if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) { 2800 chan->flags |= WPI_CHAN_ACTIVE; 2801 if (ic->ic_des_ssid[0].len != 0) 2802 chan->flags |= WPI_CHAN_DIRECT; 2803 } 2804 chan->gain_dsp = 0x6e; /* Default level */ 2805 if (IEEE80211_IS_CHAN_5GHZ(c)) { 2806 chan->active = htole16(10); 2807 chan->passive = htole16(sc->maxdwell); 2808 chan->gain_radio = 0x3b; 2809 } else { 2810 chan->active = htole16(20); 2811 chan->passive = htole16(sc->maxdwell); 2812 chan->gain_radio = 0x28; 2813 } 2814 2815 DPRINTFN(WPI_DEBUG_SCANNING, 2816 ("Scanning %u Passive: %d\n", 2817 chan->chan, 2818 c->ic_flags & IEEE80211_CHAN_PASSIVE)); 2819 2820 hdr->nchan++; 2821 chan++; 2822 2823 frm += sizeof (struct wpi_scan_chan); 2824#if 0 2825 // XXX All Channels.... 2826 for (c = &ic->ic_channels[1]; 2827 c <= &ic->ic_channels[IEEE80211_CHAN_MAX]; c++) { 2828 if ((c->ic_flags & ic->ic_curchan->ic_flags) != ic->ic_curchan->ic_flags) 2829 continue; 2830 2831 chan->chan = ieee80211_chan2ieee(ic, c); 2832 chan->flags = 0; 2833 if (!(c->ic_flags & IEEE80211_CHAN_PASSIVE)) { 2834 chan->flags |= WPI_CHAN_ACTIVE; 2835 if (ic->ic_des_ssid[0].len != 0) 2836 chan->flags |= WPI_CHAN_DIRECT; 2837 } 2838 chan->gain_dsp = 0x6e; /* Default level */ 2839 if (IEEE80211_IS_CHAN_5GHZ(c)) { 2840 chan->active = htole16(10); 2841 chan->passive = htole16(110); 2842 chan->gain_radio = 0x3b; 2843 } else { 2844 chan->active = htole16(20); 2845 chan->passive = htole16(120); 2846 chan->gain_radio = 0x28; 2847 } 2848 2849 DPRINTFN(WPI_DEBUG_SCANNING, 2850 ("Scanning %u Passive: %d\n", 2851 chan->chan, 2852 c->ic_flags & IEEE80211_CHAN_PASSIVE)); 2853 2854 hdr->nchan++; 2855 chan++; 2856 2857 frm += sizeof (struct wpi_scan_chan); 2858 } 2859#endif 2860 2861 hdr->len = htole16(frm - (uint8_t *)hdr); 2862 pktlen = frm - (uint8_t *)cmd; 2863 2864 error = bus_dmamap_load(ring->data_dmat, data->map, cmd, pktlen, 2865 wpi_dma_map_addr, &physaddr, BUS_DMA_NOWAIT); 2866 if (error != 0) { 2867 device_printf(sc->sc_dev, "could not map scan command\n"); 2868 m_freem(data->m); 2869 data->m = NULL; 2870 return error; 2871 } 2872 2873 desc->flags = htole32(WPI_PAD32(pktlen) << 28 | 1 << 24); 2874 desc->segs[0].addr = htole32(physaddr); 2875 desc->segs[0].len = htole32(pktlen); 2876 2877 bus_dmamap_sync(ring->desc_dma.tag, ring->desc_dma.map, 2878 BUS_DMASYNC_PREWRITE); 2879 bus_dmamap_sync(ring->data_dmat, data->map, BUS_DMASYNC_PREWRITE); 2880 2881 /* kick cmd ring */ 2882 ring->cur = (ring->cur + 1) % WPI_CMD_RING_COUNT; 2883 WPI_WRITE(sc, WPI_TX_WIDX, ring->qid << 8 | ring->cur); 2884 2885 return 0; /* will be notified async. of failure/success */ 2886} 2887 2888/** 2889 * Configure the card to listen to a particular channel, this transisions the 2890 * card in to being able to receive frames from remote devices. 2891 */ 2892static int 2893wpi_config(struct wpi_softc *sc) 2894{ 2895 struct ieee80211com *ic = &sc->sc_ic; 2896 struct ifnet *ifp = ic->ic_ifp; 2897 struct wpi_power power; 2898 struct wpi_bluetooth bluetooth; 2899 struct wpi_node_info node; 2900 int error; 2901 2902 /* set power mode */ 2903 memset(&power, 0, sizeof power); 2904 power.flags = htole32(WPI_POWER_CAM|0x8); 2905 error = wpi_cmd(sc, WPI_CMD_SET_POWER_MODE, &power, sizeof power, 0); 2906 if (error != 0) { 2907 device_printf(sc->sc_dev, "could not set power mode\n"); 2908 return error; 2909 } 2910 2911 /* configure bluetooth coexistence */ 2912 memset(&bluetooth, 0, sizeof bluetooth); 2913 bluetooth.flags = 3; 2914 bluetooth.lead = 0xaa; 2915 bluetooth.kill = 1; 2916 error = wpi_cmd(sc, WPI_CMD_BLUETOOTH, &bluetooth, sizeof bluetooth, 2917 0); 2918 if (error != 0) { 2919 device_printf(sc->sc_dev, 2920 "could not configure bluetooth coexistence\n"); 2921 return error; 2922 } 2923 2924 /* configure adapter */ 2925 memset(&sc->config, 0, sizeof (struct wpi_config)); 2926 IEEE80211_ADDR_COPY(sc->config.myaddr, ic->ic_myaddr); 2927 /*set default channel*/ 2928 sc->config.chan = htole16(ieee80211_chan2ieee(ic, ic->ic_curchan)); 2929 sc->config.flags = htole32(WPI_CONFIG_TSF); 2930 if (IEEE80211_IS_CHAN_2GHZ(ic->ic_curchan)) { 2931 sc->config.flags |= htole32(WPI_CONFIG_AUTO | 2932 WPI_CONFIG_24GHZ); 2933 } 2934 sc->config.filter = 0; 2935 switch (ic->ic_opmode) { 2936 case IEEE80211_M_STA: 2937 case IEEE80211_M_WDS: /* No know setup, use STA for now */ 2938 sc->config.mode = WPI_MODE_STA; 2939 sc->config.filter |= htole32(WPI_FILTER_MULTICAST); 2940 break; 2941 case IEEE80211_M_IBSS: 2942 case IEEE80211_M_AHDEMO: 2943 sc->config.mode = WPI_MODE_IBSS; 2944 sc->config.filter |= htole32(WPI_FILTER_BEACON | 2945 WPI_FILTER_MULTICAST); 2946 break; 2947 case IEEE80211_M_HOSTAP: 2948 sc->config.mode = WPI_MODE_HOSTAP; 2949 break; 2950 case IEEE80211_M_MONITOR: 2951 sc->config.mode = WPI_MODE_MONITOR; 2952 sc->config.filter |= htole32(WPI_FILTER_MULTICAST | 2953 WPI_FILTER_CTL | WPI_FILTER_PROMISC); 2954 break; 2955 } 2956 sc->config.cck_mask = 0x0f; /* not yet negotiated */ 2957 sc->config.ofdm_mask = 0xff; /* not yet negotiated */ 2958 error = wpi_cmd(sc, WPI_CMD_CONFIGURE, &sc->config, 2959 sizeof (struct wpi_config), 0); 2960 if (error != 0) { 2961 device_printf(sc->sc_dev, "configure command failed\n"); 2962 return error; 2963 } 2964 2965 /* configuration has changed, set Tx power accordingly */ 2966 if ((error = wpi_set_txpower(sc, ic->ic_curchan,0)) != 0) { 2967 device_printf(sc->sc_dev, "could not set Tx power\n"); 2968 return error; 2969 } 2970 2971 /* add broadcast node */ 2972 memset(&node, 0, sizeof node); 2973 IEEE80211_ADDR_COPY(node.bssid, ifp->if_broadcastaddr); 2974 node.id = WPI_ID_BROADCAST; 2975 node.rate = wpi_plcp_signal(2); 2976 error = wpi_cmd(sc, WPI_CMD_ADD_NODE, &node, sizeof node, 0); 2977 if (error != 0) { 2978 device_printf(sc->sc_dev, "could not add broadcast node\n"); 2979 return error; 2980 } 2981 2982 /* Setup rate scalling */ 2983 error = wpi_mrr_setup(sc); 2984 if (error != 0) { 2985 device_printf(sc->sc_dev, "could not setup MRR\n"); 2986 return error; 2987 } 2988 2989 return 0; 2990} 2991 2992static void 2993wpi_stop_master(struct wpi_softc *sc) 2994{ 2995 uint32_t tmp; 2996 int ntries; 2997 2998 DPRINTFN(WPI_DEBUG_HW,("Disabling Firmware execution\n")); 2999 3000 tmp = WPI_READ(sc, WPI_RESET); 3001 WPI_WRITE(sc, WPI_RESET, tmp | WPI_STOP_MASTER | WPI_NEVO_RESET); 3002 3003 tmp = WPI_READ(sc, WPI_GPIO_CTL); 3004 if ((tmp & WPI_GPIO_PWR_STATUS) == WPI_GPIO_PWR_SLEEP) 3005 return; /* already asleep */ 3006 3007 for (ntries = 0; ntries < 100; ntries++) { 3008 if (WPI_READ(sc, WPI_RESET) & WPI_MASTER_DISABLED) 3009 break; 3010 DELAY(10); 3011 } 3012 if (ntries == 100) { 3013 device_printf(sc->sc_dev, "timeout waiting for master\n"); 3014 } 3015} 3016 3017static int 3018wpi_power_up(struct wpi_softc *sc) 3019{ 3020 uint32_t tmp; 3021 int ntries; 3022 3023 wpi_mem_lock(sc); 3024 tmp = wpi_mem_read(sc, WPI_MEM_POWER); 3025 wpi_mem_write(sc, WPI_MEM_POWER, tmp & ~0x03000000); 3026 wpi_mem_unlock(sc); 3027 3028 for (ntries = 0; ntries < 5000; ntries++) { 3029 if (WPI_READ(sc, WPI_GPIO_STATUS) & WPI_POWERED) 3030 break; 3031 DELAY(10); 3032 } 3033 if (ntries == 5000) { 3034 device_printf(sc->sc_dev, 3035 "timeout waiting for NIC to power up\n"); 3036 return ETIMEDOUT; 3037 } 3038 return 0; 3039} 3040 3041static int 3042wpi_reset(struct wpi_softc *sc) 3043{ 3044 uint32_t tmp; 3045 int ntries; 3046 3047 DPRINTFN(WPI_DEBUG_HW, 3048 ("Resetting the card - clearing any uploaded firmware\n")); 3049 3050 /* clear any pending interrupts */ 3051 WPI_WRITE(sc, WPI_INTR, 0xffffffff); 3052 3053 tmp = WPI_READ(sc, WPI_PLL_CTL); 3054 WPI_WRITE(sc, WPI_PLL_CTL, tmp | WPI_PLL_INIT); 3055 3056 tmp = WPI_READ(sc, WPI_CHICKEN); 3057 WPI_WRITE(sc, WPI_CHICKEN, tmp | WPI_CHICKEN_RXNOLOS); 3058 3059 tmp = WPI_READ(sc, WPI_GPIO_CTL); 3060 WPI_WRITE(sc, WPI_GPIO_CTL, tmp | WPI_GPIO_INIT); 3061 3062 /* wait for clock stabilization */ 3063 for (ntries = 0; ntries < 25000; ntries++) { 3064 if (WPI_READ(sc, WPI_GPIO_CTL) & WPI_GPIO_CLOCK) 3065 break; 3066 DELAY(10); 3067 } 3068 if (ntries == 25000) { 3069 device_printf(sc->sc_dev, 3070 "timeout waiting for clock stabilization\n"); 3071 return ETIMEDOUT; 3072 } 3073 3074 /* initialize EEPROM */ 3075 tmp = WPI_READ(sc, WPI_EEPROM_STATUS); 3076 3077 if ((tmp & WPI_EEPROM_VERSION) == 0) { 3078 device_printf(sc->sc_dev, "EEPROM not found\n"); 3079 return EIO; 3080 } 3081 WPI_WRITE(sc, WPI_EEPROM_STATUS, tmp & ~WPI_EEPROM_LOCKED); 3082 3083 return 0; 3084} 3085 3086static void 3087wpi_hw_config(struct wpi_softc *sc) 3088{ 3089 uint32_t rev, hw; 3090 3091 /* voodoo from the Linux "driver".. */ 3092 hw = WPI_READ(sc, WPI_HWCONFIG); 3093 3094 rev = pci_read_config(sc->sc_dev, PCIR_REVID, 1); 3095 if ((rev & 0xc0) == 0x40) 3096 hw |= WPI_HW_ALM_MB; 3097 else if (!(rev & 0x80)) 3098 hw |= WPI_HW_ALM_MM; 3099 3100 if (sc->cap == 0x80) 3101 hw |= WPI_HW_SKU_MRC; 3102 3103 hw &= ~WPI_HW_REV_D; 3104 if ((le16toh(sc->rev) & 0xf0) == 0xd0) 3105 hw |= WPI_HW_REV_D; 3106 3107 if (sc->type > 1) 3108 hw |= WPI_HW_TYPE_B; 3109 3110 WPI_WRITE(sc, WPI_HWCONFIG, hw); 3111} 3112 3113static void 3114wpi_init(void *arg) 3115{ 3116 struct wpi_softc *sc = arg; 3117 struct ieee80211com *ic = &sc->sc_ic; 3118 struct ifnet *ifp = ic->ic_ifp; 3119 uint32_t tmp; 3120 int ntries, error, qid; 3121 WPI_LOCK_DECL; 3122 3123 WPI_LOCK(sc); 3124 3125 wpi_stop_locked(sc); 3126 (void)wpi_reset(sc); 3127 3128 wpi_mem_lock(sc); 3129 wpi_mem_write(sc, WPI_MEM_CLOCK1, 0xa00); 3130 DELAY(20); 3131 tmp = wpi_mem_read(sc, WPI_MEM_PCIDEV); 3132 wpi_mem_write(sc, WPI_MEM_PCIDEV, tmp | 0x800); 3133 wpi_mem_unlock(sc); 3134 3135 (void)wpi_power_up(sc); 3136 wpi_hw_config(sc); 3137 3138 /* init Rx ring */ 3139 wpi_mem_lock(sc); 3140 WPI_WRITE(sc, WPI_RX_BASE, sc->rxq.desc_dma.paddr); 3141 WPI_WRITE(sc, WPI_RX_RIDX_PTR, sc->shared_dma.paddr + 3142 offsetof(struct wpi_shared, next)); 3143 WPI_WRITE(sc, WPI_RX_WIDX, (WPI_RX_RING_COUNT - 1) & ~7); 3144 WPI_WRITE(sc, WPI_RX_CONFIG, 0xa9601010); 3145 wpi_mem_unlock(sc); 3146 3147 /* init Tx rings */ 3148 wpi_mem_lock(sc); 3149 wpi_mem_write(sc, WPI_MEM_MODE, 2); /* bypass mode */ 3150 wpi_mem_write(sc, WPI_MEM_RA, 1); /* enable RA0 */ 3151 wpi_mem_write(sc, WPI_MEM_TXCFG, 0x3f); /* enable all 6 Tx rings */ 3152 wpi_mem_write(sc, WPI_MEM_BYPASS1, 0x10000); 3153 wpi_mem_write(sc, WPI_MEM_BYPASS2, 0x30002); 3154 wpi_mem_write(sc, WPI_MEM_MAGIC4, 4); 3155 wpi_mem_write(sc, WPI_MEM_MAGIC5, 5); 3156 3157 WPI_WRITE(sc, WPI_TX_BASE_PTR, sc->shared_dma.paddr); 3158 WPI_WRITE(sc, WPI_MSG_CONFIG, 0xffff05a5); 3159 3160 for (qid = 0; qid < 6; qid++) { 3161 WPI_WRITE(sc, WPI_TX_CTL(qid), 0); 3162 WPI_WRITE(sc, WPI_TX_BASE(qid), 0); 3163 WPI_WRITE(sc, WPI_TX_CONFIG(qid), 0x80200008); 3164 } 3165 wpi_mem_unlock(sc); 3166 3167 /* clear "radio off" and "disable command" bits (reversed logic) */ 3168 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF); 3169 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_DISABLE_CMD); 3170 sc->flags &= ~WPI_FLAG_HW_RADIO_OFF; 3171 3172 /* clear any pending interrupts */ 3173 WPI_WRITE(sc, WPI_INTR, 0xffffffff); 3174 3175 /* enable interrupts */ 3176 WPI_WRITE(sc, WPI_MASK, WPI_INTR_MASK); 3177 3178 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF); 3179 WPI_WRITE(sc, WPI_UCODE_CLR, WPI_RADIO_OFF); 3180 3181 if ((error = wpi_load_firmware(sc)) != 0) { 3182 device_printf(sc->sc_dev, 3183 "A problem occurred loading the firmware to the driver\n"); 3184 return; 3185 } 3186 3187 /* At this point the firmware is up and running. If the hardware 3188 * RF switch is turned off thermal calibration will fail, though 3189 * the card is still happy to continue to accept commands, catch 3190 * this case and record the hw is disabled. 3191 */ 3192 wpi_mem_lock(sc); 3193 tmp = wpi_mem_read(sc, WPI_MEM_HW_RADIO_OFF); 3194 wpi_mem_unlock(sc); 3195 3196 if (!(tmp & 0x1)) { 3197 sc->flags |= WPI_FLAG_HW_RADIO_OFF; 3198 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3199 ifp->if_drv_flags |= IFF_DRV_RUNNING; 3200 device_printf(sc->sc_dev,"Radio Transmitter is switched off\n"); 3201 return; 3202 } 3203 3204 /* wait for thermal sensors to calibrate */ 3205 for (ntries = 0; ntries < 1000; ntries++) { 3206 if ((sc->temp = (int)WPI_READ(sc, WPI_TEMPERATURE)) != 0) 3207 break; 3208 DELAY(10); 3209 } 3210 3211 if (ntries == 1000) { 3212 device_printf(sc->sc_dev, 3213 "timeout waiting for thermal sensors calibration\n"); 3214 error = ETIMEDOUT; 3215 return; 3216 } 3217 DPRINTFN(WPI_DEBUG_TEMP,("temperature %d\n", sc->temp)); 3218 3219 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 3220 ifp->if_drv_flags |= IFF_DRV_RUNNING; 3221 callout_reset(&sc->watchdog_to, hz, wpi_tick, sc); 3222 WPI_UNLOCK(sc); 3223 3224 if (ic->ic_opmode == IEEE80211_M_MONITOR) 3225 ieee80211_new_state(ic, IEEE80211_S_RUN, -1); 3226 else if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL) 3227 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 3228} 3229 3230static void 3231wpi_stop(struct wpi_softc *sc) 3232{ 3233 WPI_LOCK_DECL; 3234 3235 WPI_LOCK(sc); 3236 wpi_stop_locked(sc); 3237 WPI_UNLOCK(sc); 3238 3239} 3240static void 3241wpi_stop_locked(struct wpi_softc *sc) 3242 3243{ 3244 struct ieee80211com *ic = &sc->sc_ic; 3245 struct ifnet *ifp = ic->ic_ifp; 3246 uint32_t tmp; 3247 int ac; 3248 3249 sc->watchdog_cnt = sc->sc_tx_timer = 0; 3250 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 3251 3252 /* disable interrupts */ 3253 WPI_WRITE(sc, WPI_MASK, 0); 3254 WPI_WRITE(sc, WPI_INTR, WPI_INTR_MASK); 3255 WPI_WRITE(sc, WPI_INTR_STATUS, 0xff); 3256 WPI_WRITE(sc, WPI_INTR_STATUS, 0x00070000); 3257 3258 /* Clear any commands left in the command buffer */ 3259 memset(sc->sc_cmd, 0, sizeof(sc->sc_cmd)); 3260 3261 wpi_mem_lock(sc); 3262 wpi_mem_write(sc, WPI_MEM_MODE, 0); 3263 wpi_mem_unlock(sc); 3264 3265 /* reset all Tx rings */ 3266 for (ac = 0; ac < 4; ac++) 3267 wpi_reset_tx_ring(sc, &sc->txq[ac]); 3268 wpi_reset_tx_ring(sc, &sc->cmdq); 3269 3270 /* reset Rx ring */ 3271 wpi_reset_rx_ring(sc, &sc->rxq); 3272 3273 wpi_mem_lock(sc); 3274 wpi_mem_write(sc, WPI_MEM_CLOCK2, 0x200); 3275 wpi_mem_unlock(sc); 3276 3277 DELAY(5); 3278 3279 wpi_stop_master(sc); 3280 3281 tmp = WPI_READ(sc, WPI_RESET); 3282 WPI_WRITE(sc, WPI_RESET, tmp | WPI_SW_RESET); 3283 sc->flags &= ~WPI_FLAG_BUSY; 3284 3285 ieee80211_new_state(ic, IEEE80211_S_INIT, -1); 3286} 3287 3288static void 3289wpi_iter_func(void *arg, struct ieee80211_node *ni) 3290{ 3291 struct wpi_softc *sc = arg; 3292 struct wpi_node *wn = (struct wpi_node *)ni; 3293 3294 ieee80211_amrr_choose(&sc->amrr, ni, &wn->amn); 3295} 3296 3297static void 3298wpi_newassoc(struct ieee80211_node *ni, int isnew) 3299{ 3300 struct wpi_softc *sc = ni->ni_ic->ic_ifp->if_softc; 3301 int i; 3302 3303 ieee80211_amrr_node_init(&sc->amrr, &((struct wpi_node *)ni)->amn); 3304 3305 for (i = ni->ni_rates.rs_nrates - 1; 3306 i > 0 && (ni->ni_rates.rs_rates[i] & IEEE80211_RATE_VAL) > 72; 3307 i--); 3308 ni->ni_txrate = i; 3309} 3310 3311static void 3312wpi_calib_timeout(void *arg) 3313{ 3314 struct wpi_softc *sc = arg; 3315 struct ieee80211com *ic = &sc->sc_ic; 3316 int temp; 3317 WPI_LOCK_DECL; 3318 3319 /* automatic rate control triggered every 500ms */ 3320 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) { 3321 WPI_LOCK(sc); 3322 if (ic->ic_opmode == IEEE80211_M_STA) 3323 wpi_iter_func(sc, ic->ic_bss); 3324 else 3325 ieee80211_iterate_nodes(&ic->ic_sta, wpi_iter_func, sc); 3326 WPI_UNLOCK(sc); 3327 } 3328 3329 /* update sensor data */ 3330 temp = (int)WPI_READ(sc, WPI_TEMPERATURE); 3331 DPRINTFN(WPI_DEBUG_TEMP,("Temp in calibration is: %d\n", temp)); 3332#if 0 3333 //XXX Used by OpenBSD Sensor Framework 3334 sc->sensor.value = temp + 260; 3335#endif 3336 3337 /* automatic power calibration every 60s */ 3338 if (++sc->calib_cnt >= 120) { 3339 wpi_power_calibration(sc, temp); 3340 sc->calib_cnt = 0; 3341 } 3342 3343 callout_reset(&sc->calib_to, hz/2, wpi_calib_timeout, sc); 3344} 3345 3346/* 3347 * This function is called periodically (every 60 seconds) to adjust output 3348 * power to temperature changes. 3349 */ 3350static void 3351wpi_power_calibration(struct wpi_softc *sc, int temp) 3352{ 3353 /* sanity-check read value */ 3354 if (temp < -260 || temp > 25) { 3355 /* this can't be correct, ignore */ 3356 DPRINTFN(WPI_DEBUG_TEMP, 3357 ("out-of-range temperature reported: %d\n", temp)); 3358 return; 3359 } 3360 3361 DPRINTFN(WPI_DEBUG_TEMP,("temperature %d->%d\n", sc->temp, temp)); 3362 3363 /* adjust Tx power if need be */ 3364 if (abs(temp - sc->temp) <= 6) 3365 return; 3366 3367 sc->temp = temp; 3368 3369 if (wpi_set_txpower(sc, sc->sc_ic.ic_bss->ni_chan,1) != 0) { 3370 /* just warn, too bad for the automatic calibration... */ 3371 device_printf(sc->sc_dev,"could not adjust Tx power\n"); 3372 } 3373} 3374 3375/** 3376 * Read the eeprom to find out what channels are valid for the given 3377 * band and update net80211 with what we find. 3378 */ 3379static void 3380wpi_read_eeprom_channels(struct wpi_softc *sc, int n) 3381{ 3382 struct ieee80211com *ic = &sc->sc_ic; 3383 const struct wpi_chan_band *band = &wpi_bands[n]; 3384 struct wpi_eeprom_chan channels[WPI_MAX_CHAN_PER_BAND]; 3385 int chan, i, offset, passive; 3386 3387 wpi_read_prom_data(sc, band->addr, channels, 3388 band->nchan * sizeof (struct wpi_eeprom_chan)); 3389 3390 for (i = 0; i < band->nchan; i++) { 3391 if (!(channels[i].flags & WPI_EEPROM_CHAN_VALID)) { 3392 DPRINTFN(WPI_DEBUG_HW, 3393 ("Channel Not Valid: %d, band %d\n", 3394 band->chan[i],n)); 3395 continue; 3396 } 3397 3398 passive = 0; 3399 chan = band->chan[i]; 3400 offset = ic->ic_nchans; 3401 3402 /* is active scan allowed on this channel? */ 3403 if (!(channels[i].flags & WPI_EEPROM_CHAN_ACTIVE)) { 3404 passive = IEEE80211_CHAN_PASSIVE; 3405 } 3406 3407 if (n == 0) { /* 2GHz band */ 3408 ic->ic_channels[offset].ic_ieee = chan; 3409 ic->ic_channels[offset].ic_freq = 3410 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ); 3411 ic->ic_channels[offset].ic_flags = IEEE80211_CHAN_B | passive; 3412 offset++; 3413 ic->ic_channels[offset].ic_ieee = chan; 3414 ic->ic_channels[offset].ic_freq = 3415 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_2GHZ); 3416 ic->ic_channels[offset].ic_flags = IEEE80211_CHAN_G | passive; 3417 offset++; 3418 3419 } else { /* 5GHz band */ 3420 /* 3421 * Some 3945ABG adapters support channels 7, 8, 11 3422 * and 12 in the 2GHz *and* 5GHz bands. 3423 * Because of limitations in our net80211(9) stack, 3424 * we can't support these channels in 5GHz band. 3425 * XXX not true; just need to map to proper frequency 3426 */ 3427 if (chan <= 14) 3428 continue; 3429 3430 ic->ic_channels[offset].ic_ieee = chan; 3431 ic->ic_channels[offset].ic_freq = 3432 ieee80211_ieee2mhz(chan, IEEE80211_CHAN_5GHZ); 3433 ic->ic_channels[offset].ic_flags = IEEE80211_CHAN_A | passive; 3434 offset++; 3435 } 3436 3437 /* save maximum allowed power for this channel */ 3438 sc->maxpwr[chan] = channels[i].maxpwr; 3439 3440 ic->ic_nchans = offset; 3441 3442#if 0 3443 // XXX We can probably use this an get rid of maxpwr - ben 20070617 3444 ic->ic_channels[chan].ic_maxpower = channels[i].maxpwr; 3445 //ic->ic_channels[chan].ic_minpower... 3446 //ic->ic_channels[chan].ic_maxregtxpower... 3447#endif 3448 3449 DPRINTF(("adding chan %d flags=0x%x maxpwr=%d, offset %d\n", 3450 chan, channels[i].flags, sc->maxpwr[chan], offset)); 3451 } 3452} 3453 3454static void 3455wpi_read_eeprom_group(struct wpi_softc *sc, int n) 3456{ 3457 struct wpi_power_group *group = &sc->groups[n]; 3458 struct wpi_eeprom_group rgroup; 3459 int i; 3460 3461 wpi_read_prom_data(sc, WPI_EEPROM_POWER_GRP + n * 32, &rgroup, 3462 sizeof rgroup); 3463 3464 /* save power group information */ 3465 group->chan = rgroup.chan; 3466 group->maxpwr = rgroup.maxpwr; 3467 /* temperature at which the samples were taken */ 3468 group->temp = (int16_t)le16toh(rgroup.temp); 3469 3470 DPRINTF(("power group %d: chan=%d maxpwr=%d temp=%d\n", n, 3471 group->chan, group->maxpwr, group->temp)); 3472 3473 for (i = 0; i < WPI_SAMPLES_COUNT; i++) { 3474 group->samples[i].index = rgroup.samples[i].index; 3475 group->samples[i].power = rgroup.samples[i].power; 3476 3477 DPRINTF(("\tsample %d: index=%d power=%d\n", i, 3478 group->samples[i].index, group->samples[i].power)); 3479 } 3480} 3481 3482/* 3483 * Update Tx power to match what is defined for channel `c'. 3484 */ 3485static int 3486wpi_set_txpower(struct wpi_softc *sc, struct ieee80211_channel *c, int async) 3487{ 3488 struct ieee80211com *ic = &sc->sc_ic; 3489 struct wpi_power_group *group; 3490 struct wpi_cmd_txpower txpower; 3491 u_int chan; 3492 int i; 3493 3494 /* get channel number */ 3495 chan = ieee80211_chan2ieee(ic, c); 3496 3497 /* find the power group to which this channel belongs */ 3498 if (IEEE80211_IS_CHAN_5GHZ(c)) { 3499 for (group = &sc->groups[1]; group < &sc->groups[4]; group++) 3500 if (chan <= group->chan) 3501 break; 3502 } else 3503 group = &sc->groups[0]; 3504 3505 memset(&txpower, 0, sizeof txpower); 3506 txpower.band = IEEE80211_IS_CHAN_5GHZ(c) ? 0 : 1; 3507 txpower.channel = htole16(chan); 3508 3509 /* set Tx power for all OFDM and CCK rates */ 3510 for (i = 0; i <= 11 ; i++) { 3511 /* retrieve Tx power for this channel/rate combination */ 3512 int idx = wpi_get_power_index(sc, group, c, 3513 wpi_ridx_to_rate[i]); 3514 3515 txpower.rates[i].rate = wpi_ridx_to_plcp[i]; 3516 3517 if (IEEE80211_IS_CHAN_5GHZ(c)) { 3518 txpower.rates[i].gain_radio = wpi_rf_gain_5ghz[idx]; 3519 txpower.rates[i].gain_dsp = wpi_dsp_gain_5ghz[idx]; 3520 } else { 3521 txpower.rates[i].gain_radio = wpi_rf_gain_2ghz[idx]; 3522 txpower.rates[i].gain_dsp = wpi_dsp_gain_2ghz[idx]; 3523 } 3524 DPRINTFN(WPI_DEBUG_TEMP,("chan %d/rate %d: power index %d\n", 3525 chan, wpi_ridx_to_rate[i], idx)); 3526 } 3527 3528 return wpi_cmd(sc, WPI_CMD_TXPOWER, &txpower, sizeof txpower, async); 3529} 3530 3531/* 3532 * Determine Tx power index for a given channel/rate combination. 3533 * This takes into account the regulatory information from EEPROM and the 3534 * current temperature. 3535 */ 3536static int 3537wpi_get_power_index(struct wpi_softc *sc, struct wpi_power_group *group, 3538 struct ieee80211_channel *c, int rate) 3539{ 3540/* fixed-point arithmetic division using a n-bit fractional part */ 3541#define fdivround(a, b, n) \ 3542 ((((1 << n) * (a)) / (b) + (1 << n) / 2) / (1 << n)) 3543 3544/* linear interpolation */ 3545#define interpolate(x, x1, y1, x2, y2, n) \ 3546 ((y1) + fdivround(((x) - (x1)) * ((y2) - (y1)), (x2) - (x1), n)) 3547 3548 struct ieee80211com *ic = &sc->sc_ic; 3549 struct wpi_power_sample *sample; 3550 int pwr, idx; 3551 u_int chan; 3552 3553 /* get channel number */ 3554 chan = ieee80211_chan2ieee(ic, c); 3555 3556 /* default power is group's maximum power - 3dB */ 3557 pwr = group->maxpwr / 2; 3558 3559 /* decrease power for highest OFDM rates to reduce distortion */ 3560 switch (rate) { 3561 case 72: /* 36Mb/s */ 3562 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 0 : 5; 3563 break; 3564 case 96: /* 48Mb/s */ 3565 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 7 : 10; 3566 break; 3567 case 108: /* 54Mb/s */ 3568 pwr -= IEEE80211_IS_CHAN_2GHZ(c) ? 9 : 12; 3569 break; 3570 } 3571 3572 /* never exceed channel's maximum allowed Tx power */ 3573 pwr = min(pwr, sc->maxpwr[chan]); 3574 3575 /* retrieve power index into gain tables from samples */ 3576 for (sample = group->samples; sample < &group->samples[3]; sample++) 3577 if (pwr > sample[1].power) 3578 break; 3579 /* fixed-point linear interpolation using a 19-bit fractional part */ 3580 idx = interpolate(pwr, sample[0].power, sample[0].index, 3581 sample[1].power, sample[1].index, 19); 3582 3583 /* 3584 * Adjust power index based on current temperature 3585 * - if colder than factory-calibrated: decreate output power 3586 * - if warmer than factory-calibrated: increase output power 3587 */ 3588 idx -= (sc->temp - group->temp) * 11 / 100; 3589 3590 /* decrease power for CCK rates (-5dB) */ 3591 if (!WPI_RATE_IS_OFDM(rate)) 3592 idx += 10; 3593 3594 /* keep power index in a valid range */ 3595 if (idx < 0) 3596 return 0; 3597 if (idx > WPI_MAX_PWR_INDEX) 3598 return WPI_MAX_PWR_INDEX; 3599 return idx; 3600 3601#undef interpolate 3602#undef fdivround 3603} 3604 3605#if 0 3606static void 3607wpi_radio_on(void *arg, int pending) 3608{ 3609 struct wpi_softc *sc = arg; 3610 3611 device_printf(sc->sc_dev, "radio turned on\n"); 3612} 3613 3614static void 3615wpi_radio_off(void *arg, int pending) 3616{ 3617 struct wpi_softc *sc = arg; 3618 3619 device_printf(sc->sc_dev, "radio turned off\n"); 3620} 3621#endif 3622 3623/** 3624 * Called by net80211 framework to indicate that a scan 3625 * is starting. This function doesn't actually do the scan, 3626 * wpi_scan_curchan starts things off. This function is more 3627 * of an early warning from the framework we should get ready 3628 * for the scan. 3629 */ 3630static void 3631wpi_scan_start(struct ieee80211com *ic) 3632{ 3633 struct ifnet *ifp = ic->ic_ifp; 3634 struct wpi_softc *sc = ifp->if_softc; 3635 3636 wpi_queue_cmd(sc, WPI_SCAN_START); 3637} 3638 3639/** 3640 * Called by the net80211 framework, indicates that the 3641 * scan has ended. If there is a scan in progress on the card 3642 * then it should be aborted. 3643 */ 3644static void 3645wpi_scan_end(struct ieee80211com *ic) 3646{ 3647 struct ifnet *ifp = ic->ic_ifp; 3648 struct wpi_softc *sc = ifp->if_softc; 3649 3650 wpi_queue_cmd(sc, WPI_SCAN_STOP); 3651} 3652 3653/** 3654 * Called by the net80211 framework to indicate to the driver 3655 * that the channel should be changed 3656 */ 3657static void 3658wpi_set_channel(struct ieee80211com *ic) 3659{ 3660 struct ifnet *ifp = ic->ic_ifp; 3661 struct wpi_softc *sc = ifp->if_softc; 3662 3663 wpi_queue_cmd(sc, WPI_SET_CHAN); 3664} 3665 3666/** 3667 * Called by net80211 to indicate that we need to scan the current 3668 * channel. The channel is previously be set via the wpi_set_channel 3669 * callback. 3670 */ 3671static void 3672wpi_scan_curchan(struct ieee80211com *ic, unsigned long maxdwell) 3673{ 3674 struct ifnet *ifp = ic->ic_ifp; 3675 struct wpi_softc *sc = ifp->if_softc; 3676 3677 sc->maxdwell = maxdwell; 3678 3679 wpi_queue_cmd(sc, WPI_SCAN_CURCHAN); 3680} 3681 3682/** 3683 * Called by the net80211 framework to indicate 3684 * the minimum dwell time has been met, terminate the scan. 3685 * We don't actually terminate the scan as the firmware will notify 3686 * us when it's finished and we have no way to interrupt it. 3687 */ 3688static void 3689wpi_scan_mindwell(struct ieee80211com *ic) 3690{ 3691 /* NB: don't try to abort scan; wait for firmware to finish */ 3692} 3693 3694/** 3695 * The ops function is called to perform some actual work. 3696 * because we can't sleep from any of the ic callbacks, we queue an 3697 * op task with wpi_queue_cmd and have the taskqueue process that task. 3698 * The task that gets cued is a op task, which ends up calling this function. 3699 */ 3700static void 3701wpi_ops(void *arg, int pending) 3702{ 3703 struct wpi_softc *sc = arg; 3704 struct ieee80211com *ic = &sc->sc_ic; 3705 WPI_LOCK_DECL; 3706 int cmd; 3707 3708again: 3709 WPI_CMD_LOCK(sc); 3710 cmd = sc->sc_cmd[sc->sc_cmd_cur]; 3711 3712 if (cmd == 0) { 3713 /* No more commands to process */ 3714 WPI_CMD_UNLOCK(sc); 3715 return; 3716 } 3717 sc->sc_cmd[sc->sc_cmd_cur] = 0; /* free the slot */ 3718 sc->sc_cmd_cur = (sc->sc_cmd_cur + 1) % WPI_CMD_MAXOPS; 3719 WPI_CMD_UNLOCK(sc); 3720 WPI_LOCK(sc); 3721 3722 if (!(sc->sc_ifp->if_drv_flags & IFF_DRV_RUNNING)) { 3723 WPI_UNLOCK(sc); 3724 return; 3725 } 3726 3727 { 3728 const char *name[]={"SCAN_START", "SCAN_CURCHAN",0,"STOP",0,0,0,"CHAN", 3729 0,0,0,0,0,0,"AUTH",0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,"NEXT"}; 3730 DPRINTFN(WPI_DEBUG_OPS,("wpi_ops: command: %d %s\n", cmd, name[cmd-1])); 3731 } 3732 3733 switch (cmd) { 3734 case WPI_SCAN_START: 3735 if (sc->flags & WPI_FLAG_HW_RADIO_OFF) { 3736 DPRINTF(("HERER\n")); 3737 ieee80211_cancel_scan(ic); 3738 } else 3739 sc->flags |= WPI_FLAG_SCANNING; 3740 break; 3741 3742 case WPI_SCAN_STOP: 3743 sc->flags &= ~WPI_FLAG_SCANNING; 3744 break; 3745 3746 case WPI_SCAN_NEXT: 3747 DPRINTF(("NEXT\n")); 3748 WPI_UNLOCK(sc); 3749 ieee80211_scan_next(ic); 3750 WPI_LOCK(sc); 3751 break; 3752 3753 case WPI_SCAN_CURCHAN: 3754 if (wpi_scan(sc)) 3755 ieee80211_cancel_scan(ic); 3756 break; 3757 3758 case WPI_SET_CHAN: 3759 if (sc->flags&WPI_FLAG_AUTH) { 3760 DPRINTF(("Authenticating, not changing channel\n")); 3761 break; 3762 } 3763 if (wpi_config(sc)) { 3764 DPRINTF(("Scan cancelled\n")); 3765 WPI_UNLOCK(sc); 3766 ieee80211_cancel_scan(ic); 3767 WPI_LOCK(sc); 3768 sc->flags &= ~WPI_FLAG_SCANNING; 3769 wpi_restart(sc,0); 3770 WPI_UNLOCK(sc); 3771 return; 3772 } 3773 break; 3774 3775 case WPI_AUTH: 3776 if (wpi_auth(sc) != 0) { 3777 device_printf(sc->sc_dev, 3778 "could not send authentication request\n"); 3779 wpi_stop_locked(sc); 3780 WPI_UNLOCK(sc); 3781 return; 3782 } 3783 WPI_UNLOCK(sc); 3784 ieee80211_node_authorize(ic->ic_bss); 3785 ieee80211_new_state(ic, IEEE80211_S_ASSOC, -1); 3786 WPI_LOCK(sc); 3787 break; 3788 } 3789 WPI_UNLOCK(sc); 3790 3791 /* Take another pass */ 3792 goto again; 3793} 3794 3795/** 3796 * queue a command for later execution in a different thread. 3797 * This is needed as the net80211 callbacks do not allow 3798 * sleeping, since we need to sleep to confirm commands have 3799 * been processed by the firmware, we must defer execution to 3800 * a sleep enabled thread. 3801 */ 3802static int 3803wpi_queue_cmd(struct wpi_softc *sc, int cmd) 3804{ 3805 WPI_CMD_LOCK(sc); 3806 3807 if (sc->sc_cmd[sc->sc_cmd_next] != 0) { 3808 WPI_CMD_UNLOCK(sc); 3809 DPRINTF(("%s: command %d dropped\n", __func__, cmd)); 3810 return (EBUSY); 3811 } 3812 3813 sc->sc_cmd[sc->sc_cmd_next] = cmd; 3814 sc->sc_cmd_next = (sc->sc_cmd_next + 1) % WPI_CMD_MAXOPS; 3815 3816 taskqueue_enqueue(sc->sc_tq, &sc->sc_opstask); 3817 3818 WPI_CMD_UNLOCK(sc); 3819 3820 return 0; 3821} 3822 3823static void 3824wpi_restart(void * arg, int pending) 3825{ 3826#if 0 3827 struct wpi_softc *sc = arg; 3828 struct ieee80211com *ic = &sc->sc_ic; 3829 WPI_LOCK_DECL; 3830 3831 DPRINTF(("Device failed, restarting device\n")); 3832 WPI_LOCK(sc); 3833 wpi_stop(sc); 3834 wpi_init(sc); 3835 WPI_UNLOCK(sc); 3836 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1); 3837#endif 3838} 3839 3840/* 3841 * Allocate DMA-safe memory for firmware transfer. 3842 */ 3843static int 3844wpi_alloc_fwmem(struct wpi_softc *sc) 3845{ 3846 /* allocate enough contiguous space to store text and data */ 3847 return wpi_dma_contig_alloc(sc, &sc->fw_dma, NULL, 3848 WPI_FW_MAIN_TEXT_MAXSZ + WPI_FW_MAIN_DATA_MAXSZ, 1, 3849 BUS_DMA_NOWAIT); 3850} 3851 3852static void 3853wpi_free_fwmem(struct wpi_softc *sc) 3854{ 3855 wpi_dma_contig_free(&sc->fw_dma); 3856} 3857 3858/** 3859 * Called every second, wpi_tick used by the watch dog timer 3860 * to check that the card is still alive 3861 */ 3862static void 3863wpi_tick(void *arg) 3864{ 3865 struct wpi_softc *sc = arg; 3866 3867 DPRINTFN(WPI_DEBUG_WATCHDOG,("Watchdog: tick\n")); 3868 3869 wpi_watchdog(sc->sc_ifp); 3870 callout_reset(&sc->watchdog_to, hz, wpi_tick, sc); 3871} 3872 3873#ifdef WPI_DEBUG 3874static const char *wpi_cmd_str(int cmd) 3875{ 3876 switch(cmd) { 3877 case WPI_DISABLE_CMD: return "WPI_DISABLE_CMD"; 3878 case WPI_CMD_CONFIGURE: return "WPI_CMD_CONFIGURE"; 3879 case WPI_CMD_ASSOCIATE: return "WPI_CMD_ASSOCIATE"; 3880 case WPI_CMD_SET_WME: return "WPI_CMD_SET_WME"; 3881 case WPI_CMD_TSF: return "WPI_CMD_TSF"; 3882 case WPI_CMD_ADD_NODE: return "WPI_CMD_ADD_NODE"; 3883 case WPI_CMD_TX_DATA: return "WPI_CMD_TX_DATA"; 3884 case WPI_CMD_MRR_SETUP: return "WPI_CMD_MRR_SETUP"; 3885 case WPI_CMD_SET_LED: return "WPI_CMD_SET_LED"; 3886 case WPI_CMD_SET_POWER_MODE: return "WPI_CMD_SET_POWER_MODE"; 3887 case WPI_CMD_SCAN: return "WPI_CMD_SCAN"; 3888 case WPI_CMD_SET_BEACON:return "WPI_CMD_SET_BEACON"; 3889 case WPI_CMD_TXPOWER: return "WPI_CMD_TXPOWER"; 3890 case WPI_CMD_BLUETOOTH: return "WPI_CMD_BLUETOOTH"; 3891 3892 default: 3893 KASSERT(1, ("Unknown Command: %d\n", cmd)); 3894 return "UNKNOWN CMD"; // Make the compiler happy 3895 } 3896} 3897#endif 3898 3899MODULE_DEPEND(wpi, pci, 1, 1, 1); 3900MODULE_DEPEND(wpi, wlan, 1, 1, 1); 3901MODULE_DEPEND(wpi, firmware, 1, 1, 1); 3902MODULE_DEPEND(wpi, wlan_amrr, 1, 1, 1); 3903