rt2560.c revision 190532
1/* $FreeBSD: head/sys/dev/ral/rt2560.c 190532 2009-03-29 21:17:08Z sam $ */ 2 3/*- 4 * Copyright (c) 2005, 2006 5 * Damien Bergamini <damien.bergamini@free.fr> 6 * 7 * Permission to use, copy, modify, and distribute this software for any 8 * purpose with or without fee is hereby granted, provided that the above 9 * copyright notice and this permission notice appear in all copies. 10 * 11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF 17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. 18 */ 19 20#include <sys/cdefs.h> 21__FBSDID("$FreeBSD: head/sys/dev/ral/rt2560.c 190532 2009-03-29 21:17:08Z sam $"); 22 23/*- 24 * Ralink Technology RT2560 chipset driver 25 * http://www.ralinktech.com/ 26 */ 27 28#include <sys/param.h> 29#include <sys/sysctl.h> 30#include <sys/sockio.h> 31#include <sys/mbuf.h> 32#include <sys/kernel.h> 33#include <sys/socket.h> 34#include <sys/systm.h> 35#include <sys/malloc.h> 36#include <sys/lock.h> 37#include <sys/mutex.h> 38#include <sys/module.h> 39#include <sys/bus.h> 40#include <sys/endian.h> 41 42#include <machine/bus.h> 43#include <machine/resource.h> 44#include <sys/rman.h> 45 46#include <net/bpf.h> 47#include <net/if.h> 48#include <net/if_arp.h> 49#include <net/ethernet.h> 50#include <net/if_dl.h> 51#include <net/if_media.h> 52#include <net/if_types.h> 53 54#include <net80211/ieee80211_var.h> 55#include <net80211/ieee80211_radiotap.h> 56#include <net80211/ieee80211_regdomain.h> 57#include <net80211/ieee80211_amrr.h> 58 59#include <netinet/in.h> 60#include <netinet/in_systm.h> 61#include <netinet/in_var.h> 62#include <netinet/ip.h> 63#include <netinet/if_ether.h> 64 65#include <dev/ral/rt2560reg.h> 66#include <dev/ral/rt2560var.h> 67 68#define RT2560_RSSI(sc, rssi) \ 69 ((rssi) > (RT2560_NOISE_FLOOR + (sc)->rssi_corr) ? \ 70 ((rssi) - RT2560_NOISE_FLOOR - (sc)->rssi_corr) : 0) 71 72#define RAL_DEBUG 73#ifdef RAL_DEBUG 74#define DPRINTF(sc, fmt, ...) do { \ 75 if (sc->sc_debug > 0) \ 76 printf(fmt, __VA_ARGS__); \ 77} while (0) 78#define DPRINTFN(sc, n, fmt, ...) do { \ 79 if (sc->sc_debug >= (n)) \ 80 printf(fmt, __VA_ARGS__); \ 81} while (0) 82#else 83#define DPRINTF(sc, fmt, ...) 84#define DPRINTFN(sc, n, fmt, ...) 85#endif 86 87static struct ieee80211vap *rt2560_vap_create(struct ieee80211com *, 88 const char name[IFNAMSIZ], int unit, int opmode, 89 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], 90 const uint8_t mac[IEEE80211_ADDR_LEN]); 91static void rt2560_vap_delete(struct ieee80211vap *); 92static void rt2560_dma_map_addr(void *, bus_dma_segment_t *, int, 93 int); 94static int rt2560_alloc_tx_ring(struct rt2560_softc *, 95 struct rt2560_tx_ring *, int); 96static void rt2560_reset_tx_ring(struct rt2560_softc *, 97 struct rt2560_tx_ring *); 98static void rt2560_free_tx_ring(struct rt2560_softc *, 99 struct rt2560_tx_ring *); 100static int rt2560_alloc_rx_ring(struct rt2560_softc *, 101 struct rt2560_rx_ring *, int); 102static void rt2560_reset_rx_ring(struct rt2560_softc *, 103 struct rt2560_rx_ring *); 104static void rt2560_free_rx_ring(struct rt2560_softc *, 105 struct rt2560_rx_ring *); 106static struct ieee80211_node *rt2560_node_alloc(struct ieee80211vap *, 107 const uint8_t [IEEE80211_ADDR_LEN]); 108static void rt2560_newassoc(struct ieee80211_node *, int); 109static int rt2560_newstate(struct ieee80211vap *, 110 enum ieee80211_state, int); 111static uint16_t rt2560_eeprom_read(struct rt2560_softc *, uint8_t); 112static void rt2560_encryption_intr(struct rt2560_softc *); 113static void rt2560_tx_intr(struct rt2560_softc *); 114static void rt2560_prio_intr(struct rt2560_softc *); 115static void rt2560_decryption_intr(struct rt2560_softc *); 116static void rt2560_rx_intr(struct rt2560_softc *); 117static void rt2560_beacon_update(struct ieee80211vap *, int item); 118static void rt2560_beacon_expire(struct rt2560_softc *); 119static void rt2560_wakeup_expire(struct rt2560_softc *); 120static void rt2560_scan_start(struct ieee80211com *); 121static void rt2560_scan_end(struct ieee80211com *); 122static void rt2560_set_channel(struct ieee80211com *); 123static void rt2560_setup_tx_desc(struct rt2560_softc *, 124 struct rt2560_tx_desc *, uint32_t, int, int, int, 125 bus_addr_t); 126static int rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *, 127 struct ieee80211_node *); 128static int rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *, 129 struct ieee80211_node *); 130static int rt2560_tx_data(struct rt2560_softc *, struct mbuf *, 131 struct ieee80211_node *); 132static void rt2560_start_locked(struct ifnet *); 133static void rt2560_start(struct ifnet *); 134static void rt2560_watchdog(void *); 135static int rt2560_ioctl(struct ifnet *, u_long, caddr_t); 136static void rt2560_bbp_write(struct rt2560_softc *, uint8_t, 137 uint8_t); 138static uint8_t rt2560_bbp_read(struct rt2560_softc *, uint8_t); 139static void rt2560_rf_write(struct rt2560_softc *, uint8_t, 140 uint32_t); 141static void rt2560_set_chan(struct rt2560_softc *, 142 struct ieee80211_channel *); 143#if 0 144static void rt2560_disable_rf_tune(struct rt2560_softc *); 145#endif 146static void rt2560_enable_tsf_sync(struct rt2560_softc *); 147static void rt2560_update_plcp(struct rt2560_softc *); 148static void rt2560_update_slot(struct ifnet *); 149static void rt2560_set_basicrates(struct rt2560_softc *); 150static void rt2560_update_led(struct rt2560_softc *, int, int); 151static void rt2560_set_bssid(struct rt2560_softc *, const uint8_t *); 152static void rt2560_set_macaddr(struct rt2560_softc *, uint8_t *); 153static void rt2560_get_macaddr(struct rt2560_softc *, uint8_t *); 154static void rt2560_update_promisc(struct ifnet *); 155static const char *rt2560_get_rf(int); 156static void rt2560_read_config(struct rt2560_softc *); 157static int rt2560_bbp_init(struct rt2560_softc *); 158static void rt2560_set_txantenna(struct rt2560_softc *, int); 159static void rt2560_set_rxantenna(struct rt2560_softc *, int); 160static void rt2560_init_locked(struct rt2560_softc *); 161static void rt2560_init(void *); 162static void rt2560_stop_locked(struct rt2560_softc *); 163static int rt2560_raw_xmit(struct ieee80211_node *, struct mbuf *, 164 const struct ieee80211_bpf_params *); 165 166static const struct { 167 uint32_t reg; 168 uint32_t val; 169} rt2560_def_mac[] = { 170 RT2560_DEF_MAC 171}; 172 173static const struct { 174 uint8_t reg; 175 uint8_t val; 176} rt2560_def_bbp[] = { 177 RT2560_DEF_BBP 178}; 179 180static const uint32_t rt2560_rf2522_r2[] = RT2560_RF2522_R2; 181static const uint32_t rt2560_rf2523_r2[] = RT2560_RF2523_R2; 182static const uint32_t rt2560_rf2524_r2[] = RT2560_RF2524_R2; 183static const uint32_t rt2560_rf2525_r2[] = RT2560_RF2525_R2; 184static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2; 185static const uint32_t rt2560_rf2525e_r2[] = RT2560_RF2525E_R2; 186static const uint32_t rt2560_rf2526_r2[] = RT2560_RF2526_R2; 187static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2; 188 189static const struct { 190 uint8_t chan; 191 uint32_t r1, r2, r4; 192} rt2560_rf5222[] = { 193 RT2560_RF5222 194}; 195 196int 197rt2560_attach(device_t dev, int id) 198{ 199 struct rt2560_softc *sc = device_get_softc(dev); 200 struct ieee80211com *ic; 201 struct ifnet *ifp; 202 int error; 203 uint8_t bands; 204 uint8_t macaddr[IEEE80211_ADDR_LEN]; 205 206 sc->sc_dev = dev; 207 208 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 209 MTX_DEF | MTX_RECURSE); 210 211 callout_init_mtx(&sc->watchdog_ch, &sc->sc_mtx, 0); 212 213 /* retrieve RT2560 rev. no */ 214 sc->asic_rev = RAL_READ(sc, RT2560_CSR0); 215 216 /* retrieve RF rev. no and various other things from EEPROM */ 217 rt2560_read_config(sc); 218 219 device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n", 220 sc->asic_rev, rt2560_get_rf(sc->rf_rev)); 221 222 /* 223 * Allocate Tx and Rx rings. 224 */ 225 error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT); 226 if (error != 0) { 227 device_printf(sc->sc_dev, "could not allocate Tx ring\n"); 228 goto fail1; 229 } 230 231 error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT); 232 if (error != 0) { 233 device_printf(sc->sc_dev, "could not allocate ATIM ring\n"); 234 goto fail2; 235 } 236 237 error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT); 238 if (error != 0) { 239 device_printf(sc->sc_dev, "could not allocate Prio ring\n"); 240 goto fail3; 241 } 242 243 error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT); 244 if (error != 0) { 245 device_printf(sc->sc_dev, "could not allocate Beacon ring\n"); 246 goto fail4; 247 } 248 249 error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT); 250 if (error != 0) { 251 device_printf(sc->sc_dev, "could not allocate Rx ring\n"); 252 goto fail5; 253 } 254 255 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 256 if (ifp == NULL) { 257 device_printf(sc->sc_dev, "can not if_alloc()\n"); 258 goto fail6; 259 } 260 ic = ifp->if_l2com; 261 262 /* retrieve MAC address */ 263 rt2560_get_macaddr(sc, macaddr); 264 265 ifp->if_softc = sc; 266 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 267 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 268 ifp->if_init = rt2560_init; 269 ifp->if_ioctl = rt2560_ioctl; 270 ifp->if_start = rt2560_start; 271 IFQ_SET_MAXLEN(&ifp->if_snd, IFQ_MAXLEN); 272 ifp->if_snd.ifq_drv_maxlen = IFQ_MAXLEN; 273 IFQ_SET_READY(&ifp->if_snd); 274 275 ic->ic_ifp = ifp; 276 ic->ic_opmode = IEEE80211_M_STA; 277 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 278 279 /* set device capabilities */ 280 ic->ic_caps = 281 IEEE80211_C_STA /* station mode */ 282 | IEEE80211_C_IBSS /* ibss, nee adhoc, mode */ 283 | IEEE80211_C_HOSTAP /* hostap mode */ 284 | IEEE80211_C_MONITOR /* monitor mode */ 285 | IEEE80211_C_AHDEMO /* adhoc demo mode */ 286 | IEEE80211_C_WDS /* 4-address traffic works */ 287 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 288 | IEEE80211_C_SHSLOT /* short slot time supported */ 289 | IEEE80211_C_WPA /* capable of WPA1+WPA2 */ 290 | IEEE80211_C_BGSCAN /* capable of bg scanning */ 291#ifdef notyet 292 | IEEE80211_C_TXFRAG /* handle tx frags */ 293#endif 294 ; 295 296 bands = 0; 297 setbit(&bands, IEEE80211_MODE_11B); 298 setbit(&bands, IEEE80211_MODE_11G); 299 if (sc->rf_rev == RT2560_RF_5222) 300 setbit(&bands, IEEE80211_MODE_11A); 301 ieee80211_init_channels(ic, NULL, &bands); 302 303 ieee80211_ifattach(ic, macaddr); 304 ic->ic_newassoc = rt2560_newassoc; 305 ic->ic_raw_xmit = rt2560_raw_xmit; 306 ic->ic_updateslot = rt2560_update_slot; 307 ic->ic_update_promisc = rt2560_update_promisc; 308 ic->ic_node_alloc = rt2560_node_alloc; 309 ic->ic_scan_start = rt2560_scan_start; 310 ic->ic_scan_end = rt2560_scan_end; 311 ic->ic_set_channel = rt2560_set_channel; 312 313 ic->ic_vap_create = rt2560_vap_create; 314 ic->ic_vap_delete = rt2560_vap_delete; 315 316 bpfattach(ifp, DLT_IEEE802_11_RADIO, 317 sizeof (struct ieee80211_frame) + sizeof (sc->sc_txtap)); 318 319 sc->sc_rxtap_len = sizeof sc->sc_rxtap; 320 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len); 321 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2560_RX_RADIOTAP_PRESENT); 322 323 sc->sc_txtap_len = sizeof sc->sc_txtap; 324 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len); 325 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2560_TX_RADIOTAP_PRESENT); 326 327 /* 328 * Add a few sysctl knobs. 329 */ 330#ifdef RAL_DEBUG 331 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 332 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 333 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "debug msgs"); 334#endif 335 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 336 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 337 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)"); 338 339 SYSCTL_ADD_INT(device_get_sysctl_ctx(dev), 340 SYSCTL_CHILDREN(device_get_sysctl_tree(dev)), OID_AUTO, 341 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)"); 342 343 if (bootverbose) 344 ieee80211_announce(ic); 345 346 return 0; 347 348fail6: rt2560_free_rx_ring(sc, &sc->rxq); 349fail5: rt2560_free_tx_ring(sc, &sc->bcnq); 350fail4: rt2560_free_tx_ring(sc, &sc->prioq); 351fail3: rt2560_free_tx_ring(sc, &sc->atimq); 352fail2: rt2560_free_tx_ring(sc, &sc->txq); 353fail1: mtx_destroy(&sc->sc_mtx); 354 355 return ENXIO; 356} 357 358int 359rt2560_detach(void *xsc) 360{ 361 struct rt2560_softc *sc = xsc; 362 struct ifnet *ifp = sc->sc_ifp; 363 struct ieee80211com *ic = ifp->if_l2com; 364 365 rt2560_stop(sc); 366 367 bpfdetach(ifp); 368 ieee80211_ifdetach(ic); 369 370 rt2560_free_tx_ring(sc, &sc->txq); 371 rt2560_free_tx_ring(sc, &sc->atimq); 372 rt2560_free_tx_ring(sc, &sc->prioq); 373 rt2560_free_tx_ring(sc, &sc->bcnq); 374 rt2560_free_rx_ring(sc, &sc->rxq); 375 376 if_free(ifp); 377 378 mtx_destroy(&sc->sc_mtx); 379 380 return 0; 381} 382 383static struct ieee80211vap * 384rt2560_vap_create(struct ieee80211com *ic, 385 const char name[IFNAMSIZ], int unit, int opmode, int flags, 386 const uint8_t bssid[IEEE80211_ADDR_LEN], 387 const uint8_t mac[IEEE80211_ADDR_LEN]) 388{ 389 struct ifnet *ifp = ic->ic_ifp; 390 struct rt2560_vap *rvp; 391 struct ieee80211vap *vap; 392 393 switch (opmode) { 394 case IEEE80211_M_STA: 395 case IEEE80211_M_IBSS: 396 case IEEE80211_M_AHDEMO: 397 case IEEE80211_M_MONITOR: 398 case IEEE80211_M_HOSTAP: 399 if (!TAILQ_EMPTY(&ic->ic_vaps)) { 400 if_printf(ifp, "only 1 vap supported\n"); 401 return NULL; 402 } 403 if (opmode == IEEE80211_M_STA) 404 flags |= IEEE80211_CLONE_NOBEACONS; 405 break; 406 case IEEE80211_M_WDS: 407 if (TAILQ_EMPTY(&ic->ic_vaps) || 408 ic->ic_opmode != IEEE80211_M_HOSTAP) { 409 if_printf(ifp, "wds only supported in ap mode\n"); 410 return NULL; 411 } 412 /* 413 * Silently remove any request for a unique 414 * bssid; WDS vap's always share the local 415 * mac address. 416 */ 417 flags &= ~IEEE80211_CLONE_BSSID; 418 break; 419 default: 420 if_printf(ifp, "unknown opmode %d\n", opmode); 421 return NULL; 422 } 423 rvp = (struct rt2560_vap *) malloc(sizeof(struct rt2560_vap), 424 M_80211_VAP, M_NOWAIT | M_ZERO); 425 if (rvp == NULL) 426 return NULL; 427 vap = &rvp->ral_vap; 428 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac); 429 430 /* override state transition machine */ 431 rvp->ral_newstate = vap->iv_newstate; 432 vap->iv_newstate = rt2560_newstate; 433 vap->iv_update_beacon = rt2560_beacon_update; 434 435 ieee80211_amrr_init(&rvp->amrr, vap, 436 IEEE80211_AMRR_MIN_SUCCESS_THRESHOLD, 437 IEEE80211_AMRR_MAX_SUCCESS_THRESHOLD, 438 500 /* ms */); 439 440 /* complete setup */ 441 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status); 442 if (TAILQ_FIRST(&ic->ic_vaps) == vap) 443 ic->ic_opmode = opmode; 444 return vap; 445} 446 447static void 448rt2560_vap_delete(struct ieee80211vap *vap) 449{ 450 struct rt2560_vap *rvp = RT2560_VAP(vap); 451 452 ieee80211_amrr_cleanup(&rvp->amrr); 453 ieee80211_vap_detach(vap); 454 free(rvp, M_80211_VAP); 455} 456 457void 458rt2560_resume(void *xsc) 459{ 460 struct rt2560_softc *sc = xsc; 461 struct ifnet *ifp = sc->sc_ifp; 462 463 if (ifp->if_flags & IFF_UP) 464 rt2560_init(sc); 465} 466 467static void 468rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 469{ 470 if (error != 0) 471 return; 472 473 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 474 475 *(bus_addr_t *)arg = segs[0].ds_addr; 476} 477 478static int 479rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring, 480 int count) 481{ 482 int i, error; 483 484 ring->count = count; 485 ring->queued = 0; 486 ring->cur = ring->next = 0; 487 ring->cur_encrypt = ring->next_encrypt = 0; 488 489 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0, 490 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 491 count * RT2560_TX_DESC_SIZE, 1, count * RT2560_TX_DESC_SIZE, 492 0, NULL, NULL, &ring->desc_dmat); 493 if (error != 0) { 494 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 495 goto fail; 496 } 497 498 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 499 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 500 if (error != 0) { 501 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 502 goto fail; 503 } 504 505 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 506 count * RT2560_TX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr, 507 0); 508 if (error != 0) { 509 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 510 goto fail; 511 } 512 513 ring->data = malloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF, 514 M_NOWAIT | M_ZERO); 515 if (ring->data == NULL) { 516 device_printf(sc->sc_dev, "could not allocate soft data\n"); 517 error = ENOMEM; 518 goto fail; 519 } 520 521 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 522 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 523 MCLBYTES, RT2560_MAX_SCATTER, MCLBYTES, 0, NULL, NULL, 524 &ring->data_dmat); 525 if (error != 0) { 526 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 527 goto fail; 528 } 529 530 for (i = 0; i < count; i++) { 531 error = bus_dmamap_create(ring->data_dmat, 0, 532 &ring->data[i].map); 533 if (error != 0) { 534 device_printf(sc->sc_dev, "could not create DMA map\n"); 535 goto fail; 536 } 537 } 538 539 return 0; 540 541fail: rt2560_free_tx_ring(sc, ring); 542 return error; 543} 544 545static void 546rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring) 547{ 548 struct rt2560_tx_desc *desc; 549 struct rt2560_tx_data *data; 550 int i; 551 552 for (i = 0; i < ring->count; i++) { 553 desc = &ring->desc[i]; 554 data = &ring->data[i]; 555 556 if (data->m != NULL) { 557 bus_dmamap_sync(ring->data_dmat, data->map, 558 BUS_DMASYNC_POSTWRITE); 559 bus_dmamap_unload(ring->data_dmat, data->map); 560 m_freem(data->m); 561 data->m = NULL; 562 } 563 564 if (data->ni != NULL) { 565 ieee80211_free_node(data->ni); 566 data->ni = NULL; 567 } 568 569 desc->flags = 0; 570 } 571 572 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE); 573 574 ring->queued = 0; 575 ring->cur = ring->next = 0; 576 ring->cur_encrypt = ring->next_encrypt = 0; 577} 578 579static void 580rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring) 581{ 582 struct rt2560_tx_data *data; 583 int i; 584 585 if (ring->desc != NULL) { 586 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 587 BUS_DMASYNC_POSTWRITE); 588 bus_dmamap_unload(ring->desc_dmat, ring->desc_map); 589 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map); 590 } 591 592 if (ring->desc_dmat != NULL) 593 bus_dma_tag_destroy(ring->desc_dmat); 594 595 if (ring->data != NULL) { 596 for (i = 0; i < ring->count; i++) { 597 data = &ring->data[i]; 598 599 if (data->m != NULL) { 600 bus_dmamap_sync(ring->data_dmat, data->map, 601 BUS_DMASYNC_POSTWRITE); 602 bus_dmamap_unload(ring->data_dmat, data->map); 603 m_freem(data->m); 604 } 605 606 if (data->ni != NULL) 607 ieee80211_free_node(data->ni); 608 609 if (data->map != NULL) 610 bus_dmamap_destroy(ring->data_dmat, data->map); 611 } 612 613 free(ring->data, M_DEVBUF); 614 } 615 616 if (ring->data_dmat != NULL) 617 bus_dma_tag_destroy(ring->data_dmat); 618} 619 620static int 621rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring, 622 int count) 623{ 624 struct rt2560_rx_desc *desc; 625 struct rt2560_rx_data *data; 626 bus_addr_t physaddr; 627 int i, error; 628 629 ring->count = count; 630 ring->cur = ring->next = 0; 631 ring->cur_decrypt = 0; 632 633 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 4, 0, 634 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 635 count * RT2560_RX_DESC_SIZE, 1, count * RT2560_RX_DESC_SIZE, 636 0, NULL, NULL, &ring->desc_dmat); 637 if (error != 0) { 638 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 639 goto fail; 640 } 641 642 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 643 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 644 if (error != 0) { 645 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 646 goto fail; 647 } 648 649 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 650 count * RT2560_RX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr, 651 0); 652 if (error != 0) { 653 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 654 goto fail; 655 } 656 657 ring->data = malloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF, 658 M_NOWAIT | M_ZERO); 659 if (ring->data == NULL) { 660 device_printf(sc->sc_dev, "could not allocate soft data\n"); 661 error = ENOMEM; 662 goto fail; 663 } 664 665 /* 666 * Pre-allocate Rx buffers and populate Rx ring. 667 */ 668 error = bus_dma_tag_create(bus_get_dma_tag(sc->sc_dev), 1, 0, 669 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 670 1, MCLBYTES, 0, NULL, NULL, &ring->data_dmat); 671 if (error != 0) { 672 device_printf(sc->sc_dev, "could not create data DMA tag\n"); 673 goto fail; 674 } 675 676 for (i = 0; i < count; i++) { 677 desc = &sc->rxq.desc[i]; 678 data = &sc->rxq.data[i]; 679 680 error = bus_dmamap_create(ring->data_dmat, 0, &data->map); 681 if (error != 0) { 682 device_printf(sc->sc_dev, "could not create DMA map\n"); 683 goto fail; 684 } 685 686 data->m = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 687 if (data->m == NULL) { 688 device_printf(sc->sc_dev, 689 "could not allocate rx mbuf\n"); 690 error = ENOMEM; 691 goto fail; 692 } 693 694 error = bus_dmamap_load(ring->data_dmat, data->map, 695 mtod(data->m, void *), MCLBYTES, rt2560_dma_map_addr, 696 &physaddr, 0); 697 if (error != 0) { 698 device_printf(sc->sc_dev, 699 "could not load rx buf DMA map"); 700 goto fail; 701 } 702 703 desc->flags = htole32(RT2560_RX_BUSY); 704 desc->physaddr = htole32(physaddr); 705 } 706 707 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE); 708 709 return 0; 710 711fail: rt2560_free_rx_ring(sc, ring); 712 return error; 713} 714 715static void 716rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring) 717{ 718 int i; 719 720 for (i = 0; i < ring->count; i++) { 721 ring->desc[i].flags = htole32(RT2560_RX_BUSY); 722 ring->data[i].drop = 0; 723 } 724 725 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE); 726 727 ring->cur = ring->next = 0; 728 ring->cur_decrypt = 0; 729} 730 731static void 732rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring) 733{ 734 struct rt2560_rx_data *data; 735 int i; 736 737 if (ring->desc != NULL) { 738 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, 739 BUS_DMASYNC_POSTWRITE); 740 bus_dmamap_unload(ring->desc_dmat, ring->desc_map); 741 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map); 742 } 743 744 if (ring->desc_dmat != NULL) 745 bus_dma_tag_destroy(ring->desc_dmat); 746 747 if (ring->data != NULL) { 748 for (i = 0; i < ring->count; i++) { 749 data = &ring->data[i]; 750 751 if (data->m != NULL) { 752 bus_dmamap_sync(ring->data_dmat, data->map, 753 BUS_DMASYNC_POSTREAD); 754 bus_dmamap_unload(ring->data_dmat, data->map); 755 m_freem(data->m); 756 } 757 758 if (data->map != NULL) 759 bus_dmamap_destroy(ring->data_dmat, data->map); 760 } 761 762 free(ring->data, M_DEVBUF); 763 } 764 765 if (ring->data_dmat != NULL) 766 bus_dma_tag_destroy(ring->data_dmat); 767} 768 769static struct ieee80211_node * 770rt2560_node_alloc(struct ieee80211vap *vap, 771 const uint8_t mac[IEEE80211_ADDR_LEN]) 772{ 773 struct rt2560_node *rn; 774 775 rn = malloc(sizeof (struct rt2560_node), M_80211_NODE, 776 M_NOWAIT | M_ZERO); 777 778 return (rn != NULL) ? &rn->ni : NULL; 779} 780 781static void 782rt2560_newassoc(struct ieee80211_node *ni, int isnew) 783{ 784 struct ieee80211vap *vap = ni->ni_vap; 785 786 ieee80211_amrr_node_init(&RT2560_VAP(vap)->amrr, 787 &RT2560_NODE(ni)->amrr, ni); 788} 789 790static int 791rt2560_newstate(struct ieee80211vap *vap, enum ieee80211_state nstate, int arg) 792{ 793 struct rt2560_vap *rvp = RT2560_VAP(vap); 794 struct ifnet *ifp = vap->iv_ic->ic_ifp; 795 struct rt2560_softc *sc = ifp->if_softc; 796 int error; 797 798 if (nstate == IEEE80211_S_INIT && vap->iv_state == IEEE80211_S_RUN) { 799 /* abort TSF synchronization */ 800 RAL_WRITE(sc, RT2560_CSR14, 0); 801 802 /* turn association led off */ 803 rt2560_update_led(sc, 0, 0); 804 } 805 806 error = rvp->ral_newstate(vap, nstate, arg); 807 808 if (error == 0 && nstate == IEEE80211_S_RUN) { 809 struct ieee80211_node *ni = vap->iv_bss; 810 struct mbuf *m; 811 812 if (vap->iv_opmode != IEEE80211_M_MONITOR) { 813 rt2560_update_plcp(sc); 814 rt2560_set_basicrates(sc); 815 rt2560_set_bssid(sc, ni->ni_bssid); 816 } 817 818 if (vap->iv_opmode == IEEE80211_M_HOSTAP || 819 vap->iv_opmode == IEEE80211_M_IBSS) { 820 m = ieee80211_beacon_alloc(ni, &rvp->ral_bo); 821 if (m == NULL) { 822 if_printf(ifp, "could not allocate beacon\n"); 823 return ENOBUFS; 824 } 825 ieee80211_ref_node(ni); 826 error = rt2560_tx_bcn(sc, m, ni); 827 if (error != 0) 828 return error; 829 } 830 831 /* turn assocation led on */ 832 rt2560_update_led(sc, 1, 0); 833 834 if (vap->iv_opmode != IEEE80211_M_MONITOR) 835 rt2560_enable_tsf_sync(sc); 836 } 837 return error; 838} 839 840/* 841 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or 842 * 93C66). 843 */ 844static uint16_t 845rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr) 846{ 847 uint32_t tmp; 848 uint16_t val; 849 int n; 850 851 /* clock C once before the first command */ 852 RT2560_EEPROM_CTL(sc, 0); 853 854 RT2560_EEPROM_CTL(sc, RT2560_S); 855 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C); 856 RT2560_EEPROM_CTL(sc, RT2560_S); 857 858 /* write start bit (1) */ 859 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D); 860 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C); 861 862 /* write READ opcode (10) */ 863 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D); 864 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C); 865 RT2560_EEPROM_CTL(sc, RT2560_S); 866 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C); 867 868 /* write address (A5-A0 or A7-A0) */ 869 n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7; 870 for (; n >= 0; n--) { 871 RT2560_EEPROM_CTL(sc, RT2560_S | 872 (((addr >> n) & 1) << RT2560_SHIFT_D)); 873 RT2560_EEPROM_CTL(sc, RT2560_S | 874 (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C); 875 } 876 877 RT2560_EEPROM_CTL(sc, RT2560_S); 878 879 /* read data Q15-Q0 */ 880 val = 0; 881 for (n = 15; n >= 0; n--) { 882 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C); 883 tmp = RAL_READ(sc, RT2560_CSR21); 884 val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n; 885 RT2560_EEPROM_CTL(sc, RT2560_S); 886 } 887 888 RT2560_EEPROM_CTL(sc, 0); 889 890 /* clear Chip Select and clock C */ 891 RT2560_EEPROM_CTL(sc, RT2560_S); 892 RT2560_EEPROM_CTL(sc, 0); 893 RT2560_EEPROM_CTL(sc, RT2560_C); 894 895 return val; 896} 897 898/* 899 * Some frames were processed by the hardware cipher engine and are ready for 900 * transmission. 901 */ 902static void 903rt2560_encryption_intr(struct rt2560_softc *sc) 904{ 905 struct rt2560_tx_desc *desc; 906 int hw; 907 908 /* retrieve last descriptor index processed by cipher engine */ 909 hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr; 910 hw /= RT2560_TX_DESC_SIZE; 911 912 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 913 BUS_DMASYNC_POSTREAD); 914 915 while (sc->txq.next_encrypt != hw) { 916 if (sc->txq.next_encrypt == sc->txq.cur_encrypt) { 917 printf("hw encrypt %d, cur_encrypt %d\n", hw, 918 sc->txq.cur_encrypt); 919 break; 920 } 921 922 desc = &sc->txq.desc[sc->txq.next_encrypt]; 923 924 if ((le32toh(desc->flags) & RT2560_TX_BUSY) || 925 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY)) 926 break; 927 928 /* for TKIP, swap eiv field to fix a bug in ASIC */ 929 if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) == 930 RT2560_TX_CIPHER_TKIP) 931 desc->eiv = bswap32(desc->eiv); 932 933 /* mark the frame ready for transmission */ 934 desc->flags |= htole32(RT2560_TX_VALID); 935 desc->flags |= htole32(RT2560_TX_BUSY); 936 937 DPRINTFN(sc, 15, "encryption done idx=%u\n", 938 sc->txq.next_encrypt); 939 940 sc->txq.next_encrypt = 941 (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT; 942 } 943 944 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 945 BUS_DMASYNC_PREWRITE); 946 947 /* kick Tx */ 948 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX); 949} 950 951static void 952rt2560_tx_intr(struct rt2560_softc *sc) 953{ 954 struct ifnet *ifp = sc->sc_ifp; 955 struct rt2560_tx_desc *desc; 956 struct rt2560_tx_data *data; 957 struct rt2560_node *rn; 958 struct mbuf *m; 959 uint32_t flags; 960 int retrycnt; 961 962 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 963 BUS_DMASYNC_POSTREAD); 964 965 for (;;) { 966 desc = &sc->txq.desc[sc->txq.next]; 967 data = &sc->txq.data[sc->txq.next]; 968 969 flags = le32toh(desc->flags); 970 if ((flags & RT2560_TX_BUSY) || 971 (flags & RT2560_TX_CIPHER_BUSY) || 972 !(flags & RT2560_TX_VALID)) 973 break; 974 975 rn = (struct rt2560_node *)data->ni; 976 m = data->m; 977 978 switch (flags & RT2560_TX_RESULT_MASK) { 979 case RT2560_TX_SUCCESS: 980 DPRINTFN(sc, 10, "%s\n", "data frame sent successfully"); 981 if (data->rix != IEEE80211_FIXED_RATE_NONE) 982 ieee80211_amrr_tx_complete(&rn->amrr, 983 IEEE80211_AMRR_SUCCESS, 0); 984 ifp->if_opackets++; 985 break; 986 987 case RT2560_TX_SUCCESS_RETRY: 988 retrycnt = RT2560_TX_RETRYCNT(flags); 989 990 DPRINTFN(sc, 9, "data frame sent after %u retries\n", 991 retrycnt); 992 if (data->rix != IEEE80211_FIXED_RATE_NONE) 993 ieee80211_amrr_tx_complete(&rn->amrr, 994 IEEE80211_AMRR_SUCCESS, retrycnt); 995 ifp->if_opackets++; 996 break; 997 998 case RT2560_TX_FAIL_RETRY: 999 retrycnt = RT2560_TX_RETRYCNT(flags); 1000 1001 DPRINTFN(sc, 9, "data frame failed after %d retries\n", 1002 retrycnt); 1003 if (data->rix != IEEE80211_FIXED_RATE_NONE) 1004 ieee80211_amrr_tx_complete(&rn->amrr, 1005 IEEE80211_AMRR_FAILURE, retrycnt); 1006 ifp->if_oerrors++; 1007 break; 1008 1009 case RT2560_TX_FAIL_INVALID: 1010 case RT2560_TX_FAIL_OTHER: 1011 default: 1012 device_printf(sc->sc_dev, "sending data frame failed " 1013 "0x%08x\n", flags); 1014 ifp->if_oerrors++; 1015 } 1016 1017 bus_dmamap_sync(sc->txq.data_dmat, data->map, 1018 BUS_DMASYNC_POSTWRITE); 1019 bus_dmamap_unload(sc->txq.data_dmat, data->map); 1020 m_freem(m); 1021 data->m = NULL; 1022 ieee80211_free_node(data->ni); 1023 data->ni = NULL; 1024 1025 /* descriptor is no longer valid */ 1026 desc->flags &= ~htole32(RT2560_TX_VALID); 1027 1028 DPRINTFN(sc, 15, "tx done idx=%u\n", sc->txq.next); 1029 1030 sc->txq.queued--; 1031 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT; 1032 } 1033 1034 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 1035 BUS_DMASYNC_PREWRITE); 1036 1037 if (sc->prioq.queued == 0 && sc->txq.queued == 0) 1038 sc->sc_tx_timer = 0; 1039 1040 if (sc->txq.queued < RT2560_TX_RING_COUNT - 1) { 1041 sc->sc_flags &= ~RT2560_F_DATA_OACTIVE; 1042 if ((sc->sc_flags & 1043 (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0) 1044 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1045 rt2560_start_locked(ifp); 1046 } 1047} 1048 1049static void 1050rt2560_prio_intr(struct rt2560_softc *sc) 1051{ 1052 struct ifnet *ifp = sc->sc_ifp; 1053 struct rt2560_tx_desc *desc; 1054 struct rt2560_tx_data *data; 1055 struct ieee80211_node *ni; 1056 struct mbuf *m; 1057 int flags; 1058 1059 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1060 BUS_DMASYNC_POSTREAD); 1061 1062 for (;;) { 1063 desc = &sc->prioq.desc[sc->prioq.next]; 1064 data = &sc->prioq.data[sc->prioq.next]; 1065 1066 flags = le32toh(desc->flags); 1067 if ((flags & RT2560_TX_BUSY) || (flags & RT2560_TX_VALID) == 0) 1068 break; 1069 1070 switch (flags & RT2560_TX_RESULT_MASK) { 1071 case RT2560_TX_SUCCESS: 1072 DPRINTFN(sc, 10, "%s\n", "mgt frame sent successfully"); 1073 break; 1074 1075 case RT2560_TX_SUCCESS_RETRY: 1076 DPRINTFN(sc, 9, "mgt frame sent after %u retries\n", 1077 (flags >> 5) & 0x7); 1078 break; 1079 1080 case RT2560_TX_FAIL_RETRY: 1081 DPRINTFN(sc, 9, "%s\n", 1082 "sending mgt frame failed (too much retries)"); 1083 break; 1084 1085 case RT2560_TX_FAIL_INVALID: 1086 case RT2560_TX_FAIL_OTHER: 1087 default: 1088 device_printf(sc->sc_dev, "sending mgt frame failed " 1089 "0x%08x\n", flags); 1090 break; 1091 } 1092 1093 bus_dmamap_sync(sc->prioq.data_dmat, data->map, 1094 BUS_DMASYNC_POSTWRITE); 1095 bus_dmamap_unload(sc->prioq.data_dmat, data->map); 1096 1097 m = data->m; 1098 data->m = NULL; 1099 ni = data->ni; 1100 data->ni = NULL; 1101 1102 /* descriptor is no longer valid */ 1103 desc->flags &= ~htole32(RT2560_TX_VALID); 1104 1105 DPRINTFN(sc, 15, "prio done idx=%u\n", sc->prioq.next); 1106 1107 sc->prioq.queued--; 1108 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT; 1109 1110 if (m->m_flags & M_TXCB) 1111 ieee80211_process_callback(ni, m, 1112 (flags & RT2560_TX_RESULT_MASK) &~ 1113 (RT2560_TX_SUCCESS | RT2560_TX_SUCCESS_RETRY)); 1114 m_freem(m); 1115 ieee80211_free_node(ni); 1116 } 1117 1118 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1119 BUS_DMASYNC_PREWRITE); 1120 1121 if (sc->prioq.queued == 0 && sc->txq.queued == 0) 1122 sc->sc_tx_timer = 0; 1123 1124 if (sc->prioq.queued < RT2560_PRIO_RING_COUNT) { 1125 sc->sc_flags &= ~RT2560_F_PRIO_OACTIVE; 1126 if ((sc->sc_flags & 1127 (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0) 1128 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 1129 rt2560_start_locked(ifp); 1130 } 1131} 1132 1133/* 1134 * Some frames were processed by the hardware cipher engine and are ready for 1135 * handoff to the IEEE802.11 layer. 1136 */ 1137static void 1138rt2560_decryption_intr(struct rt2560_softc *sc) 1139{ 1140 struct ifnet *ifp = sc->sc_ifp; 1141 struct ieee80211com *ic = ifp->if_l2com; 1142 struct rt2560_rx_desc *desc; 1143 struct rt2560_rx_data *data; 1144 bus_addr_t physaddr; 1145 struct ieee80211_frame *wh; 1146 struct ieee80211_node *ni; 1147 struct mbuf *mnew, *m; 1148 int hw, error; 1149 1150 /* retrieve last decriptor index processed by cipher engine */ 1151 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr; 1152 hw /= RT2560_RX_DESC_SIZE; 1153 1154 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1155 BUS_DMASYNC_POSTREAD); 1156 1157 for (; sc->rxq.cur_decrypt != hw;) { 1158 desc = &sc->rxq.desc[sc->rxq.cur_decrypt]; 1159 data = &sc->rxq.data[sc->rxq.cur_decrypt]; 1160 1161 if ((le32toh(desc->flags) & RT2560_RX_BUSY) || 1162 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY)) 1163 break; 1164 1165 if (data->drop) { 1166 ifp->if_ierrors++; 1167 goto skip; 1168 } 1169 1170 if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 && 1171 (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) { 1172 ifp->if_ierrors++; 1173 goto skip; 1174 } 1175 1176 /* 1177 * Try to allocate a new mbuf for this ring element and load it 1178 * before processing the current mbuf. If the ring element 1179 * cannot be loaded, drop the received packet and reuse the old 1180 * mbuf. In the unlikely case that the old mbuf can't be 1181 * reloaded either, explicitly panic. 1182 */ 1183 mnew = m_getcl(M_DONTWAIT, MT_DATA, M_PKTHDR); 1184 if (mnew == NULL) { 1185 ifp->if_ierrors++; 1186 goto skip; 1187 } 1188 1189 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 1190 BUS_DMASYNC_POSTREAD); 1191 bus_dmamap_unload(sc->rxq.data_dmat, data->map); 1192 1193 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1194 mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr, 1195 &physaddr, 0); 1196 if (error != 0) { 1197 m_freem(mnew); 1198 1199 /* try to reload the old mbuf */ 1200 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1201 mtod(data->m, void *), MCLBYTES, 1202 rt2560_dma_map_addr, &physaddr, 0); 1203 if (error != 0) { 1204 /* very unlikely that it will fail... */ 1205 panic("%s: could not load old rx mbuf", 1206 device_get_name(sc->sc_dev)); 1207 } 1208 ifp->if_ierrors++; 1209 goto skip; 1210 } 1211 1212 /* 1213 * New mbuf successfully loaded, update Rx ring and continue 1214 * processing. 1215 */ 1216 m = data->m; 1217 data->m = mnew; 1218 desc->physaddr = htole32(physaddr); 1219 1220 /* finalize mbuf */ 1221 m->m_pkthdr.rcvif = ifp; 1222 m->m_pkthdr.len = m->m_len = 1223 (le32toh(desc->flags) >> 16) & 0xfff; 1224 1225 if (bpf_peers_present(ifp->if_bpf)) { 1226 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap; 1227 uint32_t tsf_lo, tsf_hi; 1228 1229 /* get timestamp (low and high 32 bits) */ 1230 tsf_hi = RAL_READ(sc, RT2560_CSR17); 1231 tsf_lo = RAL_READ(sc, RT2560_CSR16); 1232 1233 tap->wr_tsf = 1234 htole64(((uint64_t)tsf_hi << 32) | tsf_lo); 1235 tap->wr_flags = 0; 1236 tap->wr_rate = ieee80211_plcp2rate(desc->rate, 1237 (desc->flags & htole32(RT2560_RX_OFDM)) ? 1238 IEEE80211_T_OFDM : IEEE80211_T_CCK); 1239 tap->wr_antenna = sc->rx_ant; 1240 tap->wr_antsignal = RT2560_RSSI(sc, desc->rssi); 1241 1242 bpf_mtap2(ifp->if_bpf, tap, sc->sc_rxtap_len, m); 1243 } 1244 1245 sc->sc_flags |= RT2560_F_INPUT_RUNNING; 1246 RAL_UNLOCK(sc); 1247 wh = mtod(m, struct ieee80211_frame *); 1248 ni = ieee80211_find_rxnode(ic, 1249 (struct ieee80211_frame_min *)wh); 1250 if (ni != NULL) { 1251 (void) ieee80211_input(ni, m, 1252 RT2560_RSSI(sc, desc->rssi), RT2560_NOISE_FLOOR, 0); 1253 ieee80211_free_node(ni); 1254 } else 1255 (void) ieee80211_input_all(ic, m, 1256 RT2560_RSSI(sc, desc->rssi), RT2560_NOISE_FLOOR, 0); 1257 1258 RAL_LOCK(sc); 1259 sc->sc_flags &= ~RT2560_F_INPUT_RUNNING; 1260skip: desc->flags = htole32(RT2560_RX_BUSY); 1261 1262 DPRINTFN(sc, 15, "decryption done idx=%u\n", sc->rxq.cur_decrypt); 1263 1264 sc->rxq.cur_decrypt = 1265 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT; 1266 } 1267 1268 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1269 BUS_DMASYNC_PREWRITE); 1270} 1271 1272/* 1273 * Some frames were received. Pass them to the hardware cipher engine before 1274 * sending them to the 802.11 layer. 1275 */ 1276static void 1277rt2560_rx_intr(struct rt2560_softc *sc) 1278{ 1279 struct rt2560_rx_desc *desc; 1280 struct rt2560_rx_data *data; 1281 1282 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1283 BUS_DMASYNC_POSTREAD); 1284 1285 for (;;) { 1286 desc = &sc->rxq.desc[sc->rxq.cur]; 1287 data = &sc->rxq.data[sc->rxq.cur]; 1288 1289 if ((le32toh(desc->flags) & RT2560_RX_BUSY) || 1290 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY)) 1291 break; 1292 1293 data->drop = 0; 1294 1295 if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) || 1296 (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) { 1297 /* 1298 * This should not happen since we did not request 1299 * to receive those frames when we filled RXCSR0. 1300 */ 1301 DPRINTFN(sc, 5, "PHY or CRC error flags 0x%08x\n", 1302 le32toh(desc->flags)); 1303 data->drop = 1; 1304 } 1305 1306 if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) { 1307 DPRINTFN(sc, 5, "%s\n", "bad length"); 1308 data->drop = 1; 1309 } 1310 1311 /* mark the frame for decryption */ 1312 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY); 1313 1314 DPRINTFN(sc, 15, "rx done idx=%u\n", sc->rxq.cur); 1315 1316 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT; 1317 } 1318 1319 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1320 BUS_DMASYNC_PREWRITE); 1321 1322 /* kick decrypt */ 1323 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT); 1324} 1325 1326static void 1327rt2560_beacon_update(struct ieee80211vap *vap, int item) 1328{ 1329 struct rt2560_vap *rvp = RT2560_VAP(vap); 1330 struct ieee80211_beacon_offsets *bo = &rvp->ral_bo; 1331 1332 setbit(bo->bo_flags, item); 1333} 1334 1335/* 1336 * This function is called periodically in IBSS mode when a new beacon must be 1337 * sent out. 1338 */ 1339static void 1340rt2560_beacon_expire(struct rt2560_softc *sc) 1341{ 1342 struct ifnet *ifp = sc->sc_ifp; 1343 struct ieee80211com *ic = ifp->if_l2com; 1344 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 1345 struct rt2560_vap *rvp = RT2560_VAP(vap); 1346 struct rt2560_tx_data *data; 1347 1348 if (ic->ic_opmode != IEEE80211_M_IBSS && 1349 ic->ic_opmode != IEEE80211_M_HOSTAP) 1350 return; 1351 1352 data = &sc->bcnq.data[sc->bcnq.next]; 1353 /* 1354 * Don't send beacon if bsschan isn't set 1355 */ 1356 if (data->ni == NULL) 1357 return; 1358 1359 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE); 1360 bus_dmamap_unload(sc->bcnq.data_dmat, data->map); 1361 1362 /* XXX 1 =>'s mcast frames which means all PS sta's will wakeup! */ 1363 ieee80211_beacon_update(data->ni, &rvp->ral_bo, data->m, 1); 1364 1365 rt2560_tx_bcn(sc, data->m, data->ni); 1366 1367 DPRINTFN(sc, 15, "%s", "beacon expired\n"); 1368 1369 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT; 1370} 1371 1372/* ARGSUSED */ 1373static void 1374rt2560_wakeup_expire(struct rt2560_softc *sc) 1375{ 1376 DPRINTFN(sc, 2, "%s", "wakeup expired\n"); 1377} 1378 1379void 1380rt2560_intr(void *arg) 1381{ 1382 struct rt2560_softc *sc = arg; 1383 struct ifnet *ifp = sc->sc_ifp; 1384 uint32_t r; 1385 1386 RAL_LOCK(sc); 1387 1388 /* disable interrupts */ 1389 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff); 1390 1391 /* don't re-enable interrupts if we're shutting down */ 1392 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 1393 RAL_UNLOCK(sc); 1394 return; 1395 } 1396 1397 r = RAL_READ(sc, RT2560_CSR7); 1398 RAL_WRITE(sc, RT2560_CSR7, r); 1399 1400 if (r & RT2560_BEACON_EXPIRE) 1401 rt2560_beacon_expire(sc); 1402 1403 if (r & RT2560_WAKEUP_EXPIRE) 1404 rt2560_wakeup_expire(sc); 1405 1406 if (r & RT2560_ENCRYPTION_DONE) 1407 rt2560_encryption_intr(sc); 1408 1409 if (r & RT2560_TX_DONE) 1410 rt2560_tx_intr(sc); 1411 1412 if (r & RT2560_PRIO_DONE) 1413 rt2560_prio_intr(sc); 1414 1415 if (r & RT2560_DECRYPTION_DONE) 1416 rt2560_decryption_intr(sc); 1417 1418 if (r & RT2560_RX_DONE) { 1419 rt2560_rx_intr(sc); 1420 rt2560_encryption_intr(sc); 1421 } 1422 1423 /* re-enable interrupts */ 1424 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK); 1425 1426 RAL_UNLOCK(sc); 1427} 1428 1429#define RAL_SIFS 10 /* us */ 1430 1431#define RT2560_TXRX_TURNAROUND 10 /* us */ 1432 1433static uint8_t 1434rt2560_plcp_signal(int rate) 1435{ 1436 switch (rate) { 1437 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1438 case 12: return 0xb; 1439 case 18: return 0xf; 1440 case 24: return 0xa; 1441 case 36: return 0xe; 1442 case 48: return 0x9; 1443 case 72: return 0xd; 1444 case 96: return 0x8; 1445 case 108: return 0xc; 1446 1447 /* CCK rates (NB: not IEEE std, device-specific) */ 1448 case 2: return 0x0; 1449 case 4: return 0x1; 1450 case 11: return 0x2; 1451 case 22: return 0x3; 1452 } 1453 return 0xff; /* XXX unsupported/unknown rate */ 1454} 1455 1456static void 1457rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc, 1458 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr) 1459{ 1460 struct ifnet *ifp = sc->sc_ifp; 1461 struct ieee80211com *ic = ifp->if_l2com; 1462 uint16_t plcp_length; 1463 int remainder; 1464 1465 desc->flags = htole32(flags); 1466 desc->flags |= htole32(len << 16); 1467 1468 desc->physaddr = htole32(physaddr); 1469 desc->wme = htole16( 1470 RT2560_AIFSN(2) | 1471 RT2560_LOGCWMIN(3) | 1472 RT2560_LOGCWMAX(8)); 1473 1474 /* setup PLCP fields */ 1475 desc->plcp_signal = rt2560_plcp_signal(rate); 1476 desc->plcp_service = 4; 1477 1478 len += IEEE80211_CRC_LEN; 1479 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) { 1480 desc->flags |= htole32(RT2560_TX_OFDM); 1481 1482 plcp_length = len & 0xfff; 1483 desc->plcp_length_hi = plcp_length >> 6; 1484 desc->plcp_length_lo = plcp_length & 0x3f; 1485 } else { 1486 plcp_length = (16 * len + rate - 1) / rate; 1487 if (rate == 22) { 1488 remainder = (16 * len) % 22; 1489 if (remainder != 0 && remainder < 7) 1490 desc->plcp_service |= RT2560_PLCP_LENGEXT; 1491 } 1492 desc->plcp_length_hi = plcp_length >> 8; 1493 desc->plcp_length_lo = plcp_length & 0xff; 1494 1495 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1496 desc->plcp_signal |= 0x08; 1497 } 1498 1499 if (!encrypt) 1500 desc->flags |= htole32(RT2560_TX_VALID); 1501 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY) 1502 : htole32(RT2560_TX_BUSY); 1503} 1504 1505static int 1506rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0, 1507 struct ieee80211_node *ni) 1508{ 1509 struct ieee80211vap *vap = ni->ni_vap; 1510 struct ieee80211com *ic = ni->ni_ic; 1511 struct ifnet *ifp = sc->sc_ifp; 1512 struct rt2560_tx_desc *desc; 1513 struct rt2560_tx_data *data; 1514 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1515 int nsegs, rate, error; 1516 1517 desc = &sc->bcnq.desc[sc->bcnq.cur]; 1518 data = &sc->bcnq.data[sc->bcnq.cur]; 1519 1520 /* XXX maybe a separate beacon rate? */ 1521 rate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].mgmtrate; 1522 1523 error = bus_dmamap_load_mbuf_sg(sc->bcnq.data_dmat, data->map, m0, 1524 segs, &nsegs, BUS_DMA_NOWAIT); 1525 if (error != 0) { 1526 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1527 error); 1528 m_freem(m0); 1529 return error; 1530 } 1531 1532 if (bpf_peers_present(ifp->if_bpf)) { 1533 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1534 1535 tap->wt_flags = 0; 1536 tap->wt_rate = rate; 1537 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1538 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1539 tap->wt_antenna = sc->tx_ant; 1540 1541 bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0); 1542 } 1543 1544 data->m = m0; 1545 data->ni = ni; 1546 1547 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF | 1548 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, segs->ds_addr); 1549 1550 DPRINTFN(sc, 10, "sending beacon frame len=%u idx=%u rate=%u\n", 1551 m0->m_pkthdr.len, sc->bcnq.cur, rate); 1552 1553 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1554 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map, 1555 BUS_DMASYNC_PREWRITE); 1556 1557 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT; 1558 1559 return 0; 1560} 1561 1562static int 1563rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0, 1564 struct ieee80211_node *ni) 1565{ 1566 struct ieee80211vap *vap = ni->ni_vap; 1567 struct ieee80211com *ic = ni->ni_ic; 1568 struct ifnet *ifp = sc->sc_ifp; 1569 struct rt2560_tx_desc *desc; 1570 struct rt2560_tx_data *data; 1571 struct ieee80211_frame *wh; 1572 struct ieee80211_key *k; 1573 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1574 uint16_t dur; 1575 uint32_t flags = 0; 1576 int nsegs, rate, error; 1577 1578 desc = &sc->prioq.desc[sc->prioq.cur]; 1579 data = &sc->prioq.data[sc->prioq.cur]; 1580 1581 rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate; 1582 1583 wh = mtod(m0, struct ieee80211_frame *); 1584 1585 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1586 k = ieee80211_crypto_encap(ni, m0); 1587 if (k == NULL) { 1588 m_freem(m0); 1589 return ENOBUFS; 1590 } 1591 } 1592 1593 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0, 1594 segs, &nsegs, 0); 1595 if (error != 0) { 1596 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1597 error); 1598 m_freem(m0); 1599 return error; 1600 } 1601 1602 if (bpf_peers_present(ifp->if_bpf)) { 1603 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1604 1605 tap->wt_flags = 0; 1606 tap->wt_rate = rate; 1607 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1608 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1609 tap->wt_antenna = sc->tx_ant; 1610 1611 bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0); 1612 } 1613 1614 data->m = m0; 1615 data->ni = ni; 1616 /* management frames are not taken into account for amrr */ 1617 data->rix = IEEE80211_FIXED_RATE_NONE; 1618 1619 wh = mtod(m0, struct ieee80211_frame *); 1620 1621 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1622 flags |= RT2560_TX_ACK; 1623 1624 dur = ieee80211_ack_duration(ic->ic_rt, 1625 rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1626 *(uint16_t *)wh->i_dur = htole16(dur); 1627 1628 /* tell hardware to add timestamp for probe responses */ 1629 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 1630 IEEE80211_FC0_TYPE_MGT && 1631 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 1632 IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1633 flags |= RT2560_TX_TIMESTAMP; 1634 } 1635 1636 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0, 1637 segs->ds_addr); 1638 1639 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1640 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1641 BUS_DMASYNC_PREWRITE); 1642 1643 DPRINTFN(sc, 10, "sending mgt frame len=%u idx=%u rate=%u\n", 1644 m0->m_pkthdr.len, sc->prioq.cur, rate); 1645 1646 /* kick prio */ 1647 sc->prioq.queued++; 1648 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT; 1649 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO); 1650 1651 return 0; 1652} 1653 1654static int 1655rt2560_sendprot(struct rt2560_softc *sc, 1656 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 1657{ 1658 struct ieee80211com *ic = ni->ni_ic; 1659 const struct ieee80211_frame *wh; 1660 struct rt2560_tx_desc *desc; 1661 struct rt2560_tx_data *data; 1662 struct mbuf *mprot; 1663 int protrate, ackrate, pktlen, flags, isshort, error; 1664 uint16_t dur; 1665 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1666 int nsegs; 1667 1668 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 1669 ("protection %d", prot)); 1670 1671 wh = mtod(m, const struct ieee80211_frame *); 1672 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 1673 1674 protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 1675 ackrate = ieee80211_ack_rate(ic->ic_rt, rate); 1676 1677 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 1678 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort) 1679 + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1680 flags = RT2560_TX_MORE_FRAG; 1681 if (prot == IEEE80211_PROT_RTSCTS) { 1682 /* NB: CTS is the same size as an ACK */ 1683 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1684 flags |= RT2560_TX_ACK; 1685 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 1686 } else { 1687 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 1688 } 1689 if (mprot == NULL) { 1690 /* XXX stat + msg */ 1691 return ENOBUFS; 1692 } 1693 1694 desc = &sc->txq.desc[sc->txq.cur_encrypt]; 1695 data = &sc->txq.data[sc->txq.cur_encrypt]; 1696 1697 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, 1698 mprot, segs, &nsegs, 0); 1699 if (error != 0) { 1700 device_printf(sc->sc_dev, 1701 "could not map mbuf (error %d)\n", error); 1702 m_freem(mprot); 1703 return error; 1704 } 1705 1706 data->m = mprot; 1707 data->ni = ieee80211_ref_node(ni); 1708 /* ctl frames are not taken into account for amrr */ 1709 data->rix = IEEE80211_FIXED_RATE_NONE; 1710 1711 rt2560_setup_tx_desc(sc, desc, flags, mprot->m_pkthdr.len, protrate, 1, 1712 segs->ds_addr); 1713 1714 bus_dmamap_sync(sc->txq.data_dmat, data->map, 1715 BUS_DMASYNC_PREWRITE); 1716 1717 sc->txq.queued++; 1718 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT; 1719 1720 return 0; 1721} 1722 1723static int 1724rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0, 1725 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params) 1726{ 1727 struct ifnet *ifp = sc->sc_ifp; 1728 struct ieee80211com *ic = ifp->if_l2com; 1729 struct rt2560_tx_desc *desc; 1730 struct rt2560_tx_data *data; 1731 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1732 uint32_t flags; 1733 int nsegs, rate, error; 1734 1735 desc = &sc->prioq.desc[sc->prioq.cur]; 1736 data = &sc->prioq.data[sc->prioq.cur]; 1737 1738 rate = params->ibp_rate0 & IEEE80211_RATE_VAL; 1739 /* XXX validate */ 1740 if (rate == 0) { 1741 /* XXX fall back to mcast/mgmt rate? */ 1742 m_freem(m0); 1743 return EINVAL; 1744 } 1745 1746 flags = 0; 1747 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1748 flags |= RT2560_TX_ACK; 1749 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 1750 error = rt2560_sendprot(sc, m0, ni, 1751 params->ibp_flags & IEEE80211_BPF_RTS ? 1752 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 1753 rate); 1754 if (error) { 1755 m_freem(m0); 1756 return error; 1757 } 1758 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS; 1759 } 1760 1761 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0, 1762 segs, &nsegs, 0); 1763 if (error != 0) { 1764 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1765 error); 1766 m_freem(m0); 1767 return error; 1768 } 1769 1770 if (bpf_peers_present(ifp->if_bpf)) { 1771 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1772 1773 tap->wt_flags = 0; 1774 tap->wt_rate = rate; 1775 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 1776 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 1777 tap->wt_antenna = sc->tx_ant; 1778 1779 bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0); 1780 } 1781 1782 data->m = m0; 1783 data->ni = ni; 1784 1785 /* XXX need to setup descriptor ourself */ 1786 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, 1787 rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0, 1788 segs->ds_addr); 1789 1790 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1791 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1792 BUS_DMASYNC_PREWRITE); 1793 1794 DPRINTFN(sc, 10, "sending raw frame len=%u idx=%u rate=%u\n", 1795 m0->m_pkthdr.len, sc->prioq.cur, rate); 1796 1797 /* kick prio */ 1798 sc->prioq.queued++; 1799 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT; 1800 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO); 1801 1802 return 0; 1803} 1804 1805static int 1806rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0, 1807 struct ieee80211_node *ni) 1808{ 1809 struct ieee80211vap *vap = ni->ni_vap; 1810 struct ieee80211com *ic = ni->ni_ic; 1811 struct ifnet *ifp = sc->sc_ifp; 1812 struct rt2560_tx_desc *desc; 1813 struct rt2560_tx_data *data; 1814 struct ieee80211_frame *wh; 1815 const struct ieee80211_txparam *tp; 1816 struct ieee80211_key *k; 1817 struct mbuf *mnew; 1818 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1819 uint16_t dur; 1820 uint32_t flags; 1821 int nsegs, rate, error; 1822 1823 wh = mtod(m0, struct ieee80211_frame *); 1824 1825 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 1826 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1827 rate = tp->mcastrate; 1828 } else if (m0->m_flags & M_EAPOL) { 1829 rate = tp->mgmtrate; 1830 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) { 1831 rate = tp->ucastrate; 1832 } else { 1833 (void) ieee80211_amrr_choose(ni, &RT2560_NODE(ni)->amrr); 1834 rate = ni->ni_txrate; 1835 } 1836 1837 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1838 k = ieee80211_crypto_encap(ni, m0); 1839 if (k == NULL) { 1840 m_freem(m0); 1841 return ENOBUFS; 1842 } 1843 1844 /* packet header may have moved, reset our local pointer */ 1845 wh = mtod(m0, struct ieee80211_frame *); 1846 } 1847 1848 flags = 0; 1849 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1850 int prot = IEEE80211_PROT_NONE; 1851 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 1852 prot = IEEE80211_PROT_RTSCTS; 1853 else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1854 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 1855 prot = ic->ic_protmode; 1856 if (prot != IEEE80211_PROT_NONE) { 1857 error = rt2560_sendprot(sc, m0, ni, prot, rate); 1858 if (error) { 1859 m_freem(m0); 1860 return error; 1861 } 1862 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS; 1863 } 1864 } 1865 1866 data = &sc->txq.data[sc->txq.cur_encrypt]; 1867 desc = &sc->txq.desc[sc->txq.cur_encrypt]; 1868 1869 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, m0, 1870 segs, &nsegs, 0); 1871 if (error != 0 && error != EFBIG) { 1872 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1873 error); 1874 m_freem(m0); 1875 return error; 1876 } 1877 if (error != 0) { 1878 mnew = m_defrag(m0, M_DONTWAIT); 1879 if (mnew == NULL) { 1880 device_printf(sc->sc_dev, 1881 "could not defragment mbuf\n"); 1882 m_freem(m0); 1883 return ENOBUFS; 1884 } 1885 m0 = mnew; 1886 1887 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, 1888 m0, segs, &nsegs, 0); 1889 if (error != 0) { 1890 device_printf(sc->sc_dev, 1891 "could not map mbuf (error %d)\n", error); 1892 m_freem(m0); 1893 return error; 1894 } 1895 1896 /* packet header may have moved, reset our local pointer */ 1897 wh = mtod(m0, struct ieee80211_frame *); 1898 } 1899 1900 if (bpf_peers_present(ifp->if_bpf)) { 1901 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1902 1903 tap->wt_flags = 0; 1904 tap->wt_rate = rate; 1905 tap->wt_antenna = sc->tx_ant; 1906 1907 bpf_mtap2(ifp->if_bpf, tap, sc->sc_txtap_len, m0); 1908 } 1909 1910 data->m = m0; 1911 data->ni = ni; 1912 1913 /* remember link conditions for rate adaptation algorithm */ 1914 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) { 1915 data->rix = ni->ni_txrate; 1916 /* XXX probably need last rssi value and not avg */ 1917 data->rssi = ic->ic_node_getrssi(ni); 1918 } else 1919 data->rix = IEEE80211_FIXED_RATE_NONE; 1920 1921 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1922 flags |= RT2560_TX_ACK; 1923 1924 dur = ieee80211_ack_duration(ic->ic_rt, 1925 rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1926 *(uint16_t *)wh->i_dur = htole16(dur); 1927 } 1928 1929 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1, 1930 segs->ds_addr); 1931 1932 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1933 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 1934 BUS_DMASYNC_PREWRITE); 1935 1936 DPRINTFN(sc, 10, "sending data frame len=%u idx=%u rate=%u\n", 1937 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate); 1938 1939 /* kick encrypt */ 1940 sc->txq.queued++; 1941 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT; 1942 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT); 1943 1944 return 0; 1945} 1946 1947static void 1948rt2560_start_locked(struct ifnet *ifp) 1949{ 1950 struct rt2560_softc *sc = ifp->if_softc; 1951 struct mbuf *m; 1952 struct ieee80211_node *ni; 1953 1954 RAL_LOCK_ASSERT(sc); 1955 1956 for (;;) { 1957 IFQ_DRV_DEQUEUE(&ifp->if_snd, m); 1958 if (m == NULL) 1959 break; 1960 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) { 1961 IFQ_DRV_PREPEND(&ifp->if_snd, m); 1962 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 1963 sc->sc_flags |= RT2560_F_DATA_OACTIVE; 1964 break; 1965 } 1966 1967 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1968 m = ieee80211_encap(ni, m); 1969 if (m == NULL) { 1970 ieee80211_free_node(ni); 1971 ifp->if_oerrors++; 1972 continue; 1973 } 1974 1975 if (rt2560_tx_data(sc, m, ni) != 0) { 1976 ieee80211_free_node(ni); 1977 ifp->if_oerrors++; 1978 break; 1979 } 1980 1981 sc->sc_tx_timer = 5; 1982 } 1983} 1984 1985static void 1986rt2560_start(struct ifnet *ifp) 1987{ 1988 struct rt2560_softc *sc = ifp->if_softc; 1989 1990 RAL_LOCK(sc); 1991 rt2560_start_locked(ifp); 1992 RAL_UNLOCK(sc); 1993} 1994 1995static void 1996rt2560_watchdog(void *arg) 1997{ 1998 struct rt2560_softc *sc = arg; 1999 struct ifnet *ifp = sc->sc_ifp; 2000 2001 RAL_LOCK_ASSERT(sc); 2002 2003 KASSERT(ifp->if_drv_flags & IFF_DRV_RUNNING, ("not running")); 2004 2005 if (sc->sc_invalid) /* card ejected */ 2006 return; 2007 2008 rt2560_encryption_intr(sc); 2009 rt2560_tx_intr(sc); 2010 2011 if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) { 2012 if_printf(ifp, "device timeout\n"); 2013 rt2560_init_locked(sc); 2014 ifp->if_oerrors++; 2015 /* NB: callout is reset in rt2560_init() */ 2016 return; 2017 } 2018 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc); 2019} 2020 2021static int 2022rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data) 2023{ 2024 struct rt2560_softc *sc = ifp->if_softc; 2025 struct ieee80211com *ic = ifp->if_l2com; 2026 struct ifreq *ifr = (struct ifreq *) data; 2027 int error = 0, startall = 0; 2028 2029 switch (cmd) { 2030 case SIOCSIFFLAGS: 2031 RAL_LOCK(sc); 2032 if (ifp->if_flags & IFF_UP) { 2033 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) { 2034 rt2560_init_locked(sc); 2035 startall = 1; 2036 } else 2037 rt2560_update_promisc(ifp); 2038 } else { 2039 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2040 rt2560_stop_locked(sc); 2041 } 2042 RAL_UNLOCK(sc); 2043 if (startall) 2044 ieee80211_start_all(ic); 2045 break; 2046 case SIOCGIFMEDIA: 2047 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 2048 break; 2049 case SIOCGIFADDR: 2050 error = ether_ioctl(ifp, cmd, data); 2051 break; 2052 default: 2053 error = EINVAL; 2054 break; 2055 } 2056 return error; 2057} 2058 2059static void 2060rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val) 2061{ 2062 uint32_t tmp; 2063 int ntries; 2064 2065 for (ntries = 0; ntries < 100; ntries++) { 2066 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY)) 2067 break; 2068 DELAY(1); 2069 } 2070 if (ntries == 100) { 2071 device_printf(sc->sc_dev, "could not write to BBP\n"); 2072 return; 2073 } 2074 2075 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val; 2076 RAL_WRITE(sc, RT2560_BBPCSR, tmp); 2077 2078 DPRINTFN(sc, 15, "BBP R%u <- 0x%02x\n", reg, val); 2079} 2080 2081static uint8_t 2082rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg) 2083{ 2084 uint32_t val; 2085 int ntries; 2086 2087 for (ntries = 0; ntries < 100; ntries++) { 2088 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY)) 2089 break; 2090 DELAY(1); 2091 } 2092 if (ntries == 100) { 2093 device_printf(sc->sc_dev, "could not read from BBP\n"); 2094 return 0; 2095 } 2096 2097 val = RT2560_BBP_BUSY | reg << 8; 2098 RAL_WRITE(sc, RT2560_BBPCSR, val); 2099 2100 for (ntries = 0; ntries < 100; ntries++) { 2101 val = RAL_READ(sc, RT2560_BBPCSR); 2102 if (!(val & RT2560_BBP_BUSY)) 2103 return val & 0xff; 2104 DELAY(1); 2105 } 2106 2107 device_printf(sc->sc_dev, "could not read from BBP\n"); 2108 return 0; 2109} 2110 2111static void 2112rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val) 2113{ 2114 uint32_t tmp; 2115 int ntries; 2116 2117 for (ntries = 0; ntries < 100; ntries++) { 2118 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY)) 2119 break; 2120 DELAY(1); 2121 } 2122 if (ntries == 100) { 2123 device_printf(sc->sc_dev, "could not write to RF\n"); 2124 return; 2125 } 2126 2127 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 | 2128 (reg & 0x3); 2129 RAL_WRITE(sc, RT2560_RFCSR, tmp); 2130 2131 /* remember last written value in sc */ 2132 sc->rf_regs[reg] = val; 2133 2134 DPRINTFN(sc, 15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff); 2135} 2136 2137static void 2138rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c) 2139{ 2140 struct ifnet *ifp = sc->sc_ifp; 2141 struct ieee80211com *ic = ifp->if_l2com; 2142 uint8_t power, tmp; 2143 u_int i, chan; 2144 2145 chan = ieee80211_chan2ieee(ic, c); 2146 KASSERT(chan != 0 && chan != IEEE80211_CHAN_ANY, ("chan 0x%x", chan)); 2147 2148 if (IEEE80211_IS_CHAN_2GHZ(c)) 2149 power = min(sc->txpow[chan - 1], 31); 2150 else 2151 power = 31; 2152 2153 /* adjust txpower using ifconfig settings */ 2154 power -= (100 - ic->ic_txpowlimit) / 8; 2155 2156 DPRINTFN(sc, 2, "setting channel to %u, txpower to %u\n", chan, power); 2157 2158 switch (sc->rf_rev) { 2159 case RT2560_RF_2522: 2160 rt2560_rf_write(sc, RAL_RF1, 0x00814); 2161 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]); 2162 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2163 break; 2164 2165 case RT2560_RF_2523: 2166 rt2560_rf_write(sc, RAL_RF1, 0x08804); 2167 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]); 2168 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044); 2169 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2170 break; 2171 2172 case RT2560_RF_2524: 2173 rt2560_rf_write(sc, RAL_RF1, 0x0c808); 2174 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]); 2175 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2176 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2177 break; 2178 2179 case RT2560_RF_2525: 2180 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2181 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]); 2182 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2183 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2184 2185 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2186 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]); 2187 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2188 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2189 break; 2190 2191 case RT2560_RF_2525E: 2192 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2193 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]); 2194 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2195 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282); 2196 break; 2197 2198 case RT2560_RF_2526: 2199 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]); 2200 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 2201 rt2560_rf_write(sc, RAL_RF1, 0x08804); 2202 2203 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]); 2204 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2205 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 2206 break; 2207 2208 /* dual-band RF */ 2209 case RT2560_RF_5222: 2210 for (i = 0; rt2560_rf5222[i].chan != chan; i++); 2211 2212 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1); 2213 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2); 2214 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2215 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4); 2216 break; 2217 default: 2218 printf("unknown ral rev=%d\n", sc->rf_rev); 2219 } 2220 2221 /* XXX */ 2222 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 2223 /* set Japan filter bit for channel 14 */ 2224 tmp = rt2560_bbp_read(sc, 70); 2225 2226 tmp &= ~RT2560_JAPAN_FILTER; 2227 if (chan == 14) 2228 tmp |= RT2560_JAPAN_FILTER; 2229 2230 rt2560_bbp_write(sc, 70, tmp); 2231 2232 /* clear CRC errors */ 2233 RAL_READ(sc, RT2560_CNT0); 2234 } 2235} 2236 2237static void 2238rt2560_set_channel(struct ieee80211com *ic) 2239{ 2240 struct ifnet *ifp = ic->ic_ifp; 2241 struct rt2560_softc *sc = ifp->if_softc; 2242 2243 RAL_LOCK(sc); 2244 rt2560_set_chan(sc, ic->ic_curchan); 2245 2246 sc->sc_txtap.wt_chan_freq = htole16(ic->ic_curchan->ic_freq); 2247 sc->sc_txtap.wt_chan_flags = htole16(ic->ic_curchan->ic_flags); 2248 sc->sc_rxtap.wr_chan_freq = htole16(ic->ic_curchan->ic_freq); 2249 sc->sc_rxtap.wr_chan_flags = htole16(ic->ic_curchan->ic_flags); 2250 RAL_UNLOCK(sc); 2251 2252} 2253 2254#if 0 2255/* 2256 * Disable RF auto-tuning. 2257 */ 2258static void 2259rt2560_disable_rf_tune(struct rt2560_softc *sc) 2260{ 2261 uint32_t tmp; 2262 2263 if (sc->rf_rev != RT2560_RF_2523) { 2264 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE; 2265 rt2560_rf_write(sc, RAL_RF1, tmp); 2266 } 2267 2268 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE; 2269 rt2560_rf_write(sc, RAL_RF3, tmp); 2270 2271 DPRINTFN(sc, 2, "%s", "disabling RF autotune\n"); 2272} 2273#endif 2274 2275/* 2276 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF 2277 * synchronization. 2278 */ 2279static void 2280rt2560_enable_tsf_sync(struct rt2560_softc *sc) 2281{ 2282 struct ifnet *ifp = sc->sc_ifp; 2283 struct ieee80211com *ic = ifp->if_l2com; 2284 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2285 uint16_t logcwmin, preload; 2286 uint32_t tmp; 2287 2288 /* first, disable TSF synchronization */ 2289 RAL_WRITE(sc, RT2560_CSR14, 0); 2290 2291 tmp = 16 * vap->iv_bss->ni_intval; 2292 RAL_WRITE(sc, RT2560_CSR12, tmp); 2293 2294 RAL_WRITE(sc, RT2560_CSR13, 0); 2295 2296 logcwmin = 5; 2297 preload = (vap->iv_opmode == IEEE80211_M_STA) ? 384 : 1024; 2298 tmp = logcwmin << 16 | preload; 2299 RAL_WRITE(sc, RT2560_BCNOCSR, tmp); 2300 2301 /* finally, enable TSF synchronization */ 2302 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN; 2303 if (ic->ic_opmode == IEEE80211_M_STA) 2304 tmp |= RT2560_ENABLE_TSF_SYNC(1); 2305 else 2306 tmp |= RT2560_ENABLE_TSF_SYNC(2) | 2307 RT2560_ENABLE_BEACON_GENERATOR; 2308 RAL_WRITE(sc, RT2560_CSR14, tmp); 2309 2310 DPRINTF(sc, "%s", "enabling TSF synchronization\n"); 2311} 2312 2313static void 2314rt2560_update_plcp(struct rt2560_softc *sc) 2315{ 2316 struct ifnet *ifp = sc->sc_ifp; 2317 struct ieee80211com *ic = ifp->if_l2com; 2318 2319 /* no short preamble for 1Mbps */ 2320 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400); 2321 2322 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) { 2323 /* values taken from the reference driver */ 2324 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401); 2325 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402); 2326 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403); 2327 } else { 2328 /* same values as above or'ed 0x8 */ 2329 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409); 2330 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a); 2331 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b); 2332 } 2333 2334 DPRINTF(sc, "updating PLCP for %s preamble\n", 2335 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"); 2336} 2337 2338/* 2339 * This function can be called by ieee80211_set_shortslottime(). Refer to 2340 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed. 2341 */ 2342static void 2343rt2560_update_slot(struct ifnet *ifp) 2344{ 2345 struct rt2560_softc *sc = ifp->if_softc; 2346 struct ieee80211com *ic = ifp->if_l2com; 2347 uint8_t slottime; 2348 uint16_t tx_sifs, tx_pifs, tx_difs, eifs; 2349 uint32_t tmp; 2350 2351#ifndef FORCE_SLOTTIME 2352 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 2353#else 2354 /* 2355 * Setting slot time according to "short slot time" capability 2356 * in beacon/probe_resp seems to cause problem to acknowledge 2357 * certain AP's data frames transimitted at CCK/DS rates: the 2358 * problematic AP keeps retransmitting data frames, probably 2359 * because MAC level acks are not received by hardware. 2360 * So we cheat a little bit here by claiming we are capable of 2361 * "short slot time" but setting hardware slot time to the normal 2362 * slot time. ral(4) does not seem to have trouble to receive 2363 * frames transmitted using short slot time even if hardware 2364 * slot time is set to normal slot time. If we didn't use this 2365 * trick, we would have to claim that short slot time is not 2366 * supported; this would give relative poor RX performance 2367 * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short 2368 * slot time. 2369 */ 2370 slottime = 20; 2371#endif 2372 2373 /* update the MAC slot boundaries */ 2374 tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND; 2375 tx_pifs = tx_sifs + slottime; 2376 tx_difs = tx_sifs + 2 * slottime; 2377 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60; 2378 2379 tmp = RAL_READ(sc, RT2560_CSR11); 2380 tmp = (tmp & ~0x1f00) | slottime << 8; 2381 RAL_WRITE(sc, RT2560_CSR11, tmp); 2382 2383 tmp = tx_pifs << 16 | tx_sifs; 2384 RAL_WRITE(sc, RT2560_CSR18, tmp); 2385 2386 tmp = eifs << 16 | tx_difs; 2387 RAL_WRITE(sc, RT2560_CSR19, tmp); 2388 2389 DPRINTF(sc, "setting slottime to %uus\n", slottime); 2390} 2391 2392static void 2393rt2560_set_basicrates(struct rt2560_softc *sc) 2394{ 2395 struct ifnet *ifp = sc->sc_ifp; 2396 struct ieee80211com *ic = ifp->if_l2com; 2397 2398 /* update basic rate set */ 2399 if (ic->ic_curmode == IEEE80211_MODE_11B) { 2400 /* 11b basic rates: 1, 2Mbps */ 2401 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3); 2402 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) { 2403 /* 11a basic rates: 6, 12, 24Mbps */ 2404 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150); 2405 } else { 2406 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */ 2407 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f); 2408 } 2409} 2410 2411static void 2412rt2560_update_led(struct rt2560_softc *sc, int led1, int led2) 2413{ 2414 uint32_t tmp; 2415 2416 /* set ON period to 70ms and OFF period to 30ms */ 2417 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30; 2418 RAL_WRITE(sc, RT2560_LEDCSR, tmp); 2419} 2420 2421static void 2422rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid) 2423{ 2424 uint32_t tmp; 2425 2426 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 2427 RAL_WRITE(sc, RT2560_CSR5, tmp); 2428 2429 tmp = bssid[4] | bssid[5] << 8; 2430 RAL_WRITE(sc, RT2560_CSR6, tmp); 2431 2432 DPRINTF(sc, "setting BSSID to %6D\n", bssid, ":"); 2433} 2434 2435static void 2436rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr) 2437{ 2438 uint32_t tmp; 2439 2440 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 2441 RAL_WRITE(sc, RT2560_CSR3, tmp); 2442 2443 tmp = addr[4] | addr[5] << 8; 2444 RAL_WRITE(sc, RT2560_CSR4, tmp); 2445 2446 DPRINTF(sc, "setting MAC address to %6D\n", addr, ":"); 2447} 2448 2449static void 2450rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr) 2451{ 2452 uint32_t tmp; 2453 2454 tmp = RAL_READ(sc, RT2560_CSR3); 2455 addr[0] = tmp & 0xff; 2456 addr[1] = (tmp >> 8) & 0xff; 2457 addr[2] = (tmp >> 16) & 0xff; 2458 addr[3] = (tmp >> 24); 2459 2460 tmp = RAL_READ(sc, RT2560_CSR4); 2461 addr[4] = tmp & 0xff; 2462 addr[5] = (tmp >> 8) & 0xff; 2463} 2464 2465static void 2466rt2560_update_promisc(struct ifnet *ifp) 2467{ 2468 struct rt2560_softc *sc = ifp->if_softc; 2469 uint32_t tmp; 2470 2471 tmp = RAL_READ(sc, RT2560_RXCSR0); 2472 2473 tmp &= ~RT2560_DROP_NOT_TO_ME; 2474 if (!(ifp->if_flags & IFF_PROMISC)) 2475 tmp |= RT2560_DROP_NOT_TO_ME; 2476 2477 RAL_WRITE(sc, RT2560_RXCSR0, tmp); 2478 2479 DPRINTF(sc, "%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 2480 "entering" : "leaving"); 2481} 2482 2483static const char * 2484rt2560_get_rf(int rev) 2485{ 2486 switch (rev) { 2487 case RT2560_RF_2522: return "RT2522"; 2488 case RT2560_RF_2523: return "RT2523"; 2489 case RT2560_RF_2524: return "RT2524"; 2490 case RT2560_RF_2525: return "RT2525"; 2491 case RT2560_RF_2525E: return "RT2525e"; 2492 case RT2560_RF_2526: return "RT2526"; 2493 case RT2560_RF_5222: return "RT5222"; 2494 default: return "unknown"; 2495 } 2496} 2497 2498static void 2499rt2560_read_config(struct rt2560_softc *sc) 2500{ 2501 uint16_t val; 2502 int i; 2503 2504 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0); 2505 sc->rf_rev = (val >> 11) & 0x7; 2506 sc->hw_radio = (val >> 10) & 0x1; 2507 sc->led_mode = (val >> 6) & 0x7; 2508 sc->rx_ant = (val >> 4) & 0x3; 2509 sc->tx_ant = (val >> 2) & 0x3; 2510 sc->nb_ant = val & 0x3; 2511 2512 /* read default values for BBP registers */ 2513 for (i = 0; i < 16; i++) { 2514 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i); 2515 if (val == 0 || val == 0xffff) 2516 continue; 2517 2518 sc->bbp_prom[i].reg = val >> 8; 2519 sc->bbp_prom[i].val = val & 0xff; 2520 } 2521 2522 /* read Tx power for all b/g channels */ 2523 for (i = 0; i < 14 / 2; i++) { 2524 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i); 2525 sc->txpow[i * 2] = val & 0xff; 2526 sc->txpow[i * 2 + 1] = val >> 8; 2527 } 2528 for (i = 0; i < 14; ++i) { 2529 if (sc->txpow[i] > 31) 2530 sc->txpow[i] = 24; 2531 } 2532 2533 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE); 2534 if ((val & 0xff) == 0xff) 2535 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR; 2536 else 2537 sc->rssi_corr = val & 0xff; 2538 DPRINTF(sc, "rssi correction %d, calibrate 0x%02x\n", 2539 sc->rssi_corr, val); 2540} 2541 2542 2543static void 2544rt2560_scan_start(struct ieee80211com *ic) 2545{ 2546 struct ifnet *ifp = ic->ic_ifp; 2547 struct rt2560_softc *sc = ifp->if_softc; 2548 2549 /* abort TSF synchronization */ 2550 RAL_WRITE(sc, RT2560_CSR14, 0); 2551 rt2560_set_bssid(sc, ifp->if_broadcastaddr); 2552} 2553 2554static void 2555rt2560_scan_end(struct ieee80211com *ic) 2556{ 2557 struct ifnet *ifp = ic->ic_ifp; 2558 struct rt2560_softc *sc = ifp->if_softc; 2559 struct ieee80211vap *vap = ic->ic_scan->ss_vap; 2560 2561 rt2560_enable_tsf_sync(sc); 2562 /* XXX keep local copy */ 2563 rt2560_set_bssid(sc, vap->iv_bss->ni_bssid); 2564} 2565 2566static int 2567rt2560_bbp_init(struct rt2560_softc *sc) 2568{ 2569#define N(a) (sizeof (a) / sizeof ((a)[0])) 2570 int i, ntries; 2571 2572 /* wait for BBP to be ready */ 2573 for (ntries = 0; ntries < 100; ntries++) { 2574 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0) 2575 break; 2576 DELAY(1); 2577 } 2578 if (ntries == 100) { 2579 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 2580 return EIO; 2581 } 2582 2583 /* initialize BBP registers to default values */ 2584 for (i = 0; i < N(rt2560_def_bbp); i++) { 2585 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg, 2586 rt2560_def_bbp[i].val); 2587 } 2588 2589 /* initialize BBP registers to values stored in EEPROM */ 2590 for (i = 0; i < 16; i++) { 2591 if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0) 2592 break; 2593 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 2594 } 2595 rt2560_bbp_write(sc, 17, 0x48); /* XXX restore bbp17 */ 2596 2597 return 0; 2598#undef N 2599} 2600 2601static void 2602rt2560_set_txantenna(struct rt2560_softc *sc, int antenna) 2603{ 2604 uint32_t tmp; 2605 uint8_t tx; 2606 2607 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK; 2608 if (antenna == 1) 2609 tx |= RT2560_BBP_ANTA; 2610 else if (antenna == 2) 2611 tx |= RT2560_BBP_ANTB; 2612 else 2613 tx |= RT2560_BBP_DIVERSITY; 2614 2615 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */ 2616 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 || 2617 sc->rf_rev == RT2560_RF_5222) 2618 tx |= RT2560_BBP_FLIPIQ; 2619 2620 rt2560_bbp_write(sc, RT2560_BBP_TX, tx); 2621 2622 /* update values for CCK and OFDM in BBPCSR1 */ 2623 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007; 2624 tmp |= (tx & 0x7) << 16 | (tx & 0x7); 2625 RAL_WRITE(sc, RT2560_BBPCSR1, tmp); 2626} 2627 2628static void 2629rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna) 2630{ 2631 uint8_t rx; 2632 2633 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK; 2634 if (antenna == 1) 2635 rx |= RT2560_BBP_ANTA; 2636 else if (antenna == 2) 2637 rx |= RT2560_BBP_ANTB; 2638 else 2639 rx |= RT2560_BBP_DIVERSITY; 2640 2641 /* need to force no I/Q flip for RF 2525e and 2526 */ 2642 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526) 2643 rx &= ~RT2560_BBP_FLIPIQ; 2644 2645 rt2560_bbp_write(sc, RT2560_BBP_RX, rx); 2646} 2647 2648static void 2649rt2560_init_locked(struct rt2560_softc *sc) 2650{ 2651#define N(a) (sizeof (a) / sizeof ((a)[0])) 2652 struct ifnet *ifp = sc->sc_ifp; 2653 struct ieee80211com *ic = ifp->if_l2com; 2654 uint32_t tmp; 2655 int i; 2656 2657 RAL_LOCK_ASSERT(sc); 2658 2659 rt2560_stop_locked(sc); 2660 2661 /* setup tx rings */ 2662 tmp = RT2560_PRIO_RING_COUNT << 24 | 2663 RT2560_ATIM_RING_COUNT << 16 | 2664 RT2560_TX_RING_COUNT << 8 | 2665 RT2560_TX_DESC_SIZE; 2666 2667 /* rings must be initialized in this exact order */ 2668 RAL_WRITE(sc, RT2560_TXCSR2, tmp); 2669 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr); 2670 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr); 2671 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr); 2672 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr); 2673 2674 /* setup rx ring */ 2675 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE; 2676 2677 RAL_WRITE(sc, RT2560_RXCSR1, tmp); 2678 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr); 2679 2680 /* initialize MAC registers to default values */ 2681 for (i = 0; i < N(rt2560_def_mac); i++) 2682 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val); 2683 2684 rt2560_set_macaddr(sc, IF_LLADDR(ifp)); 2685 2686 /* set basic rate set (will be updated later) */ 2687 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153); 2688 2689 rt2560_update_slot(ifp); 2690 rt2560_update_plcp(sc); 2691 rt2560_update_led(sc, 0, 0); 2692 2693 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC); 2694 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY); 2695 2696 if (rt2560_bbp_init(sc) != 0) { 2697 rt2560_stop(sc); 2698 RAL_UNLOCK(sc); 2699 return; 2700 } 2701 2702 rt2560_set_txantenna(sc, sc->tx_ant); 2703 rt2560_set_rxantenna(sc, sc->rx_ant); 2704 2705 /* set default BSS channel */ 2706 rt2560_set_chan(sc, ic->ic_curchan); 2707 2708 /* kick Rx */ 2709 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR; 2710 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2711 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR; 2712 if (ic->ic_opmode != IEEE80211_M_HOSTAP) 2713 tmp |= RT2560_DROP_TODS; 2714 if (!(ifp->if_flags & IFF_PROMISC)) 2715 tmp |= RT2560_DROP_NOT_TO_ME; 2716 } 2717 RAL_WRITE(sc, RT2560_RXCSR0, tmp); 2718 2719 /* clear old FCS and Rx FIFO errors */ 2720 RAL_READ(sc, RT2560_CNT0); 2721 RAL_READ(sc, RT2560_CNT4); 2722 2723 /* clear any pending interrupts */ 2724 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff); 2725 2726 /* enable interrupts */ 2727 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK); 2728 2729 ifp->if_drv_flags &= ~IFF_DRV_OACTIVE; 2730 ifp->if_drv_flags |= IFF_DRV_RUNNING; 2731 2732 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc); 2733#undef N 2734} 2735 2736static void 2737rt2560_init(void *priv) 2738{ 2739 struct rt2560_softc *sc = priv; 2740 struct ifnet *ifp = sc->sc_ifp; 2741 struct ieee80211com *ic = ifp->if_l2com; 2742 2743 RAL_LOCK(sc); 2744 rt2560_init_locked(sc); 2745 RAL_UNLOCK(sc); 2746 2747 if (ifp->if_drv_flags & IFF_DRV_RUNNING) 2748 ieee80211_start_all(ic); /* start all vap's */ 2749} 2750 2751static void 2752rt2560_stop_locked(struct rt2560_softc *sc) 2753{ 2754 struct ifnet *ifp = sc->sc_ifp; 2755 volatile int *flags = &sc->sc_flags; 2756 2757 RAL_LOCK_ASSERT(sc); 2758 2759 while (*flags & RT2560_F_INPUT_RUNNING) 2760 msleep(sc, &sc->sc_mtx, 0, "ralrunning", hz/10); 2761 2762 callout_stop(&sc->watchdog_ch); 2763 sc->sc_tx_timer = 0; 2764 2765 if (ifp->if_drv_flags & IFF_DRV_RUNNING) { 2766 ifp->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE); 2767 2768 /* abort Tx */ 2769 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX); 2770 2771 /* disable Rx */ 2772 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX); 2773 2774 /* reset ASIC (imply reset BBP) */ 2775 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC); 2776 RAL_WRITE(sc, RT2560_CSR1, 0); 2777 2778 /* disable interrupts */ 2779 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff); 2780 2781 /* reset Tx and Rx rings */ 2782 rt2560_reset_tx_ring(sc, &sc->txq); 2783 rt2560_reset_tx_ring(sc, &sc->atimq); 2784 rt2560_reset_tx_ring(sc, &sc->prioq); 2785 rt2560_reset_tx_ring(sc, &sc->bcnq); 2786 rt2560_reset_rx_ring(sc, &sc->rxq); 2787 } 2788 sc->sc_flags &= ~(RT2560_F_PRIO_OACTIVE | RT2560_F_DATA_OACTIVE); 2789} 2790 2791void 2792rt2560_stop(void *arg) 2793{ 2794 struct rt2560_softc *sc = arg; 2795 2796 RAL_LOCK(sc); 2797 rt2560_stop_locked(sc); 2798 RAL_UNLOCK(sc); 2799} 2800 2801static int 2802rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2803 const struct ieee80211_bpf_params *params) 2804{ 2805 struct ieee80211com *ic = ni->ni_ic; 2806 struct ifnet *ifp = ic->ic_ifp; 2807 struct rt2560_softc *sc = ifp->if_softc; 2808 2809 RAL_LOCK(sc); 2810 2811 /* prevent management frames from being sent if we're not ready */ 2812 if (!(ifp->if_drv_flags & IFF_DRV_RUNNING)) { 2813 RAL_UNLOCK(sc); 2814 m_freem(m); 2815 ieee80211_free_node(ni); 2816 return ENETDOWN; 2817 } 2818 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) { 2819 ifp->if_drv_flags |= IFF_DRV_OACTIVE; 2820 sc->sc_flags |= RT2560_F_PRIO_OACTIVE; 2821 RAL_UNLOCK(sc); 2822 m_freem(m); 2823 ieee80211_free_node(ni); 2824 return ENOBUFS; /* XXX */ 2825 } 2826 2827 ifp->if_opackets++; 2828 2829 if (params == NULL) { 2830 /* 2831 * Legacy path; interpret frame contents to decide 2832 * precisely how to send the frame. 2833 */ 2834 if (rt2560_tx_mgt(sc, m, ni) != 0) 2835 goto bad; 2836 } else { 2837 /* 2838 * Caller supplied explicit parameters to use in 2839 * sending the frame. 2840 */ 2841 if (rt2560_tx_raw(sc, m, ni, params)) 2842 goto bad; 2843 } 2844 sc->sc_tx_timer = 5; 2845 2846 RAL_UNLOCK(sc); 2847 2848 return 0; 2849bad: 2850 ifp->if_oerrors++; 2851 ieee80211_free_node(ni); 2852 RAL_UNLOCK(sc); 2853 return EIO; /* XXX */ 2854} 2855