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