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 __FBSDID("$FreeBSD$"); 21 22 /*- 23 * Ralink Technology RT2560 chipset driver 24 * http://www.ralinktech.com/ 25 */ 26 27 #include <sys/param.h> 28 #include <sys/sysctl.h> 29 #include <sys/sockio.h> 30 #include <sys/mbuf.h> 31 #include <sys/kernel.h> 32 #include <sys/socket.h> 33 #include <sys/systm.h> 34 #include <sys/malloc.h> 35 #include <sys/lock.h> 36 #include <sys/mutex.h> 37 #include <sys/module.h> 38 #include <sys/bus.h> 39 #include <sys/endian.h> 40 41 #include <machine/bus.h> 42 #include <machine/resource.h> 43 #include <sys/rman.h> 44 45 #include <net/bpf.h> 46 #include <net/if.h> 47 #include <net/if_var.h> 48 #include <net/if_arp.h> 49 #include <net/ethernet.h> 50 #include <net/if_dl.h> 51 #include <net/if_media.h> 52 #include <net/if_types.h> 53 54 #include <net80211/ieee80211_var.h> 55 #include <net80211/ieee80211_radiotap.h> 56 #include <net80211/ieee80211_regdomain.h> 57 #include <net80211/ieee80211_ratectl.h> 58 59 #include <netinet/in.h> 60 #include <netinet/in_systm.h> 61 #include <netinet/in_var.h> 62 #include <netinet/ip.h> 63 #include <netinet/if_ether.h> 64 65 #include <dev/ral/rt2560reg.h> 66 #include <dev/ral/rt2560var.h> 67 68 #define RT2560_RSSI(sc, rssi) \ 69 ((rssi) > (RT2560_NOISE_FLOOR + (sc)->rssi_corr) ? \ 70 ((rssi) - RT2560_NOISE_FLOOR - (sc)->rssi_corr) : 0) 71 72 #define RAL_DEBUG 73 #ifdef RAL_DEBUG 74 #define DPRINTF(sc, fmt, ...) do { \ 75 if (sc->sc_debug > 0) \ 76 printf(fmt, __VA_ARGS__); \ 77 } while (0) 78 #define DPRINTFN(sc, n, fmt, ...) do { \ 79 if (sc->sc_debug >= (n)) \ 80 printf(fmt, __VA_ARGS__); \ 81 } while (0) 82 #else 83 #define DPRINTF(sc, fmt, ...) 84 #define DPRINTFN(sc, n, fmt, ...) 85 #endif 86 87 static struct ieee80211vap *rt2560_vap_create(struct ieee80211com *, 88 const char [IFNAMSIZ], int, enum ieee80211_opmode, 89 int, const uint8_t [IEEE80211_ADDR_LEN], 90 const uint8_t [IEEE80211_ADDR_LEN]); 91 static void rt2560_vap_delete(struct ieee80211vap *); 92 static void rt2560_dma_map_addr(void *, bus_dma_segment_t *, int, 93 int); 94 static int rt2560_alloc_tx_ring(struct rt2560_softc *, 95 struct rt2560_tx_ring *, int); 96 static void rt2560_reset_tx_ring(struct rt2560_softc *, 97 struct rt2560_tx_ring *); 98 static void rt2560_free_tx_ring(struct rt2560_softc *, 99 struct rt2560_tx_ring *); 100 static int rt2560_alloc_rx_ring(struct rt2560_softc *, 101 struct rt2560_rx_ring *, int); 102 static void rt2560_reset_rx_ring(struct rt2560_softc *, 103 struct rt2560_rx_ring *); 104 static void rt2560_free_rx_ring(struct rt2560_softc *, 105 struct rt2560_rx_ring *); 106 static int rt2560_newstate(struct ieee80211vap *, 107 enum ieee80211_state, int); 108 static uint16_t rt2560_eeprom_read(struct rt2560_softc *, uint8_t); 109 static void rt2560_encryption_intr(struct rt2560_softc *); 110 static void rt2560_tx_intr(struct rt2560_softc *); 111 static void rt2560_prio_intr(struct rt2560_softc *); 112 static void rt2560_decryption_intr(struct rt2560_softc *); 113 static void rt2560_rx_intr(struct rt2560_softc *); 114 static void rt2560_beacon_update(struct ieee80211vap *, int item); 115 static void rt2560_beacon_expire(struct rt2560_softc *); 116 static void rt2560_wakeup_expire(struct rt2560_softc *); 117 static void rt2560_scan_start(struct ieee80211com *); 118 static void rt2560_scan_end(struct ieee80211com *); 119 static void rt2560_getradiocaps(struct ieee80211com *, int, int *, 120 struct ieee80211_channel[]); 121 static void rt2560_set_channel(struct ieee80211com *); 122 static void rt2560_setup_tx_desc(struct rt2560_softc *, 123 struct rt2560_tx_desc *, uint32_t, int, int, int, 124 bus_addr_t); 125 static int rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *, 126 struct ieee80211_node *); 127 static int rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *, 128 struct ieee80211_node *); 129 static int rt2560_tx_data(struct rt2560_softc *, struct mbuf *, 130 struct ieee80211_node *); 131 static int rt2560_transmit(struct ieee80211com *, struct mbuf *); 132 static void rt2560_start(struct rt2560_softc *); 133 static void rt2560_watchdog(void *); 134 static void rt2560_parent(struct ieee80211com *); 135 static void rt2560_bbp_write(struct rt2560_softc *, uint8_t, 136 uint8_t); 137 static uint8_t rt2560_bbp_read(struct rt2560_softc *, uint8_t); 138 static void rt2560_rf_write(struct rt2560_softc *, uint8_t, 139 uint32_t); 140 static void rt2560_set_chan(struct rt2560_softc *, 141 struct ieee80211_channel *); 142 #if 0 143 static void rt2560_disable_rf_tune(struct rt2560_softc *); 144 #endif 145 static void rt2560_enable_tsf_sync(struct rt2560_softc *); 146 static void rt2560_enable_tsf(struct rt2560_softc *); 147 static void rt2560_update_plcp(struct rt2560_softc *); 148 static void rt2560_update_slot(struct ieee80211com *); 149 static void rt2560_set_basicrates(struct rt2560_softc *, 150 const struct ieee80211_rateset *); 151 static void rt2560_update_led(struct rt2560_softc *, int, int); 152 static void rt2560_set_bssid(struct rt2560_softc *, const uint8_t *); 153 static void rt2560_set_macaddr(struct rt2560_softc *, 154 const uint8_t *); 155 static void rt2560_get_macaddr(struct rt2560_softc *, uint8_t *); 156 static void rt2560_update_promisc(struct ieee80211com *); 157 static const char *rt2560_get_rf(int); 158 static void rt2560_read_config(struct rt2560_softc *); 159 static int rt2560_bbp_init(struct rt2560_softc *); 160 static void rt2560_set_txantenna(struct rt2560_softc *, int); 161 static void rt2560_set_rxantenna(struct rt2560_softc *, int); 162 static void rt2560_init_locked(struct rt2560_softc *); 163 static void rt2560_init(void *); 164 static void rt2560_stop_locked(struct rt2560_softc *); 165 static int rt2560_raw_xmit(struct ieee80211_node *, struct mbuf *, 166 const struct ieee80211_bpf_params *); 167 168 static const struct { 169 uint32_t reg; 170 uint32_t val; 171 } rt2560_def_mac[] = { 172 RT2560_DEF_MAC 173 }; 174 175 static const struct { 176 uint8_t reg; 177 uint8_t val; 178 } rt2560_def_bbp[] = { 179 RT2560_DEF_BBP 180 }; 181 182 static const uint32_t rt2560_rf2522_r2[] = RT2560_RF2522_R2; 183 static const uint32_t rt2560_rf2523_r2[] = RT2560_RF2523_R2; 184 static const uint32_t rt2560_rf2524_r2[] = RT2560_RF2524_R2; 185 static const uint32_t rt2560_rf2525_r2[] = RT2560_RF2525_R2; 186 static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2; 187 static const uint32_t rt2560_rf2525e_r2[] = RT2560_RF2525E_R2; 188 static const uint32_t rt2560_rf2526_r2[] = RT2560_RF2526_R2; 189 static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2; 190 191 static const uint8_t rt2560_chan_5ghz[] = 192 { 36, 40, 44, 48, 52, 56, 60, 64, 193 100, 104, 108, 112, 116, 120, 124, 128, 132, 136, 140, 194 149, 153, 157, 161 }; 195 196 static const struct { 197 uint8_t chan; 198 uint32_t r1, r2, r4; 199 } rt2560_rf5222[] = { 200 RT2560_RF5222 201 }; 202 203 int 204 rt2560_attach(device_t dev, int id) 205 { 206 struct rt2560_softc *sc = device_get_softc(dev); 207 struct ieee80211com *ic = &sc->sc_ic; 208 int error; 209 210 sc->sc_dev = dev; 211 212 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), MTX_NETWORK_LOCK, 213 MTX_DEF | MTX_RECURSE); 214 215 callout_init_mtx(&sc->watchdog_ch, &sc->sc_mtx, 0); 216 mbufq_init(&sc->sc_snd, ifqmaxlen); 217 218 /* retrieve RT2560 rev. no */ 219 sc->asic_rev = RAL_READ(sc, RT2560_CSR0); 220 221 /* retrieve RF rev. no and various other things from EEPROM */ 222 rt2560_read_config(sc); 223 224 device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n", 225 sc->asic_rev, rt2560_get_rf(sc->rf_rev)); 226 227 /* 228 * Allocate Tx and Rx rings. 229 */ 230 error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT); 231 if (error != 0) { 232 device_printf(sc->sc_dev, "could not allocate Tx ring\n"); 233 goto fail1; 234 } 235 236 error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT); 237 if (error != 0) { 238 device_printf(sc->sc_dev, "could not allocate ATIM ring\n"); 239 goto fail2; 240 } 241 242 error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT); 243 if (error != 0) { 244 device_printf(sc->sc_dev, "could not allocate Prio ring\n"); 245 goto fail3; 246 } 247 248 error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT); 249 if (error != 0) { 250 device_printf(sc->sc_dev, "could not allocate Beacon ring\n"); 251 goto fail4; 252 } 253 254 error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT); 255 if (error != 0) { 256 device_printf(sc->sc_dev, "could not allocate Rx ring\n"); 257 goto fail5; 258 } 259 260 /* retrieve MAC address */ 261 rt2560_get_macaddr(sc, ic->ic_macaddr); 262 263 ic->ic_softc = sc; 264 ic->ic_name = device_get_nameunit(dev); 265 ic->ic_opmode = IEEE80211_M_STA; 266 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 267 268 /* set device capabilities */ 269 ic->ic_caps = 270 IEEE80211_C_STA /* station mode */ 271 | IEEE80211_C_IBSS /* ibss, nee adhoc, mode */ 272 | IEEE80211_C_HOSTAP /* hostap mode */ 273 | IEEE80211_C_MONITOR /* monitor mode */ 274 | IEEE80211_C_AHDEMO /* adhoc demo mode */ 275 | IEEE80211_C_WDS /* 4-address traffic works */ 276 | IEEE80211_C_MBSS /* mesh point link mode */ 277 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 278 | IEEE80211_C_SHSLOT /* short slot time supported */ 279 | IEEE80211_C_WPA /* capable of WPA1+WPA2 */ 280 | IEEE80211_C_BGSCAN /* capable of bg scanning */ 281 #ifdef notyet 282 | IEEE80211_C_TXFRAG /* handle tx frags */ 283 #endif 284 ; 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 epoch_tracker et; 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 descriptor 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 NET_EPOCH_ENTER(et); 1200 if (ni != NULL) { 1201 (void) ieee80211_input(ni, m, rssi, nf); 1202 ieee80211_free_node(ni); 1203 } else 1204 (void) ieee80211_input_all(ic, m, rssi, nf); 1205 NET_EPOCH_EXIT(et); 1206 RAL_LOCK(sc); 1207 sc->sc_flags &= ~RT2560_F_INPUT_RUNNING; 1208 skip: desc->flags = htole32(RT2560_RX_BUSY); 1209 1210 DPRINTFN(sc, 15, "decryption done idx=%u\n", sc->rxq.cur_decrypt); 1211 1212 sc->rxq.cur_decrypt = 1213 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT; 1214 } 1215 1216 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1217 BUS_DMASYNC_PREWRITE); 1218 } 1219 1220 /* 1221 * Some frames were received. Pass them to the hardware cipher engine before 1222 * sending them to the 802.11 layer. 1223 */ 1224 static void 1225 rt2560_rx_intr(struct rt2560_softc *sc) 1226 { 1227 struct rt2560_rx_desc *desc; 1228 struct rt2560_rx_data *data; 1229 1230 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1231 BUS_DMASYNC_POSTREAD); 1232 1233 for (;;) { 1234 desc = &sc->rxq.desc[sc->rxq.cur]; 1235 data = &sc->rxq.data[sc->rxq.cur]; 1236 1237 if ((le32toh(desc->flags) & RT2560_RX_BUSY) || 1238 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY)) 1239 break; 1240 1241 data->drop = 0; 1242 1243 if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) || 1244 (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) { 1245 /* 1246 * This should not happen since we did not request 1247 * to receive those frames when we filled RXCSR0. 1248 */ 1249 DPRINTFN(sc, 5, "PHY or CRC error flags 0x%08x\n", 1250 le32toh(desc->flags)); 1251 data->drop = 1; 1252 } 1253 1254 if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) { 1255 DPRINTFN(sc, 5, "%s\n", "bad length"); 1256 data->drop = 1; 1257 } 1258 1259 /* mark the frame for decryption */ 1260 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY); 1261 1262 DPRINTFN(sc, 15, "rx done idx=%u\n", sc->rxq.cur); 1263 1264 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT; 1265 } 1266 1267 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1268 BUS_DMASYNC_PREWRITE); 1269 1270 /* kick decrypt */ 1271 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT); 1272 } 1273 1274 static void 1275 rt2560_beacon_update(struct ieee80211vap *vap, int item) 1276 { 1277 struct ieee80211_beacon_offsets *bo = &vap->iv_bcn_off; 1278 1279 setbit(bo->bo_flags, item); 1280 } 1281 1282 /* 1283 * This function is called periodically in IBSS mode when a new beacon must be 1284 * sent out. 1285 */ 1286 static void 1287 rt2560_beacon_expire(struct rt2560_softc *sc) 1288 { 1289 struct ieee80211com *ic = &sc->sc_ic; 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, 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 = howmany(16 * len, 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 = ni->ni_txparms->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 struct rt2560_tx_desc *desc; 1596 struct rt2560_tx_data *data; 1597 struct mbuf *mprot; 1598 int protrate, flags, error; 1599 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1600 int nsegs; 1601 1602 mprot = ieee80211_alloc_prot(ni, m, rate, prot); 1603 if (mprot == NULL) { 1604 if_inc_counter(ni->ni_vap->iv_ifp, IFCOUNTER_OERRORS, 1); 1605 device_printf(sc->sc_dev, 1606 "could not allocate mbuf for protection mode %d\n", prot); 1607 return ENOBUFS; 1608 } 1609 1610 desc = &sc->txq.desc[sc->txq.cur_encrypt]; 1611 data = &sc->txq.data[sc->txq.cur_encrypt]; 1612 1613 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, 1614 mprot, segs, &nsegs, 0); 1615 if (error != 0) { 1616 device_printf(sc->sc_dev, 1617 "could not map mbuf (error %d)\n", error); 1618 m_freem(mprot); 1619 return error; 1620 } 1621 1622 data->m = mprot; 1623 data->ni = ieee80211_ref_node(ni); 1624 /* ctl frames are not taken into account for amrr */ 1625 data->rix = IEEE80211_FIXED_RATE_NONE; 1626 1627 protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 1628 flags = RT2560_TX_MORE_FRAG; 1629 if (prot == IEEE80211_PROT_RTSCTS) 1630 flags |= RT2560_TX_ACK; 1631 1632 rt2560_setup_tx_desc(sc, desc, flags, mprot->m_pkthdr.len, protrate, 1, 1633 segs->ds_addr); 1634 1635 bus_dmamap_sync(sc->txq.data_dmat, data->map, 1636 BUS_DMASYNC_PREWRITE); 1637 1638 sc->txq.queued++; 1639 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT; 1640 1641 return 0; 1642 } 1643 1644 static int 1645 rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0, 1646 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params) 1647 { 1648 struct ieee80211vap *vap = ni->ni_vap; 1649 struct ieee80211com *ic = ni->ni_ic; 1650 struct rt2560_tx_desc *desc; 1651 struct rt2560_tx_data *data; 1652 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1653 uint32_t flags; 1654 int nsegs, rate, error; 1655 1656 desc = &sc->prioq.desc[sc->prioq.cur]; 1657 data = &sc->prioq.data[sc->prioq.cur]; 1658 1659 rate = params->ibp_rate0; 1660 if (!ieee80211_isratevalid(ic->ic_rt, rate)) { 1661 /* XXX fall back to mcast/mgmt rate? */ 1662 m_freem(m0); 1663 return EINVAL; 1664 } 1665 1666 flags = 0; 1667 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1668 flags |= RT2560_TX_ACK; 1669 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 1670 error = rt2560_sendprot(sc, m0, ni, 1671 params->ibp_flags & IEEE80211_BPF_RTS ? 1672 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 1673 rate); 1674 if (error) { 1675 m_freem(m0); 1676 return error; 1677 } 1678 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS; 1679 } 1680 1681 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0, 1682 segs, &nsegs, 0); 1683 if (error != 0) { 1684 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1685 error); 1686 m_freem(m0); 1687 return error; 1688 } 1689 1690 if (ieee80211_radiotap_active_vap(vap)) { 1691 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1692 1693 tap->wt_flags = 0; 1694 tap->wt_rate = rate; 1695 tap->wt_antenna = sc->tx_ant; 1696 1697 ieee80211_radiotap_tx(ni->ni_vap, m0); 1698 } 1699 1700 data->m = m0; 1701 data->ni = ni; 1702 1703 /* XXX need to setup descriptor ourself */ 1704 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, 1705 rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0, 1706 segs->ds_addr); 1707 1708 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1709 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1710 BUS_DMASYNC_PREWRITE); 1711 1712 DPRINTFN(sc, 10, "sending raw frame len=%u idx=%u rate=%u\n", 1713 m0->m_pkthdr.len, sc->prioq.cur, rate); 1714 1715 /* kick prio */ 1716 sc->prioq.queued++; 1717 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT; 1718 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO); 1719 1720 return 0; 1721 } 1722 1723 static int 1724 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0, 1725 struct ieee80211_node *ni) 1726 { 1727 struct ieee80211vap *vap = ni->ni_vap; 1728 struct ieee80211com *ic = ni->ni_ic; 1729 struct rt2560_tx_desc *desc; 1730 struct rt2560_tx_data *data; 1731 struct ieee80211_frame *wh; 1732 const struct ieee80211_txparam *tp = ni->ni_txparms; 1733 struct ieee80211_key *k; 1734 struct mbuf *mnew; 1735 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1736 uint16_t dur; 1737 uint32_t flags; 1738 int nsegs, rate, error; 1739 1740 wh = mtod(m0, struct ieee80211_frame *); 1741 1742 if (m0->m_flags & M_EAPOL) { 1743 rate = tp->mgmtrate; 1744 } else if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1745 rate = tp->mcastrate; 1746 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) { 1747 rate = tp->ucastrate; 1748 } else { 1749 (void) ieee80211_ratectl_rate(ni, NULL, 0); 1750 rate = ni->ni_txrate; 1751 } 1752 1753 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1754 k = ieee80211_crypto_encap(ni, m0); 1755 if (k == NULL) { 1756 m_freem(m0); 1757 return ENOBUFS; 1758 } 1759 1760 /* packet header may have moved, reset our local pointer */ 1761 wh = mtod(m0, struct ieee80211_frame *); 1762 } 1763 1764 flags = 0; 1765 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1766 int prot = IEEE80211_PROT_NONE; 1767 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 1768 prot = IEEE80211_PROT_RTSCTS; 1769 else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1770 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 1771 prot = ic->ic_protmode; 1772 if (prot != IEEE80211_PROT_NONE) { 1773 error = rt2560_sendprot(sc, m0, ni, prot, rate); 1774 if (error) { 1775 m_freem(m0); 1776 return error; 1777 } 1778 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS; 1779 } 1780 } 1781 1782 data = &sc->txq.data[sc->txq.cur_encrypt]; 1783 desc = &sc->txq.desc[sc->txq.cur_encrypt]; 1784 1785 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, m0, 1786 segs, &nsegs, 0); 1787 if (error != 0 && error != EFBIG) { 1788 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1789 error); 1790 m_freem(m0); 1791 return error; 1792 } 1793 if (error != 0) { 1794 mnew = m_defrag(m0, M_NOWAIT); 1795 if (mnew == NULL) { 1796 device_printf(sc->sc_dev, 1797 "could not defragment mbuf\n"); 1798 m_freem(m0); 1799 return ENOBUFS; 1800 } 1801 m0 = mnew; 1802 1803 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, 1804 m0, segs, &nsegs, 0); 1805 if (error != 0) { 1806 device_printf(sc->sc_dev, 1807 "could not map mbuf (error %d)\n", error); 1808 m_freem(m0); 1809 return error; 1810 } 1811 1812 /* packet header may have moved, reset our local pointer */ 1813 wh = mtod(m0, struct ieee80211_frame *); 1814 } 1815 1816 if (ieee80211_radiotap_active_vap(vap)) { 1817 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1818 1819 tap->wt_flags = 0; 1820 tap->wt_rate = rate; 1821 tap->wt_antenna = sc->tx_ant; 1822 1823 ieee80211_radiotap_tx(vap, m0); 1824 } 1825 1826 data->m = m0; 1827 data->ni = ni; 1828 1829 /* remember link conditions for rate adaptation algorithm */ 1830 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) { 1831 data->rix = ni->ni_txrate; 1832 /* XXX probably need last rssi value and not avg */ 1833 data->rssi = ic->ic_node_getrssi(ni); 1834 } else 1835 data->rix = IEEE80211_FIXED_RATE_NONE; 1836 1837 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1838 flags |= RT2560_TX_ACK; 1839 1840 dur = ieee80211_ack_duration(ic->ic_rt, 1841 rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1842 *(uint16_t *)wh->i_dur = htole16(dur); 1843 } 1844 1845 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1, 1846 segs->ds_addr); 1847 1848 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1849 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 1850 BUS_DMASYNC_PREWRITE); 1851 1852 DPRINTFN(sc, 10, "sending data frame len=%u idx=%u rate=%u\n", 1853 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate); 1854 1855 /* kick encrypt */ 1856 sc->txq.queued++; 1857 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT; 1858 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT); 1859 1860 return 0; 1861 } 1862 1863 static int 1864 rt2560_transmit(struct ieee80211com *ic, struct mbuf *m) 1865 { 1866 struct rt2560_softc *sc = ic->ic_softc; 1867 int error; 1868 1869 RAL_LOCK(sc); 1870 if ((sc->sc_flags & RT2560_F_RUNNING) == 0) { 1871 RAL_UNLOCK(sc); 1872 return (ENXIO); 1873 } 1874 error = mbufq_enqueue(&sc->sc_snd, m); 1875 if (error) { 1876 RAL_UNLOCK(sc); 1877 return (error); 1878 } 1879 rt2560_start(sc); 1880 RAL_UNLOCK(sc); 1881 1882 return (0); 1883 } 1884 1885 static void 1886 rt2560_start(struct rt2560_softc *sc) 1887 { 1888 struct ieee80211_node *ni; 1889 struct mbuf *m; 1890 1891 RAL_LOCK_ASSERT(sc); 1892 1893 while (sc->txq.queued < RT2560_TX_RING_COUNT - 1 && 1894 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 1895 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1896 if (rt2560_tx_data(sc, m, ni) != 0) { 1897 if_inc_counter(ni->ni_vap->iv_ifp, 1898 IFCOUNTER_OERRORS, 1); 1899 ieee80211_free_node(ni); 1900 break; 1901 } 1902 sc->sc_tx_timer = 5; 1903 } 1904 } 1905 1906 static void 1907 rt2560_watchdog(void *arg) 1908 { 1909 struct rt2560_softc *sc = arg; 1910 1911 RAL_LOCK_ASSERT(sc); 1912 1913 KASSERT(sc->sc_flags & RT2560_F_RUNNING, ("not running")); 1914 1915 if (sc->sc_invalid) /* card ejected */ 1916 return; 1917 1918 rt2560_encryption_intr(sc); 1919 rt2560_tx_intr(sc); 1920 1921 if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) { 1922 device_printf(sc->sc_dev, "device timeout\n"); 1923 rt2560_init_locked(sc); 1924 counter_u64_add(sc->sc_ic.ic_oerrors, 1); 1925 /* NB: callout is reset in rt2560_init() */ 1926 return; 1927 } 1928 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc); 1929 } 1930 1931 static void 1932 rt2560_parent(struct ieee80211com *ic) 1933 { 1934 struct rt2560_softc *sc = ic->ic_softc; 1935 int startall = 0; 1936 1937 RAL_LOCK(sc); 1938 if (ic->ic_nrunning > 0) { 1939 if ((sc->sc_flags & RT2560_F_RUNNING) == 0) { 1940 rt2560_init_locked(sc); 1941 startall = 1; 1942 } else 1943 rt2560_update_promisc(ic); 1944 } else if (sc->sc_flags & RT2560_F_RUNNING) 1945 rt2560_stop_locked(sc); 1946 RAL_UNLOCK(sc); 1947 if (startall) 1948 ieee80211_start_all(ic); 1949 } 1950 1951 static void 1952 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val) 1953 { 1954 uint32_t tmp; 1955 int ntries; 1956 1957 for (ntries = 0; ntries < 100; ntries++) { 1958 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY)) 1959 break; 1960 DELAY(1); 1961 } 1962 if (ntries == 100) { 1963 device_printf(sc->sc_dev, "could not write to BBP\n"); 1964 return; 1965 } 1966 1967 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val; 1968 RAL_WRITE(sc, RT2560_BBPCSR, tmp); 1969 1970 DPRINTFN(sc, 15, "BBP R%u <- 0x%02x\n", reg, val); 1971 } 1972 1973 static uint8_t 1974 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg) 1975 { 1976 uint32_t val; 1977 int ntries; 1978 1979 for (ntries = 0; ntries < 100; ntries++) { 1980 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY)) 1981 break; 1982 DELAY(1); 1983 } 1984 if (ntries == 100) { 1985 device_printf(sc->sc_dev, "could not read from BBP\n"); 1986 return 0; 1987 } 1988 1989 val = RT2560_BBP_BUSY | reg << 8; 1990 RAL_WRITE(sc, RT2560_BBPCSR, val); 1991 1992 for (ntries = 0; ntries < 100; ntries++) { 1993 val = RAL_READ(sc, RT2560_BBPCSR); 1994 if (!(val & RT2560_BBP_BUSY)) 1995 return val & 0xff; 1996 DELAY(1); 1997 } 1998 1999 device_printf(sc->sc_dev, "could not read from BBP\n"); 2000 return 0; 2001 } 2002 2003 static void 2004 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val) 2005 { 2006 uint32_t tmp; 2007 int ntries; 2008 2009 for (ntries = 0; ntries < 100; ntries++) { 2010 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY)) 2011 break; 2012 DELAY(1); 2013 } 2014 if (ntries == 100) { 2015 device_printf(sc->sc_dev, "could not write to RF\n"); 2016 return; 2017 } 2018 2019 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 | 2020 (reg & 0x3); 2021 RAL_WRITE(sc, RT2560_RFCSR, tmp); 2022 2023 /* remember last written value in sc */ 2024 sc->rf_regs[reg] = val; 2025 2026 DPRINTFN(sc, 15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff); 2027 } 2028 2029 static void 2030 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c) 2031 { 2032 struct ieee80211com *ic = &sc->sc_ic; 2033 uint8_t power, tmp; 2034 u_int i, chan; 2035 2036 chan = ieee80211_chan2ieee(ic, c); 2037 KASSERT(chan != 0 && chan != IEEE80211_CHAN_ANY, ("chan 0x%x", chan)); 2038 2039 if (IEEE80211_IS_CHAN_2GHZ(c)) 2040 power = min(sc->txpow[chan - 1], 31); 2041 else 2042 power = 31; 2043 2044 /* adjust txpower using ifconfig settings */ 2045 power -= (100 - ic->ic_txpowlimit) / 8; 2046 2047 DPRINTFN(sc, 2, "setting channel to %u, txpower to %u\n", chan, power); 2048 2049 switch (sc->rf_rev) { 2050 case RT2560_RF_2522: 2051 rt2560_rf_write(sc, RAL_RF1, 0x00814); 2052 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]); 2053 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2054 break; 2055 2056 case RT2560_RF_2523: 2057 rt2560_rf_write(sc, RAL_RF1, 0x08804); 2058 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]); 2059 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044); 2060 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2061 break; 2062 2063 case RT2560_RF_2524: 2064 rt2560_rf_write(sc, RAL_RF1, 0x0c808); 2065 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]); 2066 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2067 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2068 break; 2069 2070 case RT2560_RF_2525: 2071 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2072 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]); 2073 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2074 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2075 2076 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2077 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]); 2078 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2079 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2080 break; 2081 2082 case RT2560_RF_2525E: 2083 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2084 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]); 2085 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2086 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282); 2087 break; 2088 2089 case RT2560_RF_2526: 2090 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]); 2091 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 2092 rt2560_rf_write(sc, RAL_RF1, 0x08804); 2093 2094 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]); 2095 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2096 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 2097 break; 2098 2099 /* dual-band RF */ 2100 case RT2560_RF_5222: 2101 for (i = 0; rt2560_rf5222[i].chan != chan; i++); 2102 2103 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1); 2104 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2); 2105 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2106 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4); 2107 break; 2108 default: 2109 printf("unknown ral rev=%d\n", sc->rf_rev); 2110 } 2111 2112 /* XXX */ 2113 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 2114 /* set Japan filter bit for channel 14 */ 2115 tmp = rt2560_bbp_read(sc, 70); 2116 2117 tmp &= ~RT2560_JAPAN_FILTER; 2118 if (chan == 14) 2119 tmp |= RT2560_JAPAN_FILTER; 2120 2121 rt2560_bbp_write(sc, 70, tmp); 2122 2123 /* clear CRC errors */ 2124 RAL_READ(sc, RT2560_CNT0); 2125 } 2126 } 2127 2128 static void 2129 rt2560_getradiocaps(struct ieee80211com *ic, 2130 int maxchans, int *nchans, struct ieee80211_channel chans[]) 2131 { 2132 struct rt2560_softc *sc = ic->ic_softc; 2133 uint8_t bands[IEEE80211_MODE_BYTES]; 2134 2135 memset(bands, 0, sizeof(bands)); 2136 setbit(bands, IEEE80211_MODE_11B); 2137 setbit(bands, IEEE80211_MODE_11G); 2138 ieee80211_add_channels_default_2ghz(chans, maxchans, nchans, bands, 0); 2139 2140 if (sc->rf_rev == RT2560_RF_5222) { 2141 setbit(bands, IEEE80211_MODE_11A); 2142 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans, 2143 rt2560_chan_5ghz, nitems(rt2560_chan_5ghz), bands, 0); 2144 } 2145 } 2146 2147 static void 2148 rt2560_set_channel(struct ieee80211com *ic) 2149 { 2150 struct rt2560_softc *sc = ic->ic_softc; 2151 2152 RAL_LOCK(sc); 2153 rt2560_set_chan(sc, ic->ic_curchan); 2154 RAL_UNLOCK(sc); 2155 2156 } 2157 2158 #if 0 2159 /* 2160 * Disable RF auto-tuning. 2161 */ 2162 static void 2163 rt2560_disable_rf_tune(struct rt2560_softc *sc) 2164 { 2165 uint32_t tmp; 2166 2167 if (sc->rf_rev != RT2560_RF_2523) { 2168 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE; 2169 rt2560_rf_write(sc, RAL_RF1, tmp); 2170 } 2171 2172 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE; 2173 rt2560_rf_write(sc, RAL_RF3, tmp); 2174 2175 DPRINTFN(sc, 2, "%s", "disabling RF autotune\n"); 2176 } 2177 #endif 2178 2179 /* 2180 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF 2181 * synchronization. 2182 */ 2183 static void 2184 rt2560_enable_tsf_sync(struct rt2560_softc *sc) 2185 { 2186 struct ieee80211com *ic = &sc->sc_ic; 2187 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2188 uint16_t logcwmin, preload; 2189 uint32_t tmp; 2190 2191 /* first, disable TSF synchronization */ 2192 RAL_WRITE(sc, RT2560_CSR14, 0); 2193 2194 tmp = 16 * vap->iv_bss->ni_intval; 2195 RAL_WRITE(sc, RT2560_CSR12, tmp); 2196 2197 RAL_WRITE(sc, RT2560_CSR13, 0); 2198 2199 logcwmin = 5; 2200 preload = (vap->iv_opmode == IEEE80211_M_STA) ? 384 : 1024; 2201 tmp = logcwmin << 16 | preload; 2202 RAL_WRITE(sc, RT2560_BCNOCSR, tmp); 2203 2204 /* finally, enable TSF synchronization */ 2205 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN; 2206 if (ic->ic_opmode == IEEE80211_M_STA) 2207 tmp |= RT2560_ENABLE_TSF_SYNC(1); 2208 else 2209 tmp |= RT2560_ENABLE_TSF_SYNC(2) | 2210 RT2560_ENABLE_BEACON_GENERATOR; 2211 RAL_WRITE(sc, RT2560_CSR14, tmp); 2212 2213 DPRINTF(sc, "%s", "enabling TSF synchronization\n"); 2214 } 2215 2216 static void 2217 rt2560_enable_tsf(struct rt2560_softc *sc) 2218 { 2219 RAL_WRITE(sc, RT2560_CSR14, 0); 2220 RAL_WRITE(sc, RT2560_CSR14, 2221 RT2560_ENABLE_TSF_SYNC(2) | RT2560_ENABLE_TSF); 2222 } 2223 2224 static void 2225 rt2560_update_plcp(struct rt2560_softc *sc) 2226 { 2227 struct ieee80211com *ic = &sc->sc_ic; 2228 2229 /* no short preamble for 1Mbps */ 2230 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400); 2231 2232 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) { 2233 /* values taken from the reference driver */ 2234 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401); 2235 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402); 2236 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403); 2237 } else { 2238 /* same values as above or'ed 0x8 */ 2239 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409); 2240 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a); 2241 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b); 2242 } 2243 2244 DPRINTF(sc, "updating PLCP for %s preamble\n", 2245 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"); 2246 } 2247 2248 /* 2249 * This function can be called by ieee80211_set_shortslottime(). Refer to 2250 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed. 2251 */ 2252 static void 2253 rt2560_update_slot(struct ieee80211com *ic) 2254 { 2255 struct rt2560_softc *sc = ic->ic_softc; 2256 uint8_t slottime; 2257 uint16_t tx_sifs, tx_pifs, tx_difs, eifs; 2258 uint32_t tmp; 2259 2260 #ifndef FORCE_SLOTTIME 2261 slottime = IEEE80211_GET_SLOTTIME(ic); 2262 #else 2263 /* 2264 * Setting slot time according to "short slot time" capability 2265 * in beacon/probe_resp seems to cause problem to acknowledge 2266 * certain AP's data frames transimitted at CCK/DS rates: the 2267 * problematic AP keeps retransmitting data frames, probably 2268 * because MAC level acks are not received by hardware. 2269 * So we cheat a little bit here by claiming we are capable of 2270 * "short slot time" but setting hardware slot time to the normal 2271 * slot time. ral(4) does not seem to have trouble to receive 2272 * frames transmitted using short slot time even if hardware 2273 * slot time is set to normal slot time. If we didn't use this 2274 * trick, we would have to claim that short slot time is not 2275 * supported; this would give relative poor RX performance 2276 * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short 2277 * slot time. 2278 */ 2279 slottime = IEEE80211_DUR_SLOT; 2280 #endif 2281 2282 /* update the MAC slot boundaries */ 2283 tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND; 2284 tx_pifs = tx_sifs + slottime; 2285 tx_difs = IEEE80211_DUR_DIFS(tx_sifs, slottime); 2286 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60; 2287 2288 tmp = RAL_READ(sc, RT2560_CSR11); 2289 tmp = (tmp & ~0x1f00) | slottime << 8; 2290 RAL_WRITE(sc, RT2560_CSR11, tmp); 2291 2292 tmp = tx_pifs << 16 | tx_sifs; 2293 RAL_WRITE(sc, RT2560_CSR18, tmp); 2294 2295 tmp = eifs << 16 | tx_difs; 2296 RAL_WRITE(sc, RT2560_CSR19, tmp); 2297 2298 DPRINTF(sc, "setting slottime to %uus\n", slottime); 2299 } 2300 2301 static void 2302 rt2560_set_basicrates(struct rt2560_softc *sc, 2303 const struct ieee80211_rateset *rs) 2304 { 2305 struct ieee80211com *ic = &sc->sc_ic; 2306 uint32_t mask = 0; 2307 uint8_t rate; 2308 int i; 2309 2310 for (i = 0; i < rs->rs_nrates; i++) { 2311 rate = rs->rs_rates[i]; 2312 2313 if (!(rate & IEEE80211_RATE_BASIC)) 2314 continue; 2315 2316 mask |= 1 << ieee80211_legacy_rate_lookup(ic->ic_rt, 2317 IEEE80211_RV(rate)); 2318 } 2319 2320 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, mask); 2321 2322 DPRINTF(sc, "Setting basic rate mask to 0x%x\n", mask); 2323 } 2324 2325 static void 2326 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2) 2327 { 2328 uint32_t tmp; 2329 2330 /* set ON period to 70ms and OFF period to 30ms */ 2331 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30; 2332 RAL_WRITE(sc, RT2560_LEDCSR, tmp); 2333 } 2334 2335 static void 2336 rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid) 2337 { 2338 uint32_t tmp; 2339 2340 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 2341 RAL_WRITE(sc, RT2560_CSR5, tmp); 2342 2343 tmp = bssid[4] | bssid[5] << 8; 2344 RAL_WRITE(sc, RT2560_CSR6, tmp); 2345 2346 DPRINTF(sc, "setting BSSID to %6D\n", bssid, ":"); 2347 } 2348 2349 static void 2350 rt2560_set_macaddr(struct rt2560_softc *sc, const uint8_t *addr) 2351 { 2352 uint32_t tmp; 2353 2354 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 2355 RAL_WRITE(sc, RT2560_CSR3, tmp); 2356 2357 tmp = addr[4] | addr[5] << 8; 2358 RAL_WRITE(sc, RT2560_CSR4, tmp); 2359 2360 DPRINTF(sc, "setting MAC address to %6D\n", addr, ":"); 2361 } 2362 2363 static void 2364 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr) 2365 { 2366 uint32_t tmp; 2367 2368 tmp = RAL_READ(sc, RT2560_CSR3); 2369 addr[0] = tmp & 0xff; 2370 addr[1] = (tmp >> 8) & 0xff; 2371 addr[2] = (tmp >> 16) & 0xff; 2372 addr[3] = (tmp >> 24); 2373 2374 tmp = RAL_READ(sc, RT2560_CSR4); 2375 addr[4] = tmp & 0xff; 2376 addr[5] = (tmp >> 8) & 0xff; 2377 } 2378 2379 static void 2380 rt2560_update_promisc(struct ieee80211com *ic) 2381 { 2382 struct rt2560_softc *sc = ic->ic_softc; 2383 uint32_t tmp; 2384 2385 tmp = RAL_READ(sc, RT2560_RXCSR0); 2386 2387 tmp &= ~RT2560_DROP_NOT_TO_ME; 2388 if (ic->ic_promisc == 0) 2389 tmp |= RT2560_DROP_NOT_TO_ME; 2390 2391 RAL_WRITE(sc, RT2560_RXCSR0, tmp); 2392 2393 DPRINTF(sc, "%s promiscuous mode\n", 2394 (ic->ic_promisc > 0) ? "entering" : "leaving"); 2395 } 2396 2397 static const char * 2398 rt2560_get_rf(int rev) 2399 { 2400 switch (rev) { 2401 case RT2560_RF_2522: return "RT2522"; 2402 case RT2560_RF_2523: return "RT2523"; 2403 case RT2560_RF_2524: return "RT2524"; 2404 case RT2560_RF_2525: return "RT2525"; 2405 case RT2560_RF_2525E: return "RT2525e"; 2406 case RT2560_RF_2526: return "RT2526"; 2407 case RT2560_RF_5222: return "RT5222"; 2408 default: return "unknown"; 2409 } 2410 } 2411 2412 static void 2413 rt2560_read_config(struct rt2560_softc *sc) 2414 { 2415 uint16_t val; 2416 int i; 2417 2418 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0); 2419 sc->rf_rev = (val >> 11) & 0x7; 2420 sc->hw_radio = (val >> 10) & 0x1; 2421 sc->led_mode = (val >> 6) & 0x7; 2422 sc->rx_ant = (val >> 4) & 0x3; 2423 sc->tx_ant = (val >> 2) & 0x3; 2424 sc->nb_ant = val & 0x3; 2425 2426 /* read default values for BBP registers */ 2427 for (i = 0; i < 16; i++) { 2428 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i); 2429 if (val == 0 || val == 0xffff) 2430 continue; 2431 2432 sc->bbp_prom[i].reg = val >> 8; 2433 sc->bbp_prom[i].val = val & 0xff; 2434 } 2435 2436 /* read Tx power for all b/g channels */ 2437 for (i = 0; i < 14 / 2; i++) { 2438 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i); 2439 sc->txpow[i * 2] = val & 0xff; 2440 sc->txpow[i * 2 + 1] = val >> 8; 2441 } 2442 for (i = 0; i < 14; ++i) { 2443 if (sc->txpow[i] > 31) 2444 sc->txpow[i] = 24; 2445 } 2446 2447 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE); 2448 if ((val & 0xff) == 0xff) 2449 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR; 2450 else 2451 sc->rssi_corr = val & 0xff; 2452 DPRINTF(sc, "rssi correction %d, calibrate 0x%02x\n", 2453 sc->rssi_corr, val); 2454 } 2455 2456 static void 2457 rt2560_scan_start(struct ieee80211com *ic) 2458 { 2459 struct rt2560_softc *sc = ic->ic_softc; 2460 2461 /* abort TSF synchronization */ 2462 RAL_WRITE(sc, RT2560_CSR14, 0); 2463 rt2560_set_bssid(sc, ieee80211broadcastaddr); 2464 } 2465 2466 static void 2467 rt2560_scan_end(struct ieee80211com *ic) 2468 { 2469 struct rt2560_softc *sc = ic->ic_softc; 2470 struct ieee80211vap *vap = ic->ic_scan->ss_vap; 2471 2472 rt2560_enable_tsf_sync(sc); 2473 /* XXX keep local copy */ 2474 rt2560_set_bssid(sc, vap->iv_bss->ni_bssid); 2475 } 2476 2477 static int 2478 rt2560_bbp_init(struct rt2560_softc *sc) 2479 { 2480 int i, ntries; 2481 2482 /* wait for BBP to be ready */ 2483 for (ntries = 0; ntries < 100; ntries++) { 2484 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0) 2485 break; 2486 DELAY(1); 2487 } 2488 if (ntries == 100) { 2489 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 2490 return EIO; 2491 } 2492 2493 /* initialize BBP registers to default values */ 2494 for (i = 0; i < nitems(rt2560_def_bbp); i++) { 2495 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg, 2496 rt2560_def_bbp[i].val); 2497 } 2498 2499 /* initialize BBP registers to values stored in EEPROM */ 2500 for (i = 0; i < 16; i++) { 2501 if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0) 2502 break; 2503 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 2504 } 2505 rt2560_bbp_write(sc, 17, 0x48); /* XXX restore bbp17 */ 2506 2507 return 0; 2508 } 2509 2510 static void 2511 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna) 2512 { 2513 uint32_t tmp; 2514 uint8_t tx; 2515 2516 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK; 2517 if (antenna == 1) 2518 tx |= RT2560_BBP_ANTA; 2519 else if (antenna == 2) 2520 tx |= RT2560_BBP_ANTB; 2521 else 2522 tx |= RT2560_BBP_DIVERSITY; 2523 2524 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */ 2525 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 || 2526 sc->rf_rev == RT2560_RF_5222) 2527 tx |= RT2560_BBP_FLIPIQ; 2528 2529 rt2560_bbp_write(sc, RT2560_BBP_TX, tx); 2530 2531 /* update values for CCK and OFDM in BBPCSR1 */ 2532 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007; 2533 tmp |= (tx & 0x7) << 16 | (tx & 0x7); 2534 RAL_WRITE(sc, RT2560_BBPCSR1, tmp); 2535 } 2536 2537 static void 2538 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna) 2539 { 2540 uint8_t rx; 2541 2542 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK; 2543 if (antenna == 1) 2544 rx |= RT2560_BBP_ANTA; 2545 else if (antenna == 2) 2546 rx |= RT2560_BBP_ANTB; 2547 else 2548 rx |= RT2560_BBP_DIVERSITY; 2549 2550 /* need to force no I/Q flip for RF 2525e and 2526 */ 2551 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526) 2552 rx &= ~RT2560_BBP_FLIPIQ; 2553 2554 rt2560_bbp_write(sc, RT2560_BBP_RX, rx); 2555 } 2556 2557 static void 2558 rt2560_init_locked(struct rt2560_softc *sc) 2559 { 2560 struct ieee80211com *ic = &sc->sc_ic; 2561 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2562 uint32_t tmp; 2563 int i; 2564 2565 RAL_LOCK_ASSERT(sc); 2566 2567 rt2560_stop_locked(sc); 2568 2569 /* setup tx rings */ 2570 tmp = RT2560_PRIO_RING_COUNT << 24 | 2571 RT2560_ATIM_RING_COUNT << 16 | 2572 RT2560_TX_RING_COUNT << 8 | 2573 RT2560_TX_DESC_SIZE; 2574 2575 /* rings must be initialized in this exact order */ 2576 RAL_WRITE(sc, RT2560_TXCSR2, tmp); 2577 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr); 2578 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr); 2579 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr); 2580 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr); 2581 2582 /* setup rx ring */ 2583 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE; 2584 2585 RAL_WRITE(sc, RT2560_RXCSR1, tmp); 2586 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr); 2587 2588 /* initialize MAC registers to default values */ 2589 for (i = 0; i < nitems(rt2560_def_mac); i++) 2590 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val); 2591 2592 rt2560_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr); 2593 2594 /* set basic rate set (will be updated later) */ 2595 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153); 2596 2597 rt2560_update_slot(ic); 2598 rt2560_update_plcp(sc); 2599 rt2560_update_led(sc, 0, 0); 2600 2601 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC); 2602 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY); 2603 2604 if (rt2560_bbp_init(sc) != 0) { 2605 rt2560_stop_locked(sc); 2606 return; 2607 } 2608 2609 rt2560_set_txantenna(sc, sc->tx_ant); 2610 rt2560_set_rxantenna(sc, sc->rx_ant); 2611 2612 /* set default BSS channel */ 2613 rt2560_set_chan(sc, ic->ic_curchan); 2614 2615 /* kick Rx */ 2616 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR; 2617 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2618 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR; 2619 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 2620 ic->ic_opmode != IEEE80211_M_MBSS) 2621 tmp |= RT2560_DROP_TODS; 2622 if (ic->ic_promisc == 0) 2623 tmp |= RT2560_DROP_NOT_TO_ME; 2624 } 2625 RAL_WRITE(sc, RT2560_RXCSR0, tmp); 2626 2627 /* clear old FCS and Rx FIFO errors */ 2628 RAL_READ(sc, RT2560_CNT0); 2629 RAL_READ(sc, RT2560_CNT4); 2630 2631 /* clear any pending interrupts */ 2632 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff); 2633 2634 /* enable interrupts */ 2635 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK); 2636 2637 sc->sc_flags |= RT2560_F_RUNNING; 2638 2639 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc); 2640 } 2641 2642 static void 2643 rt2560_init(void *priv) 2644 { 2645 struct rt2560_softc *sc = priv; 2646 struct ieee80211com *ic = &sc->sc_ic; 2647 2648 RAL_LOCK(sc); 2649 rt2560_init_locked(sc); 2650 RAL_UNLOCK(sc); 2651 2652 if (sc->sc_flags & RT2560_F_RUNNING) 2653 ieee80211_start_all(ic); /* start all vap's */ 2654 } 2655 2656 static void 2657 rt2560_stop_locked(struct rt2560_softc *sc) 2658 { 2659 volatile int *flags = &sc->sc_flags; 2660 2661 RAL_LOCK_ASSERT(sc); 2662 2663 while (*flags & RT2560_F_INPUT_RUNNING) 2664 msleep(sc, &sc->sc_mtx, 0, "ralrunning", hz/10); 2665 2666 callout_stop(&sc->watchdog_ch); 2667 sc->sc_tx_timer = 0; 2668 2669 if (sc->sc_flags & RT2560_F_RUNNING) { 2670 sc->sc_flags &= ~RT2560_F_RUNNING; 2671 2672 /* abort Tx */ 2673 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX); 2674 2675 /* disable Rx */ 2676 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX); 2677 2678 /* reset ASIC (imply reset BBP) */ 2679 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC); 2680 RAL_WRITE(sc, RT2560_CSR1, 0); 2681 2682 /* disable interrupts */ 2683 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff); 2684 2685 /* reset Tx and Rx rings */ 2686 rt2560_reset_tx_ring(sc, &sc->txq); 2687 rt2560_reset_tx_ring(sc, &sc->atimq); 2688 rt2560_reset_tx_ring(sc, &sc->prioq); 2689 rt2560_reset_tx_ring(sc, &sc->bcnq); 2690 rt2560_reset_rx_ring(sc, &sc->rxq); 2691 } 2692 } 2693 2694 void 2695 rt2560_stop(void *arg) 2696 { 2697 struct rt2560_softc *sc = arg; 2698 2699 RAL_LOCK(sc); 2700 rt2560_stop_locked(sc); 2701 RAL_UNLOCK(sc); 2702 } 2703 2704 static int 2705 rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2706 const struct ieee80211_bpf_params *params) 2707 { 2708 struct ieee80211com *ic = ni->ni_ic; 2709 struct rt2560_softc *sc = ic->ic_softc; 2710 2711 RAL_LOCK(sc); 2712 2713 /* prevent management frames from being sent if we're not ready */ 2714 if (!(sc->sc_flags & RT2560_F_RUNNING)) { 2715 RAL_UNLOCK(sc); 2716 m_freem(m); 2717 return ENETDOWN; 2718 } 2719 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) { 2720 RAL_UNLOCK(sc); 2721 m_freem(m); 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 RAL_UNLOCK(sc); 2747 return EIO; /* XXX */ 2748 } 2749