1 /*- 2 * Copyright (c) 2006 Sam Leffler, Errno Consulting 3 * Copyright (c) 2008-2009 Weongyo Jeong <weongyo@freebsd.org> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer, 11 * without modification. 12 * 2. Redistributions in binary form must reproduce at minimum a disclaimer 13 * similar to the "NO WARRANTY" disclaimer below ("Disclaimer") and any 14 * redistribution must be conditioned upon including a substantially 15 * similar Disclaimer requirement for further binary redistribution. 16 * 17 * NO WARRANTY 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF NONINFRINGEMENT, MERCHANTIBILITY 21 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 22 * THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, 23 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 26 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 * THE POSSIBILITY OF SUCH DAMAGES. 29 */ 30 31 /* 32 * This driver is distantly derived from a driver of the same name 33 * by Damien Bergamini. The original copyright is included below: 34 * 35 * Copyright (c) 2006 36 * Damien Bergamini <damien.bergamini@free.fr> 37 * 38 * Permission to use, copy, modify, and distribute this software for any 39 * purpose with or without fee is hereby granted, provided that the above 40 * copyright notice and this permission notice appear in all copies. 41 * 42 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 43 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 44 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 45 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 46 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 47 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 48 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 49 */ 50 51 #include <sys/cdefs.h> 52 __FBSDID("$FreeBSD$"); 53 54 /*- 55 * Driver for Atheros AR5523 USB parts. 56 * 57 * The driver requires firmware to be loaded into the device. This 58 * is done on device discovery from a user application (uathload) 59 * that is launched by devd when a device with suitable product ID 60 * is recognized. Once firmware has been loaded the device will 61 * reset the USB port and re-attach with the original product ID+1 62 * and this driver will be attached. The firmware is licensed for 63 * general use (royalty free) and may be incorporated in products. 64 * Note that the firmware normally packaged with the NDIS drivers 65 * for these devices does not work in this way and so does not work 66 * with this driver. 67 */ 68 #include <sys/param.h> 69 #include <sys/sockio.h> 70 #include <sys/sysctl.h> 71 #include <sys/lock.h> 72 #include <sys/mutex.h> 73 #include <sys/mbuf.h> 74 #include <sys/kernel.h> 75 #include <sys/socket.h> 76 #include <sys/systm.h> 77 #include <sys/malloc.h> 78 #include <sys/module.h> 79 #include <sys/bus.h> 80 #include <sys/endian.h> 81 #include <sys/kdb.h> 82 83 #include <machine/bus.h> 84 #include <machine/resource.h> 85 #include <sys/rman.h> 86 87 #include <net/bpf.h> 88 #include <net/if.h> 89 #include <net/if_var.h> 90 #include <net/if_arp.h> 91 #include <net/ethernet.h> 92 #include <net/if_dl.h> 93 #include <net/if_media.h> 94 #include <net/if_types.h> 95 96 #ifdef INET 97 #include <netinet/in.h> 98 #include <netinet/in_systm.h> 99 #include <netinet/in_var.h> 100 #include <netinet/if_ether.h> 101 #include <netinet/ip.h> 102 #endif 103 104 #include <net80211/ieee80211_var.h> 105 #include <net80211/ieee80211_input.h> 106 #include <net80211/ieee80211_regdomain.h> 107 #include <net80211/ieee80211_radiotap.h> 108 109 #include <dev/usb/usb.h> 110 #include <dev/usb/usbdi.h> 111 #include "usbdevs.h" 112 113 #include <dev/usb/wlan/if_uathreg.h> 114 #include <dev/usb/wlan/if_uathvar.h> 115 116 static SYSCTL_NODE(_hw_usb, OID_AUTO, uath, CTLFLAG_RW, 0, "USB Atheros"); 117 118 static int uath_countrycode = CTRY_DEFAULT; /* country code */ 119 SYSCTL_INT(_hw_usb_uath, OID_AUTO, countrycode, CTLFLAG_RWTUN, &uath_countrycode, 120 0, "country code"); 121 static int uath_regdomain = 0; /* regulatory domain */ 122 SYSCTL_INT(_hw_usb_uath, OID_AUTO, regdomain, CTLFLAG_RD, &uath_regdomain, 123 0, "regulatory domain"); 124 125 #ifdef UATH_DEBUG 126 int uath_debug = 0; 127 SYSCTL_INT(_hw_usb_uath, OID_AUTO, debug, CTLFLAG_RWTUN, &uath_debug, 0, 128 "uath debug level"); 129 enum { 130 UATH_DEBUG_XMIT = 0x00000001, /* basic xmit operation */ 131 UATH_DEBUG_XMIT_DUMP = 0x00000002, /* xmit dump */ 132 UATH_DEBUG_RECV = 0x00000004, /* basic recv operation */ 133 UATH_DEBUG_TX_PROC = 0x00000008, /* tx ISR proc */ 134 UATH_DEBUG_RX_PROC = 0x00000010, /* rx ISR proc */ 135 UATH_DEBUG_RECV_ALL = 0x00000020, /* trace all frames (beacons) */ 136 UATH_DEBUG_INIT = 0x00000040, /* initialization of dev */ 137 UATH_DEBUG_DEVCAP = 0x00000080, /* dev caps */ 138 UATH_DEBUG_CMDS = 0x00000100, /* commands */ 139 UATH_DEBUG_CMDS_DUMP = 0x00000200, /* command buffer dump */ 140 UATH_DEBUG_RESET = 0x00000400, /* reset processing */ 141 UATH_DEBUG_STATE = 0x00000800, /* 802.11 state transitions */ 142 UATH_DEBUG_MULTICAST = 0x00001000, /* multicast */ 143 UATH_DEBUG_WME = 0x00002000, /* WME */ 144 UATH_DEBUG_CHANNEL = 0x00004000, /* channel */ 145 UATH_DEBUG_RATES = 0x00008000, /* rates */ 146 UATH_DEBUG_CRYPTO = 0x00010000, /* crypto */ 147 UATH_DEBUG_LED = 0x00020000, /* LED */ 148 UATH_DEBUG_ANY = 0xffffffff 149 }; 150 #define DPRINTF(sc, m, fmt, ...) do { \ 151 if (sc->sc_debug & (m)) \ 152 printf(fmt, __VA_ARGS__); \ 153 } while (0) 154 #else 155 #define DPRINTF(sc, m, fmt, ...) do { \ 156 (void) sc; \ 157 } while (0) 158 #endif 159 160 /* recognized device vendors/products */ 161 static const STRUCT_USB_HOST_ID uath_devs[] = { 162 #define UATH_DEV(v,p) { USB_VP(USB_VENDOR_##v, USB_PRODUCT_##v##_##p) } 163 UATH_DEV(ACCTON, SMCWUSBTG2), 164 UATH_DEV(ATHEROS, AR5523), 165 UATH_DEV(ATHEROS2, AR5523_1), 166 UATH_DEV(ATHEROS2, AR5523_2), 167 UATH_DEV(ATHEROS2, AR5523_3), 168 UATH_DEV(CONCEPTRONIC, AR5523_1), 169 UATH_DEV(CONCEPTRONIC, AR5523_2), 170 UATH_DEV(DLINK, DWLAG122), 171 UATH_DEV(DLINK, DWLAG132), 172 UATH_DEV(DLINK, DWLG132), 173 UATH_DEV(DLINK2, DWA120), 174 UATH_DEV(GIGASET, AR5523), 175 UATH_DEV(GIGASET, SMCWUSBTG), 176 UATH_DEV(GLOBALSUN, AR5523_1), 177 UATH_DEV(GLOBALSUN, AR5523_2), 178 UATH_DEV(NETGEAR, WG111U), 179 UATH_DEV(NETGEAR3, WG111T), 180 UATH_DEV(NETGEAR3, WPN111), 181 UATH_DEV(NETGEAR3, WPN111_2), 182 UATH_DEV(UMEDIA, TEW444UBEU), 183 UATH_DEV(UMEDIA, AR5523_2), 184 UATH_DEV(WISTRONNEWEB, AR5523_1), 185 UATH_DEV(WISTRONNEWEB, AR5523_2), 186 UATH_DEV(ZCOM, AR5523) 187 #undef UATH_DEV 188 }; 189 190 static usb_callback_t uath_intr_rx_callback; 191 static usb_callback_t uath_intr_tx_callback; 192 static usb_callback_t uath_bulk_rx_callback; 193 static usb_callback_t uath_bulk_tx_callback; 194 195 static const struct usb_config uath_usbconfig[UATH_N_XFERS] = { 196 [UATH_INTR_RX] = { 197 .type = UE_BULK, 198 .endpoint = 0x1, 199 .direction = UE_DIR_IN, 200 .bufsize = UATH_MAX_CMDSZ, 201 .flags = { 202 .pipe_bof = 1, 203 .short_xfer_ok = 1 204 }, 205 .callback = uath_intr_rx_callback 206 }, 207 [UATH_INTR_TX] = { 208 .type = UE_BULK, 209 .endpoint = 0x1, 210 .direction = UE_DIR_OUT, 211 .bufsize = UATH_MAX_CMDSZ * UATH_CMD_LIST_COUNT, 212 .flags = { 213 .force_short_xfer = 1, 214 .pipe_bof = 1, 215 }, 216 .callback = uath_intr_tx_callback, 217 .timeout = UATH_CMD_TIMEOUT 218 }, 219 [UATH_BULK_RX] = { 220 .type = UE_BULK, 221 .endpoint = 0x2, 222 .direction = UE_DIR_IN, 223 .bufsize = MCLBYTES, 224 .flags = { 225 .ext_buffer = 1, 226 .pipe_bof = 1, 227 .short_xfer_ok = 1 228 }, 229 .callback = uath_bulk_rx_callback 230 }, 231 [UATH_BULK_TX] = { 232 .type = UE_BULK, 233 .endpoint = 0x2, 234 .direction = UE_DIR_OUT, 235 .bufsize = UATH_MAX_TXBUFSZ * UATH_TX_DATA_LIST_COUNT, 236 .flags = { 237 .force_short_xfer = 1, 238 .pipe_bof = 1 239 }, 240 .callback = uath_bulk_tx_callback, 241 .timeout = UATH_DATA_TIMEOUT 242 } 243 }; 244 245 static struct ieee80211vap *uath_vap_create(struct ieee80211com *, 246 const char [IFNAMSIZ], int, enum ieee80211_opmode, int, 247 const uint8_t [IEEE80211_ADDR_LEN], 248 const uint8_t [IEEE80211_ADDR_LEN]); 249 static void uath_vap_delete(struct ieee80211vap *); 250 static int uath_alloc_cmd_list(struct uath_softc *, struct uath_cmd []); 251 static void uath_free_cmd_list(struct uath_softc *, struct uath_cmd []); 252 static int uath_host_available(struct uath_softc *); 253 static int uath_get_capability(struct uath_softc *, uint32_t, uint32_t *); 254 static int uath_get_devcap(struct uath_softc *); 255 static struct uath_cmd * 256 uath_get_cmdbuf(struct uath_softc *); 257 static int uath_cmd_read(struct uath_softc *, uint32_t, const void *, 258 int, void *, int, int); 259 static int uath_cmd_write(struct uath_softc *, uint32_t, const void *, 260 int, int); 261 static void uath_stat(void *); 262 #ifdef UATH_DEBUG 263 static void uath_dump_cmd(const uint8_t *, int, char); 264 static const char * 265 uath_codename(int); 266 #endif 267 static int uath_get_devstatus(struct uath_softc *, 268 uint8_t macaddr[IEEE80211_ADDR_LEN]); 269 static int uath_get_status(struct uath_softc *, uint32_t, void *, int); 270 static int uath_alloc_rx_data_list(struct uath_softc *); 271 static int uath_alloc_tx_data_list(struct uath_softc *); 272 static void uath_free_rx_data_list(struct uath_softc *); 273 static void uath_free_tx_data_list(struct uath_softc *); 274 static int uath_init(struct uath_softc *); 275 static void uath_stop(struct uath_softc *); 276 static void uath_parent(struct ieee80211com *); 277 static int uath_transmit(struct ieee80211com *, struct mbuf *); 278 static void uath_start(struct uath_softc *); 279 static int uath_raw_xmit(struct ieee80211_node *, struct mbuf *, 280 const struct ieee80211_bpf_params *); 281 static void uath_scan_start(struct ieee80211com *); 282 static void uath_scan_end(struct ieee80211com *); 283 static void uath_set_channel(struct ieee80211com *); 284 static void uath_update_mcast(struct ieee80211com *); 285 static void uath_update_promisc(struct ieee80211com *); 286 static int uath_config(struct uath_softc *, uint32_t, uint32_t); 287 static int uath_config_multi(struct uath_softc *, uint32_t, const void *, 288 int); 289 static int uath_switch_channel(struct uath_softc *, 290 struct ieee80211_channel *); 291 static int uath_set_rxfilter(struct uath_softc *, uint32_t, uint32_t); 292 static void uath_watchdog(void *); 293 static void uath_abort_xfers(struct uath_softc *); 294 static int uath_dataflush(struct uath_softc *); 295 static int uath_cmdflush(struct uath_softc *); 296 static int uath_flush(struct uath_softc *); 297 static int uath_set_ledstate(struct uath_softc *, int); 298 static int uath_set_chan(struct uath_softc *, struct ieee80211_channel *); 299 static int uath_reset_tx_queues(struct uath_softc *); 300 static int uath_wme_init(struct uath_softc *); 301 static struct uath_data * 302 uath_getbuf(struct uath_softc *); 303 static int uath_newstate(struct ieee80211vap *, enum ieee80211_state, 304 int); 305 static int uath_set_key(struct uath_softc *, 306 const struct ieee80211_key *, int); 307 static int uath_set_keys(struct uath_softc *, struct ieee80211vap *); 308 static void uath_sysctl_node(struct uath_softc *); 309 310 static int 311 uath_match(device_t dev) 312 { 313 struct usb_attach_arg *uaa = device_get_ivars(dev); 314 315 if (uaa->usb_mode != USB_MODE_HOST) 316 return (ENXIO); 317 if (uaa->info.bConfigIndex != UATH_CONFIG_INDEX) 318 return (ENXIO); 319 if (uaa->info.bIfaceIndex != UATH_IFACE_INDEX) 320 return (ENXIO); 321 322 return (usbd_lookup_id_by_uaa(uath_devs, sizeof(uath_devs), uaa)); 323 } 324 325 static int 326 uath_attach(device_t dev) 327 { 328 struct uath_softc *sc = device_get_softc(dev); 329 struct usb_attach_arg *uaa = device_get_ivars(dev); 330 struct ieee80211com *ic = &sc->sc_ic; 331 uint8_t bands, iface_index = UATH_IFACE_INDEX; /* XXX */ 332 usb_error_t error; 333 334 sc->sc_dev = dev; 335 sc->sc_udev = uaa->device; 336 #ifdef UATH_DEBUG 337 sc->sc_debug = uath_debug; 338 #endif 339 device_set_usb_desc(dev); 340 341 /* 342 * Only post-firmware devices here. 343 */ 344 mtx_init(&sc->sc_mtx, device_get_nameunit(sc->sc_dev), MTX_NETWORK_LOCK, 345 MTX_DEF); 346 callout_init(&sc->stat_ch, 0); 347 callout_init_mtx(&sc->watchdog_ch, &sc->sc_mtx, 0); 348 mbufq_init(&sc->sc_snd, ifqmaxlen); 349 350 error = usbd_transfer_setup(uaa->device, &iface_index, sc->sc_xfer, 351 uath_usbconfig, UATH_N_XFERS, sc, &sc->sc_mtx); 352 if (error) { 353 device_printf(dev, "could not allocate USB transfers, " 354 "err=%s\n", usbd_errstr(error)); 355 goto fail; 356 } 357 358 sc->sc_cmd_dma_buf = 359 usbd_xfer_get_frame_buffer(sc->sc_xfer[UATH_INTR_TX], 0); 360 sc->sc_tx_dma_buf = 361 usbd_xfer_get_frame_buffer(sc->sc_xfer[UATH_BULK_TX], 0); 362 363 /* 364 * Setup buffers for firmware commands. 365 */ 366 error = uath_alloc_cmd_list(sc, sc->sc_cmd); 367 if (error != 0) { 368 device_printf(sc->sc_dev, 369 "could not allocate Tx command list\n"); 370 goto fail1; 371 } 372 373 /* 374 * We're now ready to send+receive firmware commands. 375 */ 376 UATH_LOCK(sc); 377 error = uath_host_available(sc); 378 if (error != 0) { 379 device_printf(sc->sc_dev, "could not initialize adapter\n"); 380 goto fail2; 381 } 382 error = uath_get_devcap(sc); 383 if (error != 0) { 384 device_printf(sc->sc_dev, 385 "could not get device capabilities\n"); 386 goto fail2; 387 } 388 UATH_UNLOCK(sc); 389 390 /* Create device sysctl node. */ 391 uath_sysctl_node(sc); 392 393 UATH_LOCK(sc); 394 error = uath_get_devstatus(sc, ic->ic_macaddr); 395 if (error != 0) { 396 device_printf(sc->sc_dev, "could not get device status\n"); 397 goto fail2; 398 } 399 400 /* 401 * Allocate xfers for Rx/Tx data pipes. 402 */ 403 error = uath_alloc_rx_data_list(sc); 404 if (error != 0) { 405 device_printf(sc->sc_dev, "could not allocate Rx data list\n"); 406 goto fail2; 407 } 408 error = uath_alloc_tx_data_list(sc); 409 if (error != 0) { 410 device_printf(sc->sc_dev, "could not allocate Tx data list\n"); 411 goto fail2; 412 } 413 UATH_UNLOCK(sc); 414 415 ic->ic_softc = sc; 416 ic->ic_name = device_get_nameunit(dev); 417 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 418 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */ 419 420 /* set device capabilities */ 421 ic->ic_caps = 422 IEEE80211_C_STA | /* station mode */ 423 IEEE80211_C_MONITOR | /* monitor mode supported */ 424 IEEE80211_C_TXPMGT | /* tx power management */ 425 IEEE80211_C_SHPREAMBLE | /* short preamble supported */ 426 IEEE80211_C_SHSLOT | /* short slot time supported */ 427 IEEE80211_C_WPA | /* 802.11i */ 428 IEEE80211_C_BGSCAN | /* capable of bg scanning */ 429 IEEE80211_C_TXFRAG; /* handle tx frags */ 430 431 /* put a regulatory domain to reveal informations. */ 432 uath_regdomain = sc->sc_devcap.regDomain; 433 434 bands = 0; 435 setbit(&bands, IEEE80211_MODE_11B); 436 setbit(&bands, IEEE80211_MODE_11G); 437 if ((sc->sc_devcap.analog5GhzRevision & 0xf0) == 0x30) 438 setbit(&bands, IEEE80211_MODE_11A); 439 /* XXX turbo */ 440 ieee80211_init_channels(ic, NULL, &bands); 441 442 ieee80211_ifattach(ic); 443 ic->ic_raw_xmit = uath_raw_xmit; 444 ic->ic_scan_start = uath_scan_start; 445 ic->ic_scan_end = uath_scan_end; 446 ic->ic_set_channel = uath_set_channel; 447 ic->ic_vap_create = uath_vap_create; 448 ic->ic_vap_delete = uath_vap_delete; 449 ic->ic_update_mcast = uath_update_mcast; 450 ic->ic_update_promisc = uath_update_promisc; 451 ic->ic_transmit = uath_transmit; 452 ic->ic_parent = uath_parent; 453 454 ieee80211_radiotap_attach(ic, 455 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 456 UATH_TX_RADIOTAP_PRESENT, 457 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 458 UATH_RX_RADIOTAP_PRESENT); 459 460 if (bootverbose) 461 ieee80211_announce(ic); 462 463 return (0); 464 465 fail2: UATH_UNLOCK(sc); 466 uath_free_cmd_list(sc, sc->sc_cmd); 467 fail1: usbd_transfer_unsetup(sc->sc_xfer, UATH_N_XFERS); 468 fail: 469 return (error); 470 } 471 472 static int 473 uath_detach(device_t dev) 474 { 475 struct uath_softc *sc = device_get_softc(dev); 476 struct ieee80211com *ic = &sc->sc_ic; 477 unsigned int x; 478 479 /* 480 * Prevent further allocations from RX/TX/CMD 481 * data lists and ioctls 482 */ 483 UATH_LOCK(sc); 484 sc->sc_flags |= UATH_FLAG_INVALID; 485 486 STAILQ_INIT(&sc->sc_rx_active); 487 STAILQ_INIT(&sc->sc_rx_inactive); 488 489 STAILQ_INIT(&sc->sc_tx_active); 490 STAILQ_INIT(&sc->sc_tx_inactive); 491 STAILQ_INIT(&sc->sc_tx_pending); 492 493 STAILQ_INIT(&sc->sc_cmd_active); 494 STAILQ_INIT(&sc->sc_cmd_pending); 495 STAILQ_INIT(&sc->sc_cmd_waiting); 496 STAILQ_INIT(&sc->sc_cmd_inactive); 497 498 uath_stop(sc); 499 UATH_UNLOCK(sc); 500 501 callout_drain(&sc->stat_ch); 502 callout_drain(&sc->watchdog_ch); 503 504 /* drain USB transfers */ 505 for (x = 0; x != UATH_N_XFERS; x++) 506 usbd_transfer_drain(sc->sc_xfer[x]); 507 508 /* free data buffers */ 509 UATH_LOCK(sc); 510 uath_free_rx_data_list(sc); 511 uath_free_tx_data_list(sc); 512 uath_free_cmd_list(sc, sc->sc_cmd); 513 UATH_UNLOCK(sc); 514 515 /* free USB transfers and some data buffers */ 516 usbd_transfer_unsetup(sc->sc_xfer, UATH_N_XFERS); 517 518 ieee80211_ifdetach(ic); 519 mbufq_drain(&sc->sc_snd); 520 mtx_destroy(&sc->sc_mtx); 521 return (0); 522 } 523 524 static void 525 uath_free_cmd_list(struct uath_softc *sc, struct uath_cmd cmds[]) 526 { 527 int i; 528 529 for (i = 0; i != UATH_CMD_LIST_COUNT; i++) 530 cmds[i].buf = NULL; 531 } 532 533 static int 534 uath_alloc_cmd_list(struct uath_softc *sc, struct uath_cmd cmds[]) 535 { 536 int i; 537 538 STAILQ_INIT(&sc->sc_cmd_active); 539 STAILQ_INIT(&sc->sc_cmd_pending); 540 STAILQ_INIT(&sc->sc_cmd_waiting); 541 STAILQ_INIT(&sc->sc_cmd_inactive); 542 543 for (i = 0; i != UATH_CMD_LIST_COUNT; i++) { 544 struct uath_cmd *cmd = &cmds[i]; 545 546 cmd->sc = sc; /* backpointer for callbacks */ 547 cmd->msgid = i; 548 cmd->buf = ((uint8_t *)sc->sc_cmd_dma_buf) + 549 (i * UATH_MAX_CMDSZ); 550 STAILQ_INSERT_TAIL(&sc->sc_cmd_inactive, cmd, next); 551 UATH_STAT_INC(sc, st_cmd_inactive); 552 } 553 return (0); 554 } 555 556 static int 557 uath_host_available(struct uath_softc *sc) 558 { 559 struct uath_cmd_host_available setup; 560 561 UATH_ASSERT_LOCKED(sc); 562 563 /* inform target the host is available */ 564 setup.sw_ver_major = htobe32(ATH_SW_VER_MAJOR); 565 setup.sw_ver_minor = htobe32(ATH_SW_VER_MINOR); 566 setup.sw_ver_patch = htobe32(ATH_SW_VER_PATCH); 567 setup.sw_ver_build = htobe32(ATH_SW_VER_BUILD); 568 return uath_cmd_read(sc, WDCMSG_HOST_AVAILABLE, 569 &setup, sizeof setup, NULL, 0, 0); 570 } 571 572 #ifdef UATH_DEBUG 573 static void 574 uath_dump_cmd(const uint8_t *buf, int len, char prefix) 575 { 576 const char *sep = ""; 577 int i; 578 579 for (i = 0; i < len; i++) { 580 if ((i % 16) == 0) { 581 printf("%s%c ", sep, prefix); 582 sep = "\n"; 583 } 584 else if ((i % 4) == 0) 585 printf(" "); 586 printf("%02x", buf[i]); 587 } 588 printf("\n"); 589 } 590 591 static const char * 592 uath_codename(int code) 593 { 594 static const char *names[] = { 595 "0x00", 596 "HOST_AVAILABLE", 597 "BIND", 598 "TARGET_RESET", 599 "TARGET_GET_CAPABILITY", 600 "TARGET_SET_CONFIG", 601 "TARGET_GET_STATUS", 602 "TARGET_GET_STATS", 603 "TARGET_START", 604 "TARGET_STOP", 605 "TARGET_ENABLE", 606 "TARGET_DISABLE", 607 "CREATE_CONNECTION", 608 "UPDATE_CONNECT_ATTR", 609 "DELETE_CONNECT", 610 "SEND", 611 "FLUSH", 612 "STATS_UPDATE", 613 "BMISS", 614 "DEVICE_AVAIL", 615 "SEND_COMPLETE", 616 "DATA_AVAIL", 617 "SET_PWR_MODE", 618 "BMISS_ACK", 619 "SET_LED_STEADY", 620 "SET_LED_BLINK", 621 "SETUP_BEACON_DESC", 622 "BEACON_INIT", 623 "RESET_KEY_CACHE", 624 "RESET_KEY_CACHE_ENTRY", 625 "SET_KEY_CACHE_ENTRY", 626 "SET_DECOMP_MASK", 627 "SET_REGULATORY_DOMAIN", 628 "SET_LED_STATE", 629 "WRITE_ASSOCID", 630 "SET_STA_BEACON_TIMERS", 631 "GET_TSF", 632 "RESET_TSF", 633 "SET_ADHOC_MODE", 634 "SET_BASIC_RATE", 635 "MIB_CONTROL", 636 "GET_CHANNEL_DATA", 637 "GET_CUR_RSSI", 638 "SET_ANTENNA_SWITCH", 639 "0x2c", "0x2d", "0x2e", 640 "USE_SHORT_SLOT_TIME", 641 "SET_POWER_MODE", 642 "SETUP_PSPOLL_DESC", 643 "SET_RX_MULTICAST_FILTER", 644 "RX_FILTER", 645 "PER_CALIBRATION", 646 "RESET", 647 "DISABLE", 648 "PHY_DISABLE", 649 "SET_TX_POWER_LIMIT", 650 "SET_TX_QUEUE_PARAMS", 651 "SETUP_TX_QUEUE", 652 "RELEASE_TX_QUEUE", 653 }; 654 static char buf[8]; 655 656 if (code < nitems(names)) 657 return names[code]; 658 if (code == WDCMSG_SET_DEFAULT_KEY) 659 return "SET_DEFAULT_KEY"; 660 snprintf(buf, sizeof(buf), "0x%02x", code); 661 return buf; 662 } 663 #endif 664 665 /* 666 * Low-level function to send read or write commands to the firmware. 667 */ 668 static int 669 uath_cmdsend(struct uath_softc *sc, uint32_t code, const void *idata, int ilen, 670 void *odata, int olen, int flags) 671 { 672 struct uath_cmd_hdr *hdr; 673 struct uath_cmd *cmd; 674 int error; 675 676 UATH_ASSERT_LOCKED(sc); 677 678 /* grab a xfer */ 679 cmd = uath_get_cmdbuf(sc); 680 if (cmd == NULL) { 681 device_printf(sc->sc_dev, "%s: empty inactive queue\n", 682 __func__); 683 return (ENOBUFS); 684 } 685 cmd->flags = flags; 686 /* always bulk-out a multiple of 4 bytes */ 687 cmd->buflen = roundup2(sizeof(struct uath_cmd_hdr) + ilen, 4); 688 689 hdr = (struct uath_cmd_hdr *)cmd->buf; 690 memset(hdr, 0, sizeof(struct uath_cmd_hdr)); 691 hdr->len = htobe32(cmd->buflen); 692 hdr->code = htobe32(code); 693 hdr->msgid = cmd->msgid; /* don't care about endianness */ 694 hdr->magic = htobe32((cmd->flags & UATH_CMD_FLAG_MAGIC) ? 1 << 24 : 0); 695 memcpy((uint8_t *)(hdr + 1), idata, ilen); 696 697 #ifdef UATH_DEBUG 698 if (sc->sc_debug & UATH_DEBUG_CMDS) { 699 printf("%s: send %s [flags 0x%x] olen %d\n", 700 __func__, uath_codename(code), cmd->flags, olen); 701 if (sc->sc_debug & UATH_DEBUG_CMDS_DUMP) 702 uath_dump_cmd(cmd->buf, cmd->buflen, '+'); 703 } 704 #endif 705 cmd->odata = odata; 706 KASSERT(odata == NULL || 707 olen < UATH_MAX_CMDSZ - sizeof(*hdr) + sizeof(uint32_t), 708 ("odata %p olen %u", odata, olen)); 709 cmd->olen = olen; 710 711 STAILQ_INSERT_TAIL(&sc->sc_cmd_pending, cmd, next); 712 UATH_STAT_INC(sc, st_cmd_pending); 713 usbd_transfer_start(sc->sc_xfer[UATH_INTR_TX]); 714 715 if (cmd->flags & UATH_CMD_FLAG_READ) { 716 usbd_transfer_start(sc->sc_xfer[UATH_INTR_RX]); 717 718 /* wait at most two seconds for command reply */ 719 error = mtx_sleep(cmd, &sc->sc_mtx, 0, "uathcmd", 2 * hz); 720 cmd->odata = NULL; /* in case reply comes too late */ 721 if (error != 0) { 722 device_printf(sc->sc_dev, "timeout waiting for reply " 723 "to cmd 0x%x (%u)\n", code, code); 724 } else if (cmd->olen != olen) { 725 device_printf(sc->sc_dev, "unexpected reply data count " 726 "to cmd 0x%x (%u), got %u, expected %u\n", 727 code, code, cmd->olen, olen); 728 error = EINVAL; 729 } 730 return (error); 731 } 732 return (0); 733 } 734 735 static int 736 uath_cmd_read(struct uath_softc *sc, uint32_t code, const void *idata, 737 int ilen, void *odata, int olen, int flags) 738 { 739 740 flags |= UATH_CMD_FLAG_READ; 741 return uath_cmdsend(sc, code, idata, ilen, odata, olen, flags); 742 } 743 744 static int 745 uath_cmd_write(struct uath_softc *sc, uint32_t code, const void *data, int len, 746 int flags) 747 { 748 749 flags &= ~UATH_CMD_FLAG_READ; 750 return uath_cmdsend(sc, code, data, len, NULL, 0, flags); 751 } 752 753 static struct uath_cmd * 754 uath_get_cmdbuf(struct uath_softc *sc) 755 { 756 struct uath_cmd *uc; 757 758 UATH_ASSERT_LOCKED(sc); 759 760 uc = STAILQ_FIRST(&sc->sc_cmd_inactive); 761 if (uc != NULL) { 762 STAILQ_REMOVE_HEAD(&sc->sc_cmd_inactive, next); 763 UATH_STAT_DEC(sc, st_cmd_inactive); 764 } else 765 uc = NULL; 766 if (uc == NULL) 767 DPRINTF(sc, UATH_DEBUG_XMIT, "%s: %s\n", __func__, 768 "out of command xmit buffers"); 769 return (uc); 770 } 771 772 /* 773 * This function is called periodically (every second) when associated to 774 * query device statistics. 775 */ 776 static void 777 uath_stat(void *arg) 778 { 779 struct uath_softc *sc = arg; 780 int error; 781 782 UATH_LOCK(sc); 783 /* 784 * Send request for statistics asynchronously. The timer will be 785 * restarted when we'll get the stats notification. 786 */ 787 error = uath_cmd_write(sc, WDCMSG_TARGET_GET_STATS, NULL, 0, 788 UATH_CMD_FLAG_ASYNC); 789 if (error != 0) { 790 device_printf(sc->sc_dev, 791 "could not query stats, error %d\n", error); 792 } 793 UATH_UNLOCK(sc); 794 } 795 796 static int 797 uath_get_capability(struct uath_softc *sc, uint32_t cap, uint32_t *val) 798 { 799 int error; 800 801 cap = htobe32(cap); 802 error = uath_cmd_read(sc, WDCMSG_TARGET_GET_CAPABILITY, 803 &cap, sizeof cap, val, sizeof(uint32_t), UATH_CMD_FLAG_MAGIC); 804 if (error != 0) { 805 device_printf(sc->sc_dev, "could not read capability %u\n", 806 be32toh(cap)); 807 return (error); 808 } 809 *val = be32toh(*val); 810 return (error); 811 } 812 813 static int 814 uath_get_devcap(struct uath_softc *sc) 815 { 816 #define GETCAP(x, v) do { \ 817 error = uath_get_capability(sc, x, &v); \ 818 if (error != 0) \ 819 return (error); \ 820 DPRINTF(sc, UATH_DEBUG_DEVCAP, \ 821 "%s: %s=0x%08x\n", __func__, #x, v); \ 822 } while (0) 823 struct uath_devcap *cap = &sc->sc_devcap; 824 int error; 825 826 /* collect device capabilities */ 827 GETCAP(CAP_TARGET_VERSION, cap->targetVersion); 828 GETCAP(CAP_TARGET_REVISION, cap->targetRevision); 829 GETCAP(CAP_MAC_VERSION, cap->macVersion); 830 GETCAP(CAP_MAC_REVISION, cap->macRevision); 831 GETCAP(CAP_PHY_REVISION, cap->phyRevision); 832 GETCAP(CAP_ANALOG_5GHz_REVISION, cap->analog5GhzRevision); 833 GETCAP(CAP_ANALOG_2GHz_REVISION, cap->analog2GhzRevision); 834 835 GETCAP(CAP_REG_DOMAIN, cap->regDomain); 836 GETCAP(CAP_REG_CAP_BITS, cap->regCapBits); 837 #if 0 838 /* NB: not supported in rev 1.5 */ 839 GETCAP(CAP_COUNTRY_CODE, cap->countryCode); 840 #endif 841 GETCAP(CAP_WIRELESS_MODES, cap->wirelessModes); 842 GETCAP(CAP_CHAN_SPREAD_SUPPORT, cap->chanSpreadSupport); 843 GETCAP(CAP_COMPRESS_SUPPORT, cap->compressSupport); 844 GETCAP(CAP_BURST_SUPPORT, cap->burstSupport); 845 GETCAP(CAP_FAST_FRAMES_SUPPORT, cap->fastFramesSupport); 846 GETCAP(CAP_CHAP_TUNING_SUPPORT, cap->chapTuningSupport); 847 GETCAP(CAP_TURBOG_SUPPORT, cap->turboGSupport); 848 GETCAP(CAP_TURBO_PRIME_SUPPORT, cap->turboPrimeSupport); 849 GETCAP(CAP_DEVICE_TYPE, cap->deviceType); 850 GETCAP(CAP_WME_SUPPORT, cap->wmeSupport); 851 GETCAP(CAP_TOTAL_QUEUES, cap->numTxQueues); 852 GETCAP(CAP_CONNECTION_ID_MAX, cap->connectionIdMax); 853 854 GETCAP(CAP_LOW_5GHZ_CHAN, cap->low5GhzChan); 855 GETCAP(CAP_HIGH_5GHZ_CHAN, cap->high5GhzChan); 856 GETCAP(CAP_LOW_2GHZ_CHAN, cap->low2GhzChan); 857 GETCAP(CAP_HIGH_2GHZ_CHAN, cap->high2GhzChan); 858 GETCAP(CAP_TWICE_ANTENNAGAIN_5G, cap->twiceAntennaGain5G); 859 GETCAP(CAP_TWICE_ANTENNAGAIN_2G, cap->twiceAntennaGain2G); 860 861 GETCAP(CAP_CIPHER_AES_CCM, cap->supportCipherAES_CCM); 862 GETCAP(CAP_CIPHER_TKIP, cap->supportCipherTKIP); 863 GETCAP(CAP_MIC_TKIP, cap->supportMicTKIP); 864 865 cap->supportCipherWEP = 1; /* NB: always available */ 866 867 return (0); 868 } 869 870 static int 871 uath_get_devstatus(struct uath_softc *sc, uint8_t macaddr[IEEE80211_ADDR_LEN]) 872 { 873 int error; 874 875 /* retrieve MAC address */ 876 error = uath_get_status(sc, ST_MAC_ADDR, macaddr, IEEE80211_ADDR_LEN); 877 if (error != 0) { 878 device_printf(sc->sc_dev, "could not read MAC address\n"); 879 return (error); 880 } 881 882 error = uath_get_status(sc, ST_SERIAL_NUMBER, 883 &sc->sc_serial[0], sizeof(sc->sc_serial)); 884 if (error != 0) { 885 device_printf(sc->sc_dev, 886 "could not read device serial number\n"); 887 return (error); 888 } 889 return (0); 890 } 891 892 static int 893 uath_get_status(struct uath_softc *sc, uint32_t which, void *odata, int olen) 894 { 895 int error; 896 897 which = htobe32(which); 898 error = uath_cmd_read(sc, WDCMSG_TARGET_GET_STATUS, 899 &which, sizeof(which), odata, olen, UATH_CMD_FLAG_MAGIC); 900 if (error != 0) 901 device_printf(sc->sc_dev, 902 "could not read EEPROM offset 0x%02x\n", be32toh(which)); 903 return (error); 904 } 905 906 static void 907 uath_free_data_list(struct uath_softc *sc, struct uath_data data[], int ndata, 908 int fillmbuf) 909 { 910 int i; 911 912 for (i = 0; i < ndata; i++) { 913 struct uath_data *dp = &data[i]; 914 915 if (fillmbuf == 1) { 916 if (dp->m != NULL) { 917 m_freem(dp->m); 918 dp->m = NULL; 919 dp->buf = NULL; 920 } 921 } else { 922 dp->buf = NULL; 923 } 924 if (dp->ni != NULL) { 925 ieee80211_free_node(dp->ni); 926 dp->ni = NULL; 927 } 928 } 929 } 930 931 static int 932 uath_alloc_data_list(struct uath_softc *sc, struct uath_data data[], 933 int ndata, int maxsz, void *dma_buf) 934 { 935 int i, error; 936 937 for (i = 0; i < ndata; i++) { 938 struct uath_data *dp = &data[i]; 939 940 dp->sc = sc; 941 if (dma_buf == NULL) { 942 /* XXX check maxsz */ 943 dp->m = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 944 if (dp->m == NULL) { 945 device_printf(sc->sc_dev, 946 "could not allocate rx mbuf\n"); 947 error = ENOMEM; 948 goto fail; 949 } 950 dp->buf = mtod(dp->m, uint8_t *); 951 } else { 952 dp->m = NULL; 953 dp->buf = ((uint8_t *)dma_buf) + (i * maxsz); 954 } 955 dp->ni = NULL; 956 } 957 958 return (0); 959 960 fail: uath_free_data_list(sc, data, ndata, 1 /* free mbufs */); 961 return (error); 962 } 963 964 static int 965 uath_alloc_rx_data_list(struct uath_softc *sc) 966 { 967 int error, i; 968 969 /* XXX is it enough to store the RX packet with MCLBYTES bytes? */ 970 error = uath_alloc_data_list(sc, 971 sc->sc_rx, UATH_RX_DATA_LIST_COUNT, MCLBYTES, 972 NULL /* setup mbufs */); 973 if (error != 0) 974 return (error); 975 976 STAILQ_INIT(&sc->sc_rx_active); 977 STAILQ_INIT(&sc->sc_rx_inactive); 978 979 for (i = 0; i < UATH_RX_DATA_LIST_COUNT; i++) { 980 STAILQ_INSERT_HEAD(&sc->sc_rx_inactive, &sc->sc_rx[i], 981 next); 982 UATH_STAT_INC(sc, st_rx_inactive); 983 } 984 985 return (0); 986 } 987 988 static int 989 uath_alloc_tx_data_list(struct uath_softc *sc) 990 { 991 int error, i; 992 993 error = uath_alloc_data_list(sc, 994 sc->sc_tx, UATH_TX_DATA_LIST_COUNT, UATH_MAX_TXBUFSZ, 995 sc->sc_tx_dma_buf); 996 if (error != 0) 997 return (error); 998 999 STAILQ_INIT(&sc->sc_tx_active); 1000 STAILQ_INIT(&sc->sc_tx_inactive); 1001 STAILQ_INIT(&sc->sc_tx_pending); 1002 1003 for (i = 0; i < UATH_TX_DATA_LIST_COUNT; i++) { 1004 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, &sc->sc_tx[i], 1005 next); 1006 UATH_STAT_INC(sc, st_tx_inactive); 1007 } 1008 1009 return (0); 1010 } 1011 1012 static void 1013 uath_free_rx_data_list(struct uath_softc *sc) 1014 { 1015 uath_free_data_list(sc, sc->sc_rx, UATH_RX_DATA_LIST_COUNT, 1016 1 /* free mbufs */); 1017 } 1018 1019 static void 1020 uath_free_tx_data_list(struct uath_softc *sc) 1021 { 1022 uath_free_data_list(sc, sc->sc_tx, UATH_TX_DATA_LIST_COUNT, 1023 0 /* no mbufs */); 1024 } 1025 1026 static struct ieee80211vap * 1027 uath_vap_create(struct ieee80211com *ic, const char name[IFNAMSIZ], int unit, 1028 enum ieee80211_opmode opmode, int flags, 1029 const uint8_t bssid[IEEE80211_ADDR_LEN], 1030 const uint8_t mac[IEEE80211_ADDR_LEN]) 1031 { 1032 struct uath_vap *uvp; 1033 struct ieee80211vap *vap; 1034 1035 if (!TAILQ_EMPTY(&ic->ic_vaps)) /* only one at a time */ 1036 return (NULL); 1037 uvp = malloc(sizeof(struct uath_vap), M_80211_VAP, M_WAITOK | M_ZERO); 1038 vap = &uvp->vap; 1039 /* enable s/w bmiss handling for sta mode */ 1040 1041 if (ieee80211_vap_setup(ic, vap, name, unit, opmode, 1042 flags | IEEE80211_CLONE_NOBEACONS, bssid) != 0) { 1043 /* out of memory */ 1044 free(uvp, M_80211_VAP); 1045 return (NULL); 1046 } 1047 1048 /* override state transition machine */ 1049 uvp->newstate = vap->iv_newstate; 1050 vap->iv_newstate = uath_newstate; 1051 1052 /* complete setup */ 1053 ieee80211_vap_attach(vap, ieee80211_media_change, 1054 ieee80211_media_status, mac); 1055 ic->ic_opmode = opmode; 1056 return (vap); 1057 } 1058 1059 static void 1060 uath_vap_delete(struct ieee80211vap *vap) 1061 { 1062 struct uath_vap *uvp = UATH_VAP(vap); 1063 1064 ieee80211_vap_detach(vap); 1065 free(uvp, M_80211_VAP); 1066 } 1067 1068 static int 1069 uath_init(struct uath_softc *sc) 1070 { 1071 struct ieee80211com *ic = &sc->sc_ic; 1072 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1073 uint32_t val; 1074 int error; 1075 1076 UATH_ASSERT_LOCKED(sc); 1077 1078 if (sc->sc_flags & UATH_FLAG_INITDONE) 1079 uath_stop(sc); 1080 1081 /* reset variables */ 1082 sc->sc_intrx_nextnum = sc->sc_msgid = 0; 1083 1084 val = htobe32(0); 1085 uath_cmd_write(sc, WDCMSG_BIND, &val, sizeof val, 0); 1086 1087 /* set MAC address */ 1088 uath_config_multi(sc, CFG_MAC_ADDR, 1089 vap ? vap->iv_myaddr : ic->ic_macaddr, IEEE80211_ADDR_LEN); 1090 1091 /* XXX honor net80211 state */ 1092 uath_config(sc, CFG_RATE_CONTROL_ENABLE, 0x00000001); 1093 uath_config(sc, CFG_DIVERSITY_CTL, 0x00000001); 1094 uath_config(sc, CFG_ABOLT, 0x0000003f); 1095 uath_config(sc, CFG_WME_ENABLED, 0x00000001); 1096 1097 uath_config(sc, CFG_SERVICE_TYPE, 1); 1098 uath_config(sc, CFG_TP_SCALE, 0x00000000); 1099 uath_config(sc, CFG_TPC_HALF_DBM5, 0x0000003c); 1100 uath_config(sc, CFG_TPC_HALF_DBM2, 0x0000003c); 1101 uath_config(sc, CFG_OVERRD_TX_POWER, 0x00000000); 1102 uath_config(sc, CFG_GMODE_PROTECTION, 0x00000000); 1103 uath_config(sc, CFG_GMODE_PROTECT_RATE_INDEX, 0x00000003); 1104 uath_config(sc, CFG_PROTECTION_TYPE, 0x00000000); 1105 uath_config(sc, CFG_MODE_CTS, 0x00000002); 1106 1107 error = uath_cmd_read(sc, WDCMSG_TARGET_START, NULL, 0, 1108 &val, sizeof(val), UATH_CMD_FLAG_MAGIC); 1109 if (error) { 1110 device_printf(sc->sc_dev, 1111 "could not start target, error %d\n", error); 1112 goto fail; 1113 } 1114 DPRINTF(sc, UATH_DEBUG_INIT, "%s returns handle: 0x%x\n", 1115 uath_codename(WDCMSG_TARGET_START), be32toh(val)); 1116 1117 /* set default channel */ 1118 error = uath_switch_channel(sc, ic->ic_curchan); 1119 if (error) { 1120 device_printf(sc->sc_dev, 1121 "could not switch channel, error %d\n", error); 1122 goto fail; 1123 } 1124 1125 val = htobe32(TARGET_DEVICE_AWAKE); 1126 uath_cmd_write(sc, WDCMSG_SET_PWR_MODE, &val, sizeof val, 0); 1127 /* XXX? check */ 1128 uath_cmd_write(sc, WDCMSG_RESET_KEY_CACHE, NULL, 0, 0); 1129 1130 usbd_transfer_start(sc->sc_xfer[UATH_BULK_RX]); 1131 /* enable Rx */ 1132 uath_set_rxfilter(sc, 0x0, UATH_FILTER_OP_INIT); 1133 uath_set_rxfilter(sc, 1134 UATH_FILTER_RX_UCAST | UATH_FILTER_RX_MCAST | 1135 UATH_FILTER_RX_BCAST | UATH_FILTER_RX_BEACON, 1136 UATH_FILTER_OP_SET); 1137 1138 sc->sc_flags |= UATH_FLAG_INITDONE; 1139 1140 callout_reset(&sc->watchdog_ch, hz, uath_watchdog, sc); 1141 1142 return (0); 1143 1144 fail: 1145 uath_stop(sc); 1146 return (error); 1147 } 1148 1149 static void 1150 uath_stop(struct uath_softc *sc) 1151 { 1152 1153 UATH_ASSERT_LOCKED(sc); 1154 1155 sc->sc_flags &= ~UATH_FLAG_INITDONE; 1156 1157 callout_stop(&sc->stat_ch); 1158 callout_stop(&sc->watchdog_ch); 1159 sc->sc_tx_timer = 0; 1160 /* abort pending transmits */ 1161 uath_abort_xfers(sc); 1162 /* flush data & control requests into the target */ 1163 (void)uath_flush(sc); 1164 /* set a LED status to the disconnected. */ 1165 uath_set_ledstate(sc, 0); 1166 /* stop the target */ 1167 uath_cmd_write(sc, WDCMSG_TARGET_STOP, NULL, 0, 0); 1168 } 1169 1170 static int 1171 uath_config(struct uath_softc *sc, uint32_t reg, uint32_t val) 1172 { 1173 struct uath_write_mac write; 1174 int error; 1175 1176 write.reg = htobe32(reg); 1177 write.len = htobe32(0); /* 0 = single write */ 1178 *(uint32_t *)write.data = htobe32(val); 1179 1180 error = uath_cmd_write(sc, WDCMSG_TARGET_SET_CONFIG, &write, 1181 3 * sizeof (uint32_t), 0); 1182 if (error != 0) { 1183 device_printf(sc->sc_dev, "could not write register 0x%02x\n", 1184 reg); 1185 } 1186 return (error); 1187 } 1188 1189 static int 1190 uath_config_multi(struct uath_softc *sc, uint32_t reg, const void *data, 1191 int len) 1192 { 1193 struct uath_write_mac write; 1194 int error; 1195 1196 write.reg = htobe32(reg); 1197 write.len = htobe32(len); 1198 bcopy(data, write.data, len); 1199 1200 /* properly handle the case where len is zero (reset) */ 1201 error = uath_cmd_write(sc, WDCMSG_TARGET_SET_CONFIG, &write, 1202 (len == 0) ? sizeof (uint32_t) : 2 * sizeof (uint32_t) + len, 0); 1203 if (error != 0) { 1204 device_printf(sc->sc_dev, 1205 "could not write %d bytes to register 0x%02x\n", len, reg); 1206 } 1207 return (error); 1208 } 1209 1210 static int 1211 uath_switch_channel(struct uath_softc *sc, struct ieee80211_channel *c) 1212 { 1213 int error; 1214 1215 UATH_ASSERT_LOCKED(sc); 1216 1217 /* set radio frequency */ 1218 error = uath_set_chan(sc, c); 1219 if (error) { 1220 device_printf(sc->sc_dev, 1221 "could not set channel, error %d\n", error); 1222 goto failed; 1223 } 1224 /* reset Tx rings */ 1225 error = uath_reset_tx_queues(sc); 1226 if (error) { 1227 device_printf(sc->sc_dev, 1228 "could not reset Tx queues, error %d\n", error); 1229 goto failed; 1230 } 1231 /* set Tx rings WME properties */ 1232 error = uath_wme_init(sc); 1233 if (error) { 1234 device_printf(sc->sc_dev, 1235 "could not init Tx queues, error %d\n", error); 1236 goto failed; 1237 } 1238 error = uath_set_ledstate(sc, 0); 1239 if (error) { 1240 device_printf(sc->sc_dev, 1241 "could not set led state, error %d\n", error); 1242 goto failed; 1243 } 1244 error = uath_flush(sc); 1245 if (error) { 1246 device_printf(sc->sc_dev, 1247 "could not flush pipes, error %d\n", error); 1248 goto failed; 1249 } 1250 failed: 1251 return (error); 1252 } 1253 1254 static int 1255 uath_set_rxfilter(struct uath_softc *sc, uint32_t bits, uint32_t op) 1256 { 1257 struct uath_cmd_rx_filter rxfilter; 1258 1259 rxfilter.bits = htobe32(bits); 1260 rxfilter.op = htobe32(op); 1261 1262 DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL, 1263 "setting Rx filter=0x%x flags=0x%x\n", bits, op); 1264 return uath_cmd_write(sc, WDCMSG_RX_FILTER, &rxfilter, 1265 sizeof rxfilter, 0); 1266 } 1267 1268 static void 1269 uath_watchdog(void *arg) 1270 { 1271 struct uath_softc *sc = arg; 1272 struct ieee80211com *ic = &sc->sc_ic; 1273 1274 if (sc->sc_tx_timer > 0) { 1275 if (--sc->sc_tx_timer == 0) { 1276 device_printf(sc->sc_dev, "device timeout\n"); 1277 /*uath_init(sc); XXX needs a process context! */ 1278 counter_u64_add(ic->ic_oerrors, 1); 1279 return; 1280 } 1281 callout_reset(&sc->watchdog_ch, hz, uath_watchdog, sc); 1282 } 1283 } 1284 1285 static void 1286 uath_abort_xfers(struct uath_softc *sc) 1287 { 1288 int i; 1289 1290 UATH_ASSERT_LOCKED(sc); 1291 /* abort any pending transfers */ 1292 for (i = 0; i < UATH_N_XFERS; i++) 1293 usbd_transfer_stop(sc->sc_xfer[i]); 1294 } 1295 1296 static int 1297 uath_flush(struct uath_softc *sc) 1298 { 1299 int error; 1300 1301 error = uath_dataflush(sc); 1302 if (error != 0) 1303 goto failed; 1304 1305 error = uath_cmdflush(sc); 1306 if (error != 0) 1307 goto failed; 1308 1309 failed: 1310 return (error); 1311 } 1312 1313 static int 1314 uath_cmdflush(struct uath_softc *sc) 1315 { 1316 1317 return uath_cmd_write(sc, WDCMSG_FLUSH, NULL, 0, 0); 1318 } 1319 1320 static int 1321 uath_dataflush(struct uath_softc *sc) 1322 { 1323 struct uath_data *data; 1324 struct uath_chunk *chunk; 1325 struct uath_tx_desc *desc; 1326 1327 UATH_ASSERT_LOCKED(sc); 1328 1329 data = uath_getbuf(sc); 1330 if (data == NULL) 1331 return (ENOBUFS); 1332 data->buflen = sizeof(struct uath_chunk) + sizeof(struct uath_tx_desc); 1333 data->m = NULL; 1334 data->ni = NULL; 1335 chunk = (struct uath_chunk *)data->buf; 1336 desc = (struct uath_tx_desc *)(chunk + 1); 1337 1338 /* one chunk only */ 1339 chunk->seqnum = 0; 1340 chunk->flags = UATH_CFLAGS_FINAL; 1341 chunk->length = htobe16(sizeof (struct uath_tx_desc)); 1342 1343 memset(desc, 0, sizeof(struct uath_tx_desc)); 1344 desc->msglen = htobe32(sizeof(struct uath_tx_desc)); 1345 desc->msgid = (sc->sc_msgid++) + 1; /* don't care about endianness */ 1346 desc->type = htobe32(WDCMSG_FLUSH); 1347 desc->txqid = htobe32(0); 1348 desc->connid = htobe32(0); 1349 desc->flags = htobe32(0); 1350 1351 #ifdef UATH_DEBUG 1352 if (sc->sc_debug & UATH_DEBUG_CMDS) { 1353 DPRINTF(sc, UATH_DEBUG_RESET, "send flush ix %d\n", 1354 desc->msgid); 1355 if (sc->sc_debug & UATH_DEBUG_CMDS_DUMP) 1356 uath_dump_cmd(data->buf, data->buflen, '+'); 1357 } 1358 #endif 1359 1360 STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next); 1361 UATH_STAT_INC(sc, st_tx_pending); 1362 sc->sc_tx_timer = 5; 1363 usbd_transfer_start(sc->sc_xfer[UATH_BULK_TX]); 1364 1365 return (0); 1366 } 1367 1368 static struct uath_data * 1369 _uath_getbuf(struct uath_softc *sc) 1370 { 1371 struct uath_data *bf; 1372 1373 bf = STAILQ_FIRST(&sc->sc_tx_inactive); 1374 if (bf != NULL) { 1375 STAILQ_REMOVE_HEAD(&sc->sc_tx_inactive, next); 1376 UATH_STAT_DEC(sc, st_tx_inactive); 1377 } else 1378 bf = NULL; 1379 if (bf == NULL) 1380 DPRINTF(sc, UATH_DEBUG_XMIT, "%s: %s\n", __func__, 1381 "out of xmit buffers"); 1382 return (bf); 1383 } 1384 1385 static struct uath_data * 1386 uath_getbuf(struct uath_softc *sc) 1387 { 1388 struct uath_data *bf; 1389 1390 UATH_ASSERT_LOCKED(sc); 1391 1392 bf = _uath_getbuf(sc); 1393 if (bf == NULL) 1394 DPRINTF(sc, UATH_DEBUG_XMIT, "%s: stop queue\n", __func__); 1395 return (bf); 1396 } 1397 1398 static int 1399 uath_set_ledstate(struct uath_softc *sc, int connected) 1400 { 1401 1402 DPRINTF(sc, UATH_DEBUG_LED, 1403 "set led state %sconnected\n", connected ? "" : "!"); 1404 connected = htobe32(connected); 1405 return uath_cmd_write(sc, WDCMSG_SET_LED_STATE, 1406 &connected, sizeof connected, 0); 1407 } 1408 1409 static int 1410 uath_set_chan(struct uath_softc *sc, struct ieee80211_channel *c) 1411 { 1412 #ifdef UATH_DEBUG 1413 struct ieee80211com *ic = &sc->sc_ic; 1414 #endif 1415 struct uath_cmd_reset reset; 1416 1417 memset(&reset, 0, sizeof(reset)); 1418 if (IEEE80211_IS_CHAN_2GHZ(c)) 1419 reset.flags |= htobe32(UATH_CHAN_2GHZ); 1420 if (IEEE80211_IS_CHAN_5GHZ(c)) 1421 reset.flags |= htobe32(UATH_CHAN_5GHZ); 1422 /* NB: 11g =>'s 11b so don't specify both OFDM and CCK */ 1423 if (IEEE80211_IS_CHAN_OFDM(c)) 1424 reset.flags |= htobe32(UATH_CHAN_OFDM); 1425 else if (IEEE80211_IS_CHAN_CCK(c)) 1426 reset.flags |= htobe32(UATH_CHAN_CCK); 1427 /* turbo can be used in either 2GHz or 5GHz */ 1428 if (c->ic_flags & IEEE80211_CHAN_TURBO) 1429 reset.flags |= htobe32(UATH_CHAN_TURBO); 1430 reset.freq = htobe32(c->ic_freq); 1431 reset.maxrdpower = htobe32(50); /* XXX */ 1432 reset.channelchange = htobe32(1); 1433 reset.keeprccontent = htobe32(0); 1434 1435 DPRINTF(sc, UATH_DEBUG_CHANNEL, "set channel %d, flags 0x%x freq %u\n", 1436 ieee80211_chan2ieee(ic, c), 1437 be32toh(reset.flags), be32toh(reset.freq)); 1438 return uath_cmd_write(sc, WDCMSG_RESET, &reset, sizeof reset, 0); 1439 } 1440 1441 static int 1442 uath_reset_tx_queues(struct uath_softc *sc) 1443 { 1444 int ac, error; 1445 1446 DPRINTF(sc, UATH_DEBUG_RESET, "%s: reset Tx queues\n", __func__); 1447 for (ac = 0; ac < 4; ac++) { 1448 const uint32_t qid = htobe32(ac); 1449 1450 error = uath_cmd_write(sc, WDCMSG_RELEASE_TX_QUEUE, &qid, 1451 sizeof qid, 0); 1452 if (error != 0) 1453 break; 1454 } 1455 return (error); 1456 } 1457 1458 static int 1459 uath_wme_init(struct uath_softc *sc) 1460 { 1461 /* XXX get from net80211 */ 1462 static const struct uath_wme_settings uath_wme_11g[4] = { 1463 { 7, 4, 10, 0, 0 }, /* Background */ 1464 { 3, 4, 10, 0, 0 }, /* Best-Effort */ 1465 { 3, 3, 4, 26, 0 }, /* Video */ 1466 { 2, 2, 3, 47, 0 } /* Voice */ 1467 }; 1468 struct uath_cmd_txq_setup qinfo; 1469 int ac, error; 1470 1471 DPRINTF(sc, UATH_DEBUG_WME, "%s: setup Tx queues\n", __func__); 1472 for (ac = 0; ac < 4; ac++) { 1473 qinfo.qid = htobe32(ac); 1474 qinfo.len = htobe32(sizeof(qinfo.attr)); 1475 qinfo.attr.priority = htobe32(ac); /* XXX */ 1476 qinfo.attr.aifs = htobe32(uath_wme_11g[ac].aifsn); 1477 qinfo.attr.logcwmin = htobe32(uath_wme_11g[ac].logcwmin); 1478 qinfo.attr.logcwmax = htobe32(uath_wme_11g[ac].logcwmax); 1479 qinfo.attr.bursttime = htobe32(IEEE80211_TXOP_TO_US( 1480 uath_wme_11g[ac].txop)); 1481 qinfo.attr.mode = htobe32(uath_wme_11g[ac].acm);/*XXX? */ 1482 qinfo.attr.qflags = htobe32(1); /* XXX? */ 1483 1484 error = uath_cmd_write(sc, WDCMSG_SETUP_TX_QUEUE, &qinfo, 1485 sizeof qinfo, 0); 1486 if (error != 0) 1487 break; 1488 } 1489 return (error); 1490 } 1491 1492 static void 1493 uath_parent(struct ieee80211com *ic) 1494 { 1495 struct uath_softc *sc = ic->ic_softc; 1496 int startall = 0; 1497 1498 UATH_LOCK(sc); 1499 if (sc->sc_flags & UATH_FLAG_INVALID) { 1500 UATH_UNLOCK(sc); 1501 return; 1502 } 1503 1504 if (ic->ic_nrunning > 0) { 1505 if (!(sc->sc_flags & UATH_FLAG_INITDONE)) { 1506 uath_init(sc); 1507 startall = 1; 1508 } 1509 } else if (sc->sc_flags & UATH_FLAG_INITDONE) 1510 uath_stop(sc); 1511 UATH_UNLOCK(sc); 1512 if (startall) 1513 ieee80211_start_all(ic); 1514 } 1515 1516 static int 1517 uath_tx_start(struct uath_softc *sc, struct mbuf *m0, struct ieee80211_node *ni, 1518 struct uath_data *data) 1519 { 1520 struct ieee80211vap *vap = ni->ni_vap; 1521 struct uath_chunk *chunk; 1522 struct uath_tx_desc *desc; 1523 const struct ieee80211_frame *wh; 1524 struct ieee80211_key *k; 1525 int framelen, msglen; 1526 1527 UATH_ASSERT_LOCKED(sc); 1528 1529 data->ni = ni; 1530 data->m = m0; 1531 chunk = (struct uath_chunk *)data->buf; 1532 desc = (struct uath_tx_desc *)(chunk + 1); 1533 1534 if (ieee80211_radiotap_active_vap(vap)) { 1535 struct uath_tx_radiotap_header *tap = &sc->sc_txtap; 1536 1537 tap->wt_flags = 0; 1538 if (m0->m_flags & M_FRAG) 1539 tap->wt_flags |= IEEE80211_RADIOTAP_F_FRAG; 1540 1541 ieee80211_radiotap_tx(vap, m0); 1542 } 1543 1544 wh = mtod(m0, struct ieee80211_frame *); 1545 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1546 k = ieee80211_crypto_encap(ni, m0); 1547 if (k == NULL) { 1548 m_freem(m0); 1549 return (ENOBUFS); 1550 } 1551 1552 /* packet header may have moved, reset our local pointer */ 1553 wh = mtod(m0, struct ieee80211_frame *); 1554 } 1555 m_copydata(m0, 0, m0->m_pkthdr.len, (uint8_t *)(desc + 1)); 1556 1557 framelen = m0->m_pkthdr.len + IEEE80211_CRC_LEN; 1558 msglen = framelen + sizeof (struct uath_tx_desc); 1559 data->buflen = msglen + sizeof (struct uath_chunk); 1560 1561 /* one chunk only for now */ 1562 chunk->seqnum = sc->sc_seqnum++; 1563 chunk->flags = (m0->m_flags & M_FRAG) ? 0 : UATH_CFLAGS_FINAL; 1564 if (m0->m_flags & M_LASTFRAG) 1565 chunk->flags |= UATH_CFLAGS_FINAL; 1566 chunk->flags = UATH_CFLAGS_FINAL; 1567 chunk->length = htobe16(msglen); 1568 1569 /* fill Tx descriptor */ 1570 desc->msglen = htobe32(msglen); 1571 /* NB: to get UATH_TX_NOTIFY reply, `msgid' must be larger than 0 */ 1572 desc->msgid = (sc->sc_msgid++) + 1; /* don't care about endianness */ 1573 desc->type = htobe32(WDCMSG_SEND); 1574 switch (wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) { 1575 case IEEE80211_FC0_TYPE_CTL: 1576 case IEEE80211_FC0_TYPE_MGT: 1577 /* NB: force all management frames to highest queue */ 1578 if (ni->ni_flags & IEEE80211_NODE_QOS) { 1579 /* NB: force all management frames to highest queue */ 1580 desc->txqid = htobe32(WME_AC_VO | UATH_TXQID_MINRATE); 1581 } else 1582 desc->txqid = htobe32(WME_AC_BE | UATH_TXQID_MINRATE); 1583 break; 1584 case IEEE80211_FC0_TYPE_DATA: 1585 /* XXX multicast frames should honor mcastrate */ 1586 desc->txqid = htobe32(M_WME_GETAC(m0)); 1587 break; 1588 default: 1589 device_printf(sc->sc_dev, "bogus frame type 0x%x (%s)\n", 1590 wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK, __func__); 1591 m_freem(m0); 1592 return (EIO); 1593 } 1594 if (vap->iv_state == IEEE80211_S_AUTH || 1595 vap->iv_state == IEEE80211_S_ASSOC || 1596 vap->iv_state == IEEE80211_S_RUN) 1597 desc->connid = htobe32(UATH_ID_BSS); 1598 else 1599 desc->connid = htobe32(UATH_ID_INVALID); 1600 desc->flags = htobe32(0 /* no UATH_TX_NOTIFY */); 1601 desc->buflen = htobe32(m0->m_pkthdr.len); 1602 1603 #ifdef UATH_DEBUG 1604 DPRINTF(sc, UATH_DEBUG_XMIT, 1605 "send frame ix %u framelen %d msglen %d connid 0x%x txqid 0x%x\n", 1606 desc->msgid, framelen, msglen, be32toh(desc->connid), 1607 be32toh(desc->txqid)); 1608 if (sc->sc_debug & UATH_DEBUG_XMIT_DUMP) 1609 uath_dump_cmd(data->buf, data->buflen, '+'); 1610 #endif 1611 1612 STAILQ_INSERT_TAIL(&sc->sc_tx_pending, data, next); 1613 UATH_STAT_INC(sc, st_tx_pending); 1614 usbd_transfer_start(sc->sc_xfer[UATH_BULK_TX]); 1615 1616 return (0); 1617 } 1618 1619 /* 1620 * Cleanup driver resources when we run out of buffers while processing 1621 * fragments; return the tx buffers allocated and drop node references. 1622 */ 1623 static void 1624 uath_txfrag_cleanup(struct uath_softc *sc, 1625 uath_datahead *frags, struct ieee80211_node *ni) 1626 { 1627 struct uath_data *bf, *next; 1628 1629 UATH_ASSERT_LOCKED(sc); 1630 1631 STAILQ_FOREACH_SAFE(bf, frags, next, next) { 1632 /* NB: bf assumed clean */ 1633 STAILQ_REMOVE_HEAD(frags, next); 1634 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next); 1635 UATH_STAT_INC(sc, st_tx_inactive); 1636 ieee80211_node_decref(ni); 1637 } 1638 } 1639 1640 /* 1641 * Setup xmit of a fragmented frame. Allocate a buffer for each frag and bump 1642 * the node reference count to reflect the held reference to be setup by 1643 * uath_tx_start. 1644 */ 1645 static int 1646 uath_txfrag_setup(struct uath_softc *sc, uath_datahead *frags, 1647 struct mbuf *m0, struct ieee80211_node *ni) 1648 { 1649 struct mbuf *m; 1650 struct uath_data *bf; 1651 1652 UATH_ASSERT_LOCKED(sc); 1653 for (m = m0->m_nextpkt; m != NULL; m = m->m_nextpkt) { 1654 bf = uath_getbuf(sc); 1655 if (bf == NULL) { /* out of buffers, cleanup */ 1656 uath_txfrag_cleanup(sc, frags, ni); 1657 break; 1658 } 1659 ieee80211_node_incref(ni); 1660 STAILQ_INSERT_TAIL(frags, bf, next); 1661 } 1662 1663 return !STAILQ_EMPTY(frags); 1664 } 1665 1666 static int 1667 uath_transmit(struct ieee80211com *ic, struct mbuf *m) 1668 { 1669 struct uath_softc *sc = ic->ic_softc; 1670 int error; 1671 1672 UATH_LOCK(sc); 1673 if ((sc->sc_flags & UATH_FLAG_INITDONE) == 0) { 1674 UATH_UNLOCK(sc); 1675 return (ENXIO); 1676 } 1677 error = mbufq_enqueue(&sc->sc_snd, m); 1678 if (error) { 1679 UATH_UNLOCK(sc); 1680 return (error); 1681 } 1682 uath_start(sc); 1683 UATH_UNLOCK(sc); 1684 1685 return (0); 1686 } 1687 1688 static void 1689 uath_start(struct uath_softc *sc) 1690 { 1691 struct uath_data *bf; 1692 struct ieee80211_node *ni; 1693 struct mbuf *m, *next; 1694 uath_datahead frags; 1695 1696 UATH_ASSERT_LOCKED(sc); 1697 1698 if ((sc->sc_flags & UATH_FLAG_INITDONE) == 0 || 1699 (sc->sc_flags & UATH_FLAG_INVALID)) 1700 return; 1701 1702 while ((m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 1703 bf = uath_getbuf(sc); 1704 if (bf == NULL) { 1705 mbufq_prepend(&sc->sc_snd, m); 1706 break; 1707 } 1708 1709 ni = (struct ieee80211_node *)m->m_pkthdr.rcvif; 1710 m->m_pkthdr.rcvif = NULL; 1711 1712 /* 1713 * Check for fragmentation. If this frame has been broken up 1714 * verify we have enough buffers to send all the fragments 1715 * so all go out or none... 1716 */ 1717 STAILQ_INIT(&frags); 1718 if ((m->m_flags & M_FRAG) && 1719 !uath_txfrag_setup(sc, &frags, m, ni)) { 1720 DPRINTF(sc, UATH_DEBUG_XMIT, 1721 "%s: out of txfrag buffers\n", __func__); 1722 ieee80211_free_mbuf(m); 1723 goto bad; 1724 } 1725 sc->sc_seqnum = 0; 1726 nextfrag: 1727 /* 1728 * Pass the frame to the h/w for transmission. 1729 * Fragmented frames have each frag chained together 1730 * with m_nextpkt. We know there are sufficient uath_data's 1731 * to send all the frags because of work done by 1732 * uath_txfrag_setup. 1733 */ 1734 next = m->m_nextpkt; 1735 if (uath_tx_start(sc, m, ni, bf) != 0) { 1736 bad: 1737 if_inc_counter(ni->ni_vap->iv_ifp, 1738 IFCOUNTER_OERRORS, 1); 1739 reclaim: 1740 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next); 1741 UATH_STAT_INC(sc, st_tx_inactive); 1742 uath_txfrag_cleanup(sc, &frags, ni); 1743 ieee80211_free_node(ni); 1744 continue; 1745 } 1746 1747 if (next != NULL) { 1748 /* 1749 * Beware of state changing between frags. 1750 XXX check sta power-save state? 1751 */ 1752 if (ni->ni_vap->iv_state != IEEE80211_S_RUN) { 1753 DPRINTF(sc, UATH_DEBUG_XMIT, 1754 "%s: flush fragmented packet, state %s\n", 1755 __func__, 1756 ieee80211_state_name[ni->ni_vap->iv_state]); 1757 ieee80211_free_mbuf(next); 1758 goto reclaim; 1759 } 1760 m = next; 1761 bf = STAILQ_FIRST(&frags); 1762 KASSERT(bf != NULL, ("no buf for txfrag")); 1763 STAILQ_REMOVE_HEAD(&frags, next); 1764 goto nextfrag; 1765 } 1766 1767 sc->sc_tx_timer = 5; 1768 } 1769 } 1770 1771 static int 1772 uath_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 1773 const struct ieee80211_bpf_params *params) 1774 { 1775 struct ieee80211com *ic = ni->ni_ic; 1776 struct uath_data *bf; 1777 struct uath_softc *sc = ic->ic_softc; 1778 1779 UATH_LOCK(sc); 1780 /* prevent management frames from being sent if we're not ready */ 1781 if ((sc->sc_flags & UATH_FLAG_INVALID) || 1782 !(sc->sc_flags & UATH_FLAG_INITDONE)) { 1783 m_freem(m); 1784 UATH_UNLOCK(sc); 1785 return (ENETDOWN); 1786 } 1787 1788 /* grab a TX buffer */ 1789 bf = uath_getbuf(sc); 1790 if (bf == NULL) { 1791 m_freem(m); 1792 UATH_UNLOCK(sc); 1793 return (ENOBUFS); 1794 } 1795 1796 sc->sc_seqnum = 0; 1797 if (uath_tx_start(sc, m, ni, bf) != 0) { 1798 STAILQ_INSERT_HEAD(&sc->sc_tx_inactive, bf, next); 1799 UATH_STAT_INC(sc, st_tx_inactive); 1800 UATH_UNLOCK(sc); 1801 return (EIO); 1802 } 1803 UATH_UNLOCK(sc); 1804 1805 sc->sc_tx_timer = 5; 1806 return (0); 1807 } 1808 1809 static void 1810 uath_scan_start(struct ieee80211com *ic) 1811 { 1812 /* do nothing */ 1813 } 1814 1815 static void 1816 uath_scan_end(struct ieee80211com *ic) 1817 { 1818 /* do nothing */ 1819 } 1820 1821 static void 1822 uath_set_channel(struct ieee80211com *ic) 1823 { 1824 struct uath_softc *sc = ic->ic_softc; 1825 1826 UATH_LOCK(sc); 1827 if ((sc->sc_flags & UATH_FLAG_INVALID) || 1828 (sc->sc_flags & UATH_FLAG_INITDONE) == 0) { 1829 UATH_UNLOCK(sc); 1830 return; 1831 } 1832 (void)uath_switch_channel(sc, ic->ic_curchan); 1833 UATH_UNLOCK(sc); 1834 } 1835 1836 static int 1837 uath_set_rxmulti_filter(struct uath_softc *sc) 1838 { 1839 /* XXX broken */ 1840 return (0); 1841 } 1842 static void 1843 uath_update_mcast(struct ieee80211com *ic) 1844 { 1845 struct uath_softc *sc = ic->ic_softc; 1846 1847 UATH_LOCK(sc); 1848 if ((sc->sc_flags & UATH_FLAG_INVALID) || 1849 (sc->sc_flags & UATH_FLAG_INITDONE) == 0) { 1850 UATH_UNLOCK(sc); 1851 return; 1852 } 1853 /* 1854 * this is for avoiding the race condition when we're try to 1855 * connect to the AP with WPA. 1856 */ 1857 if (sc->sc_flags & UATH_FLAG_INITDONE) 1858 (void)uath_set_rxmulti_filter(sc); 1859 UATH_UNLOCK(sc); 1860 } 1861 1862 static void 1863 uath_update_promisc(struct ieee80211com *ic) 1864 { 1865 struct uath_softc *sc = ic->ic_softc; 1866 1867 UATH_LOCK(sc); 1868 if ((sc->sc_flags & UATH_FLAG_INVALID) || 1869 (sc->sc_flags & UATH_FLAG_INITDONE) == 0) { 1870 UATH_UNLOCK(sc); 1871 return; 1872 } 1873 if (sc->sc_flags & UATH_FLAG_INITDONE) { 1874 uath_set_rxfilter(sc, 1875 UATH_FILTER_RX_UCAST | UATH_FILTER_RX_MCAST | 1876 UATH_FILTER_RX_BCAST | UATH_FILTER_RX_BEACON | 1877 UATH_FILTER_RX_PROM, UATH_FILTER_OP_SET); 1878 } 1879 UATH_UNLOCK(sc); 1880 } 1881 1882 static int 1883 uath_create_connection(struct uath_softc *sc, uint32_t connid) 1884 { 1885 const struct ieee80211_rateset *rs; 1886 struct ieee80211com *ic = &sc->sc_ic; 1887 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1888 struct ieee80211_node *ni; 1889 struct uath_cmd_create_connection create; 1890 1891 ni = ieee80211_ref_node(vap->iv_bss); 1892 memset(&create, 0, sizeof(create)); 1893 create.connid = htobe32(connid); 1894 create.bssid = htobe32(0); 1895 /* XXX packed or not? */ 1896 create.size = htobe32(sizeof(struct uath_cmd_rateset)); 1897 1898 rs = &ni->ni_rates; 1899 create.connattr.rateset.length = rs->rs_nrates; 1900 bcopy(rs->rs_rates, &create.connattr.rateset.set[0], 1901 rs->rs_nrates); 1902 1903 /* XXX turbo */ 1904 if (IEEE80211_IS_CHAN_A(ni->ni_chan)) 1905 create.connattr.wlanmode = htobe32(WLAN_MODE_11a); 1906 else if (IEEE80211_IS_CHAN_ANYG(ni->ni_chan)) 1907 create.connattr.wlanmode = htobe32(WLAN_MODE_11g); 1908 else 1909 create.connattr.wlanmode = htobe32(WLAN_MODE_11b); 1910 ieee80211_free_node(ni); 1911 1912 return uath_cmd_write(sc, WDCMSG_CREATE_CONNECTION, &create, 1913 sizeof create, 0); 1914 } 1915 1916 static int 1917 uath_set_rates(struct uath_softc *sc, const struct ieee80211_rateset *rs) 1918 { 1919 struct uath_cmd_rates rates; 1920 1921 memset(&rates, 0, sizeof(rates)); 1922 rates.connid = htobe32(UATH_ID_BSS); /* XXX */ 1923 rates.size = htobe32(sizeof(struct uath_cmd_rateset)); 1924 /* XXX bounds check rs->rs_nrates */ 1925 rates.rateset.length = rs->rs_nrates; 1926 bcopy(rs->rs_rates, &rates.rateset.set[0], rs->rs_nrates); 1927 1928 DPRINTF(sc, UATH_DEBUG_RATES, 1929 "setting supported rates nrates=%d\n", rs->rs_nrates); 1930 return uath_cmd_write(sc, WDCMSG_SET_BASIC_RATE, 1931 &rates, sizeof rates, 0); 1932 } 1933 1934 static int 1935 uath_write_associd(struct uath_softc *sc) 1936 { 1937 struct ieee80211com *ic = &sc->sc_ic; 1938 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1939 struct ieee80211_node *ni; 1940 struct uath_cmd_set_associd associd; 1941 1942 ni = ieee80211_ref_node(vap->iv_bss); 1943 memset(&associd, 0, sizeof(associd)); 1944 associd.defaultrateix = htobe32(1); /* XXX */ 1945 associd.associd = htobe32(ni->ni_associd); 1946 associd.timoffset = htobe32(0x3b); /* XXX */ 1947 IEEE80211_ADDR_COPY(associd.bssid, ni->ni_bssid); 1948 ieee80211_free_node(ni); 1949 return uath_cmd_write(sc, WDCMSG_WRITE_ASSOCID, &associd, 1950 sizeof associd, 0); 1951 } 1952 1953 static int 1954 uath_set_ledsteady(struct uath_softc *sc, int lednum, int ledmode) 1955 { 1956 struct uath_cmd_ledsteady led; 1957 1958 led.lednum = htobe32(lednum); 1959 led.ledmode = htobe32(ledmode); 1960 1961 DPRINTF(sc, UATH_DEBUG_LED, "set %s led %s (steady)\n", 1962 (lednum == UATH_LED_LINK) ? "link" : "activity", 1963 ledmode ? "on" : "off"); 1964 return uath_cmd_write(sc, WDCMSG_SET_LED_STEADY, &led, sizeof led, 0); 1965 } 1966 1967 static int 1968 uath_set_ledblink(struct uath_softc *sc, int lednum, int ledmode, 1969 int blinkrate, int slowmode) 1970 { 1971 struct uath_cmd_ledblink led; 1972 1973 led.lednum = htobe32(lednum); 1974 led.ledmode = htobe32(ledmode); 1975 led.blinkrate = htobe32(blinkrate); 1976 led.slowmode = htobe32(slowmode); 1977 1978 DPRINTF(sc, UATH_DEBUG_LED, "set %s led %s (blink)\n", 1979 (lednum == UATH_LED_LINK) ? "link" : "activity", 1980 ledmode ? "on" : "off"); 1981 return uath_cmd_write(sc, WDCMSG_SET_LED_BLINK, &led, sizeof led, 0); 1982 } 1983 1984 static int 1985 uath_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 1986 { 1987 enum ieee80211_state ostate = vap->iv_state; 1988 int error; 1989 struct ieee80211_node *ni; 1990 struct ieee80211com *ic = vap->iv_ic; 1991 struct uath_softc *sc = ic->ic_softc; 1992 struct uath_vap *uvp = UATH_VAP(vap); 1993 1994 DPRINTF(sc, UATH_DEBUG_STATE, 1995 "%s: %s -> %s\n", __func__, ieee80211_state_name[vap->iv_state], 1996 ieee80211_state_name[nstate]); 1997 1998 IEEE80211_UNLOCK(ic); 1999 UATH_LOCK(sc); 2000 callout_stop(&sc->stat_ch); 2001 callout_stop(&sc->watchdog_ch); 2002 ni = ieee80211_ref_node(vap->iv_bss); 2003 2004 switch (nstate) { 2005 case IEEE80211_S_INIT: 2006 if (ostate == IEEE80211_S_RUN) { 2007 /* turn link and activity LEDs off */ 2008 uath_set_ledstate(sc, 0); 2009 } 2010 break; 2011 2012 case IEEE80211_S_SCAN: 2013 break; 2014 2015 case IEEE80211_S_AUTH: 2016 /* XXX good place? set RTS threshold */ 2017 uath_config(sc, CFG_USER_RTS_THRESHOLD, vap->iv_rtsthreshold); 2018 /* XXX bad place */ 2019 error = uath_set_keys(sc, vap); 2020 if (error != 0) { 2021 device_printf(sc->sc_dev, 2022 "could not set crypto keys, error %d\n", error); 2023 break; 2024 } 2025 if (uath_switch_channel(sc, ni->ni_chan) != 0) { 2026 device_printf(sc->sc_dev, "could not switch channel\n"); 2027 break; 2028 } 2029 if (uath_create_connection(sc, UATH_ID_BSS) != 0) { 2030 device_printf(sc->sc_dev, 2031 "could not create connection\n"); 2032 break; 2033 } 2034 break; 2035 2036 case IEEE80211_S_ASSOC: 2037 if (uath_set_rates(sc, &ni->ni_rates) != 0) { 2038 device_printf(sc->sc_dev, 2039 "could not set negotiated rate set\n"); 2040 break; 2041 } 2042 break; 2043 2044 case IEEE80211_S_RUN: 2045 /* XXX monitor mode doesn't be tested */ 2046 if (ic->ic_opmode == IEEE80211_M_MONITOR) { 2047 uath_set_ledstate(sc, 1); 2048 break; 2049 } 2050 2051 /* 2052 * Tx rate is controlled by firmware, report the maximum 2053 * negotiated rate in ifconfig output. 2054 */ 2055 ni->ni_txrate = ni->ni_rates.rs_rates[ni->ni_rates.rs_nrates-1]; 2056 2057 if (uath_write_associd(sc) != 0) { 2058 device_printf(sc->sc_dev, 2059 "could not write association id\n"); 2060 break; 2061 } 2062 /* turn link LED on */ 2063 uath_set_ledsteady(sc, UATH_LED_LINK, UATH_LED_ON); 2064 /* make activity LED blink */ 2065 uath_set_ledblink(sc, UATH_LED_ACTIVITY, UATH_LED_ON, 1, 2); 2066 /* set state to associated */ 2067 uath_set_ledstate(sc, 1); 2068 2069 /* start statistics timer */ 2070 callout_reset(&sc->stat_ch, hz, uath_stat, sc); 2071 break; 2072 default: 2073 break; 2074 } 2075 ieee80211_free_node(ni); 2076 UATH_UNLOCK(sc); 2077 IEEE80211_LOCK(ic); 2078 return (uvp->newstate(vap, nstate, arg)); 2079 } 2080 2081 static int 2082 uath_set_key(struct uath_softc *sc, const struct ieee80211_key *wk, 2083 int index) 2084 { 2085 #if 0 2086 struct uath_cmd_crypto crypto; 2087 int i; 2088 2089 memset(&crypto, 0, sizeof(crypto)); 2090 crypto.keyidx = htobe32(index); 2091 crypto.magic1 = htobe32(1); 2092 crypto.size = htobe32(368); 2093 crypto.mask = htobe32(0xffff); 2094 crypto.flags = htobe32(0x80000068); 2095 if (index != UATH_DEFAULT_KEY) 2096 crypto.flags |= htobe32(index << 16); 2097 memset(crypto.magic2, 0xff, sizeof(crypto.magic2)); 2098 2099 /* 2100 * Each byte of the key must be XOR'ed with 10101010 before being 2101 * transmitted to the firmware. 2102 */ 2103 for (i = 0; i < wk->wk_keylen; i++) 2104 crypto.key[i] = wk->wk_key[i] ^ 0xaa; 2105 2106 DPRINTF(sc, UATH_DEBUG_CRYPTO, 2107 "setting crypto key index=%d len=%d\n", index, wk->wk_keylen); 2108 return uath_cmd_write(sc, WDCMSG_SET_KEY_CACHE_ENTRY, &crypto, 2109 sizeof crypto, 0); 2110 #else 2111 /* XXX support H/W cryto */ 2112 return (0); 2113 #endif 2114 } 2115 2116 static int 2117 uath_set_keys(struct uath_softc *sc, struct ieee80211vap *vap) 2118 { 2119 int i, error; 2120 2121 error = 0; 2122 for (i = 0; i < IEEE80211_WEP_NKID; i++) { 2123 const struct ieee80211_key *wk = &vap->iv_nw_keys[i]; 2124 2125 if (wk->wk_flags & (IEEE80211_KEY_XMIT|IEEE80211_KEY_RECV)) { 2126 error = uath_set_key(sc, wk, i); 2127 if (error) 2128 return (error); 2129 } 2130 } 2131 if (vap->iv_def_txkey != IEEE80211_KEYIX_NONE) { 2132 error = uath_set_key(sc, &vap->iv_nw_keys[vap->iv_def_txkey], 2133 UATH_DEFAULT_KEY); 2134 } 2135 return (error); 2136 } 2137 2138 #define UATH_SYSCTL_STAT_ADD32(c, h, n, p, d) \ 2139 SYSCTL_ADD_UINT(c, h, OID_AUTO, n, CTLFLAG_RD, p, 0, d) 2140 2141 static void 2142 uath_sysctl_node(struct uath_softc *sc) 2143 { 2144 struct sysctl_ctx_list *ctx; 2145 struct sysctl_oid_list *child; 2146 struct sysctl_oid *tree; 2147 struct uath_stat *stats; 2148 2149 stats = &sc->sc_stat; 2150 ctx = device_get_sysctl_ctx(sc->sc_dev); 2151 child = SYSCTL_CHILDREN(device_get_sysctl_tree(sc->sc_dev)); 2152 2153 tree = SYSCTL_ADD_NODE(ctx, child, OID_AUTO, "stats", CTLFLAG_RD, 2154 NULL, "UATH statistics"); 2155 child = SYSCTL_CHILDREN(tree); 2156 UATH_SYSCTL_STAT_ADD32(ctx, child, "badchunkseqnum", 2157 &stats->st_badchunkseqnum, "Bad chunk sequence numbers"); 2158 UATH_SYSCTL_STAT_ADD32(ctx, child, "invalidlen", &stats->st_invalidlen, 2159 "Invalid length"); 2160 UATH_SYSCTL_STAT_ADD32(ctx, child, "multichunk", &stats->st_multichunk, 2161 "Multi chunks"); 2162 UATH_SYSCTL_STAT_ADD32(ctx, child, "toobigrxpkt", 2163 &stats->st_toobigrxpkt, "Too big rx packets"); 2164 UATH_SYSCTL_STAT_ADD32(ctx, child, "stopinprogress", 2165 &stats->st_stopinprogress, "Stop in progress"); 2166 UATH_SYSCTL_STAT_ADD32(ctx, child, "crcerrs", &stats->st_crcerr, 2167 "CRC errors"); 2168 UATH_SYSCTL_STAT_ADD32(ctx, child, "phyerr", &stats->st_phyerr, 2169 "PHY errors"); 2170 UATH_SYSCTL_STAT_ADD32(ctx, child, "decrypt_crcerr", 2171 &stats->st_decrypt_crcerr, "Decryption CRC errors"); 2172 UATH_SYSCTL_STAT_ADD32(ctx, child, "decrypt_micerr", 2173 &stats->st_decrypt_micerr, "Decryption Misc errors"); 2174 UATH_SYSCTL_STAT_ADD32(ctx, child, "decomperr", &stats->st_decomperr, 2175 "Decomp errors"); 2176 UATH_SYSCTL_STAT_ADD32(ctx, child, "keyerr", &stats->st_keyerr, 2177 "Key errors"); 2178 UATH_SYSCTL_STAT_ADD32(ctx, child, "err", &stats->st_err, 2179 "Unknown errors"); 2180 2181 UATH_SYSCTL_STAT_ADD32(ctx, child, "cmd_active", 2182 &stats->st_cmd_active, "Active numbers in Command queue"); 2183 UATH_SYSCTL_STAT_ADD32(ctx, child, "cmd_inactive", 2184 &stats->st_cmd_inactive, "Inactive numbers in Command queue"); 2185 UATH_SYSCTL_STAT_ADD32(ctx, child, "cmd_pending", 2186 &stats->st_cmd_pending, "Pending numbers in Command queue"); 2187 UATH_SYSCTL_STAT_ADD32(ctx, child, "cmd_waiting", 2188 &stats->st_cmd_waiting, "Waiting numbers in Command queue"); 2189 UATH_SYSCTL_STAT_ADD32(ctx, child, "rx_active", 2190 &stats->st_rx_active, "Active numbers in RX queue"); 2191 UATH_SYSCTL_STAT_ADD32(ctx, child, "rx_inactive", 2192 &stats->st_rx_inactive, "Inactive numbers in RX queue"); 2193 UATH_SYSCTL_STAT_ADD32(ctx, child, "tx_active", 2194 &stats->st_tx_active, "Active numbers in TX queue"); 2195 UATH_SYSCTL_STAT_ADD32(ctx, child, "tx_inactive", 2196 &stats->st_tx_inactive, "Inactive numbers in TX queue"); 2197 UATH_SYSCTL_STAT_ADD32(ctx, child, "tx_pending", 2198 &stats->st_tx_pending, "Pending numbers in TX queue"); 2199 } 2200 2201 #undef UATH_SYSCTL_STAT_ADD32 2202 2203 static void 2204 uath_cmdeof(struct uath_softc *sc, struct uath_cmd *cmd) 2205 { 2206 struct uath_cmd_hdr *hdr; 2207 int dlen; 2208 2209 hdr = (struct uath_cmd_hdr *)cmd->buf; 2210 /* NB: msgid is passed thru w/o byte swapping */ 2211 #ifdef UATH_DEBUG 2212 if (sc->sc_debug & UATH_DEBUG_CMDS) { 2213 int len = be32toh(hdr->len); 2214 printf("%s: %s [ix %u] len %u status %u\n", 2215 __func__, uath_codename(be32toh(hdr->code)), 2216 hdr->msgid, len, be32toh(hdr->magic)); 2217 if (sc->sc_debug & UATH_DEBUG_CMDS_DUMP) 2218 uath_dump_cmd(cmd->buf, 2219 len > UATH_MAX_CMDSZ ? sizeof(*hdr) : len, '-'); 2220 } 2221 #endif 2222 hdr->code = be32toh(hdr->code); 2223 hdr->len = be32toh(hdr->len); 2224 hdr->magic = be32toh(hdr->magic); /* target status on return */ 2225 2226 switch (hdr->code & 0xff) { 2227 /* reply to a read command */ 2228 default: 2229 dlen = hdr->len - sizeof(*hdr); 2230 if (dlen < 0) { 2231 device_printf(sc->sc_dev, 2232 "Invalid header length %d\n", dlen); 2233 return; 2234 } 2235 DPRINTF(sc, UATH_DEBUG_RX_PROC | UATH_DEBUG_RECV_ALL, 2236 "%s: code %d data len %u\n", 2237 __func__, hdr->code & 0xff, dlen); 2238 /* 2239 * The first response from the target after the 2240 * HOST_AVAILABLE has an invalid msgid so we must 2241 * treat it specially. 2242 */ 2243 if (hdr->msgid < UATH_CMD_LIST_COUNT) { 2244 uint32_t *rp = (uint32_t *)(hdr+1); 2245 u_int olen; 2246 2247 if (!(sizeof(*hdr) <= hdr->len && 2248 hdr->len < UATH_MAX_CMDSZ)) { 2249 device_printf(sc->sc_dev, 2250 "%s: invalid WDC msg length %u; " 2251 "msg ignored\n", __func__, hdr->len); 2252 return; 2253 } 2254 /* 2255 * Calculate return/receive payload size; the 2256 * first word, if present, always gives the 2257 * number of bytes--unless it's 0 in which 2258 * case a single 32-bit word should be present. 2259 */ 2260 if (dlen >= (int)sizeof(uint32_t)) { 2261 olen = be32toh(rp[0]); 2262 dlen -= sizeof(uint32_t); 2263 if (olen == 0) { 2264 /* convention is 0 =>'s one word */ 2265 olen = sizeof(uint32_t); 2266 /* XXX KASSERT(olen == dlen ) */ 2267 } 2268 } else 2269 olen = 0; 2270 if (cmd->odata != NULL) { 2271 /* NB: cmd->olen validated in uath_cmd */ 2272 if (olen > (u_int)cmd->olen) { 2273 /* XXX complain? */ 2274 device_printf(sc->sc_dev, 2275 "%s: cmd 0x%x olen %u cmd olen %u\n", 2276 __func__, hdr->code, olen, 2277 cmd->olen); 2278 olen = cmd->olen; 2279 } 2280 if (olen > (u_int)dlen) { 2281 /* XXX complain, shouldn't happen */ 2282 device_printf(sc->sc_dev, 2283 "%s: cmd 0x%x olen %u dlen %u\n", 2284 __func__, hdr->code, olen, dlen); 2285 olen = dlen; 2286 } 2287 /* XXX have submitter do this */ 2288 /* copy answer into caller's supplied buffer */ 2289 bcopy(&rp[1], cmd->odata, olen); 2290 cmd->olen = olen; 2291 } 2292 } 2293 wakeup_one(cmd); /* wake up caller */ 2294 break; 2295 2296 case WDCMSG_TARGET_START: 2297 if (hdr->msgid >= UATH_CMD_LIST_COUNT) { 2298 /* XXX */ 2299 return; 2300 } 2301 dlen = hdr->len - sizeof(*hdr); 2302 if (dlen != (int)sizeof(uint32_t)) { 2303 /* XXX something wrong */ 2304 return; 2305 } 2306 /* XXX have submitter do this */ 2307 /* copy answer into caller's supplied buffer */ 2308 bcopy(hdr+1, cmd->odata, sizeof(uint32_t)); 2309 cmd->olen = sizeof(uint32_t); 2310 wakeup_one(cmd); /* wake up caller */ 2311 break; 2312 2313 case WDCMSG_SEND_COMPLETE: 2314 /* this notification is sent when UATH_TX_NOTIFY is set */ 2315 DPRINTF(sc, UATH_DEBUG_RX_PROC | UATH_DEBUG_RECV_ALL, 2316 "%s: received Tx notification\n", __func__); 2317 break; 2318 2319 case WDCMSG_TARGET_GET_STATS: 2320 DPRINTF(sc, UATH_DEBUG_RX_PROC | UATH_DEBUG_RECV_ALL, 2321 "%s: received device statistics\n", __func__); 2322 callout_reset(&sc->stat_ch, hz, uath_stat, sc); 2323 break; 2324 } 2325 } 2326 2327 static void 2328 uath_intr_rx_callback(struct usb_xfer *xfer, usb_error_t error) 2329 { 2330 struct uath_softc *sc = usbd_xfer_softc(xfer); 2331 struct uath_cmd *cmd; 2332 struct usb_page_cache *pc; 2333 int actlen; 2334 2335 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 2336 2337 UATH_ASSERT_LOCKED(sc); 2338 2339 switch (USB_GET_STATE(xfer)) { 2340 case USB_ST_TRANSFERRED: 2341 cmd = STAILQ_FIRST(&sc->sc_cmd_waiting); 2342 if (cmd == NULL) 2343 goto setup; 2344 STAILQ_REMOVE_HEAD(&sc->sc_cmd_waiting, next); 2345 UATH_STAT_DEC(sc, st_cmd_waiting); 2346 STAILQ_INSERT_TAIL(&sc->sc_cmd_inactive, cmd, next); 2347 UATH_STAT_INC(sc, st_cmd_inactive); 2348 2349 KASSERT(actlen >= (int)sizeof(struct uath_cmd_hdr), 2350 ("short xfer error")); 2351 pc = usbd_xfer_get_frame(xfer, 0); 2352 usbd_copy_out(pc, 0, cmd->buf, actlen); 2353 uath_cmdeof(sc, cmd); 2354 case USB_ST_SETUP: 2355 setup: 2356 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer)); 2357 usbd_transfer_submit(xfer); 2358 break; 2359 default: 2360 if (error != USB_ERR_CANCELLED) { 2361 usbd_xfer_set_stall(xfer); 2362 goto setup; 2363 } 2364 break; 2365 } 2366 } 2367 2368 static void 2369 uath_intr_tx_callback(struct usb_xfer *xfer, usb_error_t error) 2370 { 2371 struct uath_softc *sc = usbd_xfer_softc(xfer); 2372 struct uath_cmd *cmd; 2373 2374 UATH_ASSERT_LOCKED(sc); 2375 2376 cmd = STAILQ_FIRST(&sc->sc_cmd_active); 2377 if (cmd != NULL && USB_GET_STATE(xfer) != USB_ST_SETUP) { 2378 STAILQ_REMOVE_HEAD(&sc->sc_cmd_active, next); 2379 UATH_STAT_DEC(sc, st_cmd_active); 2380 STAILQ_INSERT_TAIL((cmd->flags & UATH_CMD_FLAG_READ) ? 2381 &sc->sc_cmd_waiting : &sc->sc_cmd_inactive, cmd, next); 2382 if (cmd->flags & UATH_CMD_FLAG_READ) 2383 UATH_STAT_INC(sc, st_cmd_waiting); 2384 else 2385 UATH_STAT_INC(sc, st_cmd_inactive); 2386 } 2387 2388 switch (USB_GET_STATE(xfer)) { 2389 case USB_ST_TRANSFERRED: 2390 case USB_ST_SETUP: 2391 setup: 2392 cmd = STAILQ_FIRST(&sc->sc_cmd_pending); 2393 if (cmd == NULL) { 2394 DPRINTF(sc, UATH_DEBUG_XMIT, "%s: empty pending queue\n", 2395 __func__); 2396 return; 2397 } 2398 STAILQ_REMOVE_HEAD(&sc->sc_cmd_pending, next); 2399 UATH_STAT_DEC(sc, st_cmd_pending); 2400 STAILQ_INSERT_TAIL((cmd->flags & UATH_CMD_FLAG_ASYNC) ? 2401 &sc->sc_cmd_inactive : &sc->sc_cmd_active, cmd, next); 2402 if (cmd->flags & UATH_CMD_FLAG_ASYNC) 2403 UATH_STAT_INC(sc, st_cmd_inactive); 2404 else 2405 UATH_STAT_INC(sc, st_cmd_active); 2406 2407 usbd_xfer_set_frame_data(xfer, 0, cmd->buf, cmd->buflen); 2408 usbd_transfer_submit(xfer); 2409 break; 2410 default: 2411 if (error != USB_ERR_CANCELLED) { 2412 usbd_xfer_set_stall(xfer); 2413 goto setup; 2414 } 2415 break; 2416 } 2417 } 2418 2419 static void 2420 uath_update_rxstat(struct uath_softc *sc, uint32_t status) 2421 { 2422 2423 switch (status) { 2424 case UATH_STATUS_STOP_IN_PROGRESS: 2425 UATH_STAT_INC(sc, st_stopinprogress); 2426 break; 2427 case UATH_STATUS_CRC_ERR: 2428 UATH_STAT_INC(sc, st_crcerr); 2429 break; 2430 case UATH_STATUS_PHY_ERR: 2431 UATH_STAT_INC(sc, st_phyerr); 2432 break; 2433 case UATH_STATUS_DECRYPT_CRC_ERR: 2434 UATH_STAT_INC(sc, st_decrypt_crcerr); 2435 break; 2436 case UATH_STATUS_DECRYPT_MIC_ERR: 2437 UATH_STAT_INC(sc, st_decrypt_micerr); 2438 break; 2439 case UATH_STATUS_DECOMP_ERR: 2440 UATH_STAT_INC(sc, st_decomperr); 2441 break; 2442 case UATH_STATUS_KEY_ERR: 2443 UATH_STAT_INC(sc, st_keyerr); 2444 break; 2445 case UATH_STATUS_ERR: 2446 UATH_STAT_INC(sc, st_err); 2447 break; 2448 default: 2449 break; 2450 } 2451 } 2452 2453 static struct mbuf * 2454 uath_data_rxeof(struct usb_xfer *xfer, struct uath_data *data, 2455 struct uath_rx_desc **pdesc) 2456 { 2457 struct uath_softc *sc = usbd_xfer_softc(xfer); 2458 struct ieee80211com *ic = &sc->sc_ic; 2459 struct uath_chunk *chunk; 2460 struct uath_rx_desc *desc; 2461 struct mbuf *m = data->m, *mnew, *mp; 2462 uint16_t chunklen; 2463 int actlen; 2464 2465 usbd_xfer_status(xfer, &actlen, NULL, NULL, NULL); 2466 2467 if (actlen < (int)UATH_MIN_RXBUFSZ) { 2468 DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL, 2469 "%s: wrong xfer size (len=%d)\n", __func__, actlen); 2470 counter_u64_add(ic->ic_ierrors, 1); 2471 return (NULL); 2472 } 2473 2474 chunk = (struct uath_chunk *)data->buf; 2475 if (chunk->seqnum == 0 && chunk->flags == 0 && chunk->length == 0) { 2476 device_printf(sc->sc_dev, "%s: strange response\n", __func__); 2477 counter_u64_add(ic->ic_ierrors, 1); 2478 UATH_RESET_INTRX(sc); 2479 return (NULL); 2480 } 2481 2482 if (chunk->seqnum != sc->sc_intrx_nextnum) { 2483 DPRINTF(sc, UATH_DEBUG_XMIT, "invalid seqnum %d, expected %d\n", 2484 chunk->seqnum, sc->sc_intrx_nextnum); 2485 UATH_STAT_INC(sc, st_badchunkseqnum); 2486 if (sc->sc_intrx_head != NULL) 2487 m_freem(sc->sc_intrx_head); 2488 UATH_RESET_INTRX(sc); 2489 return (NULL); 2490 } 2491 2492 /* check multi-chunk frames */ 2493 if ((chunk->seqnum == 0 && !(chunk->flags & UATH_CFLAGS_FINAL)) || 2494 (chunk->seqnum != 0 && (chunk->flags & UATH_CFLAGS_FINAL)) || 2495 chunk->flags & UATH_CFLAGS_RXMSG) 2496 UATH_STAT_INC(sc, st_multichunk); 2497 2498 chunklen = be16toh(chunk->length); 2499 if (chunk->flags & UATH_CFLAGS_FINAL) 2500 chunklen -= sizeof(struct uath_rx_desc); 2501 2502 if (chunklen > 0 && 2503 (!(chunk->flags & UATH_CFLAGS_FINAL) || !(chunk->seqnum == 0))) { 2504 /* we should use intermediate RX buffer */ 2505 if (chunk->seqnum == 0) 2506 UATH_RESET_INTRX(sc); 2507 if ((sc->sc_intrx_len + sizeof(struct uath_rx_desc) + 2508 chunklen) > UATH_MAX_INTRX_SIZE) { 2509 UATH_STAT_INC(sc, st_invalidlen); 2510 counter_u64_add(ic->ic_ierrors, 1); 2511 if (sc->sc_intrx_head != NULL) 2512 m_freem(sc->sc_intrx_head); 2513 UATH_RESET_INTRX(sc); 2514 return (NULL); 2515 } 2516 2517 m->m_len = chunklen; 2518 m->m_data += sizeof(struct uath_chunk); 2519 2520 if (sc->sc_intrx_head == NULL) { 2521 sc->sc_intrx_head = m; 2522 sc->sc_intrx_tail = m; 2523 } else { 2524 m->m_flags &= ~M_PKTHDR; 2525 sc->sc_intrx_tail->m_next = m; 2526 sc->sc_intrx_tail = m; 2527 } 2528 } 2529 sc->sc_intrx_len += chunklen; 2530 2531 mnew = m_getcl(M_NOWAIT, MT_DATA, M_PKTHDR); 2532 if (mnew == NULL) { 2533 DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL, 2534 "%s: can't get new mbuf, drop frame\n", __func__); 2535 counter_u64_add(ic->ic_ierrors, 1); 2536 if (sc->sc_intrx_head != NULL) 2537 m_freem(sc->sc_intrx_head); 2538 UATH_RESET_INTRX(sc); 2539 return (NULL); 2540 } 2541 2542 data->m = mnew; 2543 data->buf = mtod(mnew, uint8_t *); 2544 2545 /* if the frame is not final continue the transfer */ 2546 if (!(chunk->flags & UATH_CFLAGS_FINAL)) { 2547 sc->sc_intrx_nextnum++; 2548 UATH_RESET_INTRX(sc); 2549 return (NULL); 2550 } 2551 2552 /* 2553 * if the frame is not set UATH_CFLAGS_RXMSG, then rx descriptor is 2554 * located at the end, 32-bit aligned 2555 */ 2556 desc = (chunk->flags & UATH_CFLAGS_RXMSG) ? 2557 (struct uath_rx_desc *)(chunk + 1) : 2558 (struct uath_rx_desc *)(((uint8_t *)chunk) + 2559 sizeof(struct uath_chunk) + be16toh(chunk->length) - 2560 sizeof(struct uath_rx_desc)); 2561 *pdesc = desc; 2562 2563 DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL, 2564 "%s: frame len %u code %u status %u rate %u antenna %u " 2565 "rssi %d channel %u phyerror %u connix %u decrypterror %u " 2566 "keycachemiss %u\n", __func__, be32toh(desc->framelen) 2567 , be32toh(desc->code), be32toh(desc->status), be32toh(desc->rate) 2568 , be32toh(desc->antenna), be32toh(desc->rssi), be32toh(desc->channel) 2569 , be32toh(desc->phyerror), be32toh(desc->connix) 2570 , be32toh(desc->decrypterror), be32toh(desc->keycachemiss)); 2571 2572 if (be32toh(desc->len) > MCLBYTES) { 2573 DPRINTF(sc, UATH_DEBUG_RECV | UATH_DEBUG_RECV_ALL, 2574 "%s: bad descriptor (len=%d)\n", __func__, 2575 be32toh(desc->len)); 2576 counter_u64_add(ic->ic_ierrors, 1); 2577 UATH_STAT_INC(sc, st_toobigrxpkt); 2578 if (sc->sc_intrx_head != NULL) 2579 m_freem(sc->sc_intrx_head); 2580 UATH_RESET_INTRX(sc); 2581 return (NULL); 2582 } 2583 2584 uath_update_rxstat(sc, be32toh(desc->status)); 2585 2586 /* finalize mbuf */ 2587 if (sc->sc_intrx_head == NULL) { 2588 m->m_pkthdr.len = m->m_len = 2589 be32toh(desc->framelen) - UATH_RX_DUMMYSIZE; 2590 m->m_data += sizeof(struct uath_chunk); 2591 } else { 2592 mp = sc->sc_intrx_head; 2593 mp->m_flags |= M_PKTHDR; 2594 mp->m_pkthdr.len = sc->sc_intrx_len; 2595 m = mp; 2596 } 2597 2598 /* there are a lot more fields in the RX descriptor */ 2599 if ((sc->sc_flags & UATH_FLAG_INVALID) == 0 && 2600 ieee80211_radiotap_active(ic)) { 2601 struct uath_rx_radiotap_header *tap = &sc->sc_rxtap; 2602 uint32_t tsf_hi = be32toh(desc->tstamp_high); 2603 uint32_t tsf_lo = be32toh(desc->tstamp_low); 2604 2605 /* XXX only get low order 24bits of tsf from h/w */ 2606 tap->wr_tsf = htole64(((uint64_t)tsf_hi << 32) | tsf_lo); 2607 tap->wr_flags = 0; 2608 if (be32toh(desc->status) == UATH_STATUS_CRC_ERR) 2609 tap->wr_flags |= IEEE80211_RADIOTAP_F_BADFCS; 2610 /* XXX map other status to BADFCS? */ 2611 /* XXX ath h/w rate code, need to map */ 2612 tap->wr_rate = be32toh(desc->rate); 2613 tap->wr_antenna = be32toh(desc->antenna); 2614 tap->wr_antsignal = -95 + be32toh(desc->rssi); 2615 tap->wr_antnoise = -95; 2616 } 2617 2618 UATH_RESET_INTRX(sc); 2619 2620 return (m); 2621 } 2622 2623 static void 2624 uath_bulk_rx_callback(struct usb_xfer *xfer, usb_error_t error) 2625 { 2626 struct uath_softc *sc = usbd_xfer_softc(xfer); 2627 struct ieee80211com *ic = &sc->sc_ic; 2628 struct ieee80211_frame *wh; 2629 struct ieee80211_node *ni; 2630 struct mbuf *m = NULL; 2631 struct uath_data *data; 2632 struct uath_rx_desc *desc = NULL; 2633 int8_t nf; 2634 2635 UATH_ASSERT_LOCKED(sc); 2636 2637 switch (USB_GET_STATE(xfer)) { 2638 case USB_ST_TRANSFERRED: 2639 data = STAILQ_FIRST(&sc->sc_rx_active); 2640 if (data == NULL) 2641 goto setup; 2642 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next); 2643 UATH_STAT_DEC(sc, st_rx_active); 2644 m = uath_data_rxeof(xfer, data, &desc); 2645 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 2646 UATH_STAT_INC(sc, st_rx_inactive); 2647 /* FALLTHROUGH */ 2648 case USB_ST_SETUP: 2649 setup: 2650 data = STAILQ_FIRST(&sc->sc_rx_inactive); 2651 if (data == NULL) 2652 return; 2653 STAILQ_REMOVE_HEAD(&sc->sc_rx_inactive, next); 2654 UATH_STAT_DEC(sc, st_rx_inactive); 2655 STAILQ_INSERT_TAIL(&sc->sc_rx_active, data, next); 2656 UATH_STAT_INC(sc, st_rx_active); 2657 usbd_xfer_set_frame_data(xfer, 0, data->buf, MCLBYTES); 2658 usbd_transfer_submit(xfer); 2659 2660 /* 2661 * To avoid LOR we should unlock our private mutex here to call 2662 * ieee80211_input() because here is at the end of a USB 2663 * callback and safe to unlock. 2664 */ 2665 if (sc->sc_flags & UATH_FLAG_INVALID) { 2666 if (m != NULL) 2667 m_freem(m); 2668 return; 2669 } 2670 UATH_UNLOCK(sc); 2671 if (m != NULL && desc != NULL) { 2672 wh = mtod(m, struct ieee80211_frame *); 2673 ni = ieee80211_find_rxnode(ic, 2674 (struct ieee80211_frame_min *)wh); 2675 nf = -95; /* XXX */ 2676 if (ni != NULL) { 2677 (void) ieee80211_input(ni, m, 2678 (int)be32toh(desc->rssi), nf); 2679 /* node is no longer needed */ 2680 ieee80211_free_node(ni); 2681 } else 2682 (void) ieee80211_input_all(ic, m, 2683 (int)be32toh(desc->rssi), nf); 2684 m = NULL; 2685 desc = NULL; 2686 } 2687 UATH_LOCK(sc); 2688 uath_start(sc); 2689 break; 2690 default: 2691 /* needs it to the inactive queue due to a error. */ 2692 data = STAILQ_FIRST(&sc->sc_rx_active); 2693 if (data != NULL) { 2694 STAILQ_REMOVE_HEAD(&sc->sc_rx_active, next); 2695 UATH_STAT_DEC(sc, st_rx_active); 2696 STAILQ_INSERT_TAIL(&sc->sc_rx_inactive, data, next); 2697 UATH_STAT_INC(sc, st_rx_inactive); 2698 } 2699 if (error != USB_ERR_CANCELLED) { 2700 usbd_xfer_set_stall(xfer); 2701 counter_u64_add(ic->ic_ierrors, 1); 2702 goto setup; 2703 } 2704 break; 2705 } 2706 } 2707 2708 static void 2709 uath_data_txeof(struct usb_xfer *xfer, struct uath_data *data) 2710 { 2711 struct uath_softc *sc = usbd_xfer_softc(xfer); 2712 2713 UATH_ASSERT_LOCKED(sc); 2714 2715 if (data->m) { 2716 /* XXX status? */ 2717 ieee80211_tx_complete(data->ni, data->m, 0); 2718 data->m = NULL; 2719 data->ni = NULL; 2720 } 2721 sc->sc_tx_timer = 0; 2722 } 2723 2724 static void 2725 uath_bulk_tx_callback(struct usb_xfer *xfer, usb_error_t error) 2726 { 2727 struct uath_softc *sc = usbd_xfer_softc(xfer); 2728 struct uath_data *data; 2729 2730 UATH_ASSERT_LOCKED(sc); 2731 2732 switch (USB_GET_STATE(xfer)) { 2733 case USB_ST_TRANSFERRED: 2734 data = STAILQ_FIRST(&sc->sc_tx_active); 2735 if (data == NULL) 2736 goto setup; 2737 STAILQ_REMOVE_HEAD(&sc->sc_tx_active, next); 2738 UATH_STAT_DEC(sc, st_tx_active); 2739 uath_data_txeof(xfer, data); 2740 STAILQ_INSERT_TAIL(&sc->sc_tx_inactive, data, next); 2741 UATH_STAT_INC(sc, st_tx_inactive); 2742 /* FALLTHROUGH */ 2743 case USB_ST_SETUP: 2744 setup: 2745 data = STAILQ_FIRST(&sc->sc_tx_pending); 2746 if (data == NULL) { 2747 DPRINTF(sc, UATH_DEBUG_XMIT, "%s: empty pending queue\n", 2748 __func__); 2749 return; 2750 } 2751 STAILQ_REMOVE_HEAD(&sc->sc_tx_pending, next); 2752 UATH_STAT_DEC(sc, st_tx_pending); 2753 STAILQ_INSERT_TAIL(&sc->sc_tx_active, data, next); 2754 UATH_STAT_INC(sc, st_tx_active); 2755 2756 usbd_xfer_set_frame_data(xfer, 0, data->buf, data->buflen); 2757 usbd_transfer_submit(xfer); 2758 2759 uath_start(sc); 2760 break; 2761 default: 2762 data = STAILQ_FIRST(&sc->sc_tx_active); 2763 if (data == NULL) 2764 goto setup; 2765 if (data->ni != NULL) { 2766 if_inc_counter(data->ni->ni_vap->iv_ifp, 2767 IFCOUNTER_OERRORS, 1); 2768 if ((sc->sc_flags & UATH_FLAG_INVALID) == 0) 2769 ieee80211_free_node(data->ni); 2770 data->ni = NULL; 2771 } 2772 if (error != USB_ERR_CANCELLED) { 2773 usbd_xfer_set_stall(xfer); 2774 goto setup; 2775 } 2776 break; 2777 } 2778 } 2779 2780 static device_method_t uath_methods[] = { 2781 DEVMETHOD(device_probe, uath_match), 2782 DEVMETHOD(device_attach, uath_attach), 2783 DEVMETHOD(device_detach, uath_detach), 2784 DEVMETHOD_END 2785 }; 2786 static driver_t uath_driver = { 2787 .name = "uath", 2788 .methods = uath_methods, 2789 .size = sizeof(struct uath_softc) 2790 }; 2791 static devclass_t uath_devclass; 2792 2793 DRIVER_MODULE(uath, uhub, uath_driver, uath_devclass, NULL, 0); 2794 MODULE_DEPEND(uath, wlan, 1, 1, 1); 2795 MODULE_DEPEND(uath, usb, 1, 1, 1); 2796 MODULE_VERSION(uath, 1); 2797