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