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 ieee80211_output_seqno_assign(ni, -1, m0); 1679 1680 error = bus_dmamap_load_mbuf_sg(sc->prioq.data_dmat, data->map, m0, 1681 segs, &nsegs, 0); 1682 if (error != 0) { 1683 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1684 error); 1685 m_freem(m0); 1686 return error; 1687 } 1688 1689 if (ieee80211_radiotap_active_vap(vap)) { 1690 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1691 1692 tap->wt_flags = 0; 1693 tap->wt_rate = rate; 1694 tap->wt_antenna = sc->tx_ant; 1695 1696 ieee80211_radiotap_tx(ni->ni_vap, m0); 1697 } 1698 1699 data->m = m0; 1700 data->ni = ni; 1701 1702 /* XXX need to setup descriptor ourself */ 1703 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, 1704 rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0, 1705 segs->ds_addr); 1706 1707 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1708 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1709 BUS_DMASYNC_PREWRITE); 1710 1711 DPRINTFN(sc, 10, "sending raw frame len=%u idx=%u rate=%u\n", 1712 m0->m_pkthdr.len, sc->prioq.cur, rate); 1713 1714 /* kick prio */ 1715 sc->prioq.queued++; 1716 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT; 1717 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO); 1718 1719 return 0; 1720 } 1721 1722 static int 1723 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0, 1724 struct ieee80211_node *ni) 1725 { 1726 struct ieee80211vap *vap = ni->ni_vap; 1727 struct ieee80211com *ic = ni->ni_ic; 1728 struct rt2560_tx_desc *desc; 1729 struct rt2560_tx_data *data; 1730 struct ieee80211_frame *wh; 1731 const struct ieee80211_txparam *tp = ni->ni_txparms; 1732 struct ieee80211_key *k; 1733 struct mbuf *mnew; 1734 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1735 uint16_t dur; 1736 uint32_t flags; 1737 int nsegs, rate, error; 1738 1739 wh = mtod(m0, struct ieee80211_frame *); 1740 1741 if (m0->m_flags & M_EAPOL) { 1742 rate = tp->mgmtrate; 1743 } else if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1744 rate = tp->mcastrate; 1745 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) { 1746 rate = tp->ucastrate; 1747 } else { 1748 (void) ieee80211_ratectl_rate(ni, NULL, 0); 1749 rate = ieee80211_node_get_txrate_dot11rate(ni); 1750 } 1751 1752 if (wh->i_fc[1] & IEEE80211_FC1_PROTECTED) { 1753 k = ieee80211_crypto_encap(ni, m0); 1754 if (k == NULL) { 1755 m_freem(m0); 1756 return ENOBUFS; 1757 } 1758 1759 /* packet header may have moved, reset our local pointer */ 1760 wh = mtod(m0, struct ieee80211_frame *); 1761 } 1762 1763 flags = 0; 1764 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1765 int prot = IEEE80211_PROT_NONE; 1766 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 1767 prot = IEEE80211_PROT_RTSCTS; 1768 else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1769 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 1770 prot = ic->ic_protmode; 1771 if (prot != IEEE80211_PROT_NONE) { 1772 error = rt2560_sendprot(sc, m0, ni, prot, rate); 1773 if (error) { 1774 m_freem(m0); 1775 return error; 1776 } 1777 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS; 1778 } 1779 } 1780 1781 data = &sc->txq.data[sc->txq.cur_encrypt]; 1782 desc = &sc->txq.desc[sc->txq.cur_encrypt]; 1783 1784 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, m0, 1785 segs, &nsegs, 0); 1786 if (error != 0 && error != EFBIG) { 1787 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1788 error); 1789 m_freem(m0); 1790 return error; 1791 } 1792 if (error != 0) { 1793 mnew = m_defrag(m0, M_NOWAIT); 1794 if (mnew == NULL) { 1795 device_printf(sc->sc_dev, 1796 "could not defragment mbuf\n"); 1797 m_freem(m0); 1798 return ENOBUFS; 1799 } 1800 m0 = mnew; 1801 1802 error = bus_dmamap_load_mbuf_sg(sc->txq.data_dmat, data->map, 1803 m0, segs, &nsegs, 0); 1804 if (error != 0) { 1805 device_printf(sc->sc_dev, 1806 "could not map mbuf (error %d)\n", error); 1807 m_freem(m0); 1808 return error; 1809 } 1810 1811 /* packet header may have moved, reset our local pointer */ 1812 wh = mtod(m0, struct ieee80211_frame *); 1813 } 1814 1815 if (ieee80211_radiotap_active_vap(vap)) { 1816 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1817 1818 tap->wt_flags = 0; 1819 tap->wt_rate = rate; 1820 tap->wt_antenna = sc->tx_ant; 1821 1822 ieee80211_radiotap_tx(vap, m0); 1823 } 1824 1825 data->m = m0; 1826 data->ni = ni; 1827 1828 /* remember link conditions for rate adaptation algorithm */ 1829 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) { 1830 data->rix = ieee80211_node_get_txrate_dot11rate(ni); 1831 /* XXX probably need last rssi value and not avg */ 1832 data->rssi = ic->ic_node_getrssi(ni); 1833 } else 1834 data->rix = IEEE80211_FIXED_RATE_NONE; 1835 1836 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1837 flags |= RT2560_TX_ACK; 1838 1839 dur = ieee80211_ack_duration(ic->ic_rt, 1840 rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1841 *(uint16_t *)wh->i_dur = htole16(dur); 1842 } 1843 1844 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1, 1845 segs->ds_addr); 1846 1847 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1848 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 1849 BUS_DMASYNC_PREWRITE); 1850 1851 DPRINTFN(sc, 10, "sending data frame len=%u idx=%u rate=%u\n", 1852 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate); 1853 1854 /* kick encrypt */ 1855 sc->txq.queued++; 1856 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT; 1857 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT); 1858 1859 return 0; 1860 } 1861 1862 static int 1863 rt2560_transmit(struct ieee80211com *ic, struct mbuf *m) 1864 { 1865 struct rt2560_softc *sc = ic->ic_softc; 1866 int error; 1867 1868 RAL_LOCK(sc); 1869 if ((sc->sc_flags & RT2560_F_RUNNING) == 0) { 1870 RAL_UNLOCK(sc); 1871 return (ENXIO); 1872 } 1873 error = mbufq_enqueue(&sc->sc_snd, m); 1874 if (error) { 1875 RAL_UNLOCK(sc); 1876 return (error); 1877 } 1878 rt2560_start(sc); 1879 RAL_UNLOCK(sc); 1880 1881 return (0); 1882 } 1883 1884 static void 1885 rt2560_start(struct rt2560_softc *sc) 1886 { 1887 struct ieee80211_node *ni; 1888 struct mbuf *m; 1889 1890 RAL_LOCK_ASSERT(sc); 1891 1892 while (sc->txq.queued < RT2560_TX_RING_COUNT - 1 && 1893 (m = mbufq_dequeue(&sc->sc_snd)) != NULL) { 1894 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1895 if (rt2560_tx_data(sc, m, ni) != 0) { 1896 if_inc_counter(ni->ni_vap->iv_ifp, 1897 IFCOUNTER_OERRORS, 1); 1898 ieee80211_free_node(ni); 1899 break; 1900 } 1901 sc->sc_tx_timer = 5; 1902 } 1903 } 1904 1905 static void 1906 rt2560_watchdog(void *arg) 1907 { 1908 struct rt2560_softc *sc = arg; 1909 1910 RAL_LOCK_ASSERT(sc); 1911 1912 KASSERT(sc->sc_flags & RT2560_F_RUNNING, ("not running")); 1913 1914 if (sc->sc_invalid) /* card ejected */ 1915 return; 1916 1917 rt2560_encryption_intr(sc); 1918 rt2560_tx_intr(sc); 1919 1920 if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) { 1921 device_printf(sc->sc_dev, "device timeout\n"); 1922 rt2560_init_locked(sc); 1923 counter_u64_add(sc->sc_ic.ic_oerrors, 1); 1924 /* NB: callout is reset in rt2560_init() */ 1925 return; 1926 } 1927 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc); 1928 } 1929 1930 static void 1931 rt2560_parent(struct ieee80211com *ic) 1932 { 1933 struct rt2560_softc *sc = ic->ic_softc; 1934 int startall = 0; 1935 1936 RAL_LOCK(sc); 1937 if (ic->ic_nrunning > 0) { 1938 if ((sc->sc_flags & RT2560_F_RUNNING) == 0) { 1939 rt2560_init_locked(sc); 1940 startall = 1; 1941 } else 1942 rt2560_update_promisc(ic); 1943 } else if (sc->sc_flags & RT2560_F_RUNNING) 1944 rt2560_stop_locked(sc); 1945 RAL_UNLOCK(sc); 1946 if (startall) 1947 ieee80211_start_all(ic); 1948 } 1949 1950 static void 1951 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val) 1952 { 1953 uint32_t tmp; 1954 int ntries; 1955 1956 for (ntries = 0; ntries < 100; ntries++) { 1957 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY)) 1958 break; 1959 DELAY(1); 1960 } 1961 if (ntries == 100) { 1962 device_printf(sc->sc_dev, "could not write to BBP\n"); 1963 return; 1964 } 1965 1966 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val; 1967 RAL_WRITE(sc, RT2560_BBPCSR, tmp); 1968 1969 DPRINTFN(sc, 15, "BBP R%u <- 0x%02x\n", reg, val); 1970 } 1971 1972 static uint8_t 1973 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg) 1974 { 1975 uint32_t val; 1976 int ntries; 1977 1978 for (ntries = 0; ntries < 100; ntries++) { 1979 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY)) 1980 break; 1981 DELAY(1); 1982 } 1983 if (ntries == 100) { 1984 device_printf(sc->sc_dev, "could not read from BBP\n"); 1985 return 0; 1986 } 1987 1988 val = RT2560_BBP_BUSY | reg << 8; 1989 RAL_WRITE(sc, RT2560_BBPCSR, val); 1990 1991 for (ntries = 0; ntries < 100; ntries++) { 1992 val = RAL_READ(sc, RT2560_BBPCSR); 1993 if (!(val & RT2560_BBP_BUSY)) 1994 return val & 0xff; 1995 DELAY(1); 1996 } 1997 1998 device_printf(sc->sc_dev, "could not read from BBP\n"); 1999 return 0; 2000 } 2001 2002 static void 2003 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val) 2004 { 2005 uint32_t tmp; 2006 int ntries; 2007 2008 for (ntries = 0; ntries < 100; ntries++) { 2009 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY)) 2010 break; 2011 DELAY(1); 2012 } 2013 if (ntries == 100) { 2014 device_printf(sc->sc_dev, "could not write to RF\n"); 2015 return; 2016 } 2017 2018 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 | 2019 (reg & 0x3); 2020 RAL_WRITE(sc, RT2560_RFCSR, tmp); 2021 2022 /* remember last written value in sc */ 2023 sc->rf_regs[reg] = val; 2024 2025 DPRINTFN(sc, 15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff); 2026 } 2027 2028 static void 2029 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c) 2030 { 2031 struct ieee80211com *ic = &sc->sc_ic; 2032 uint8_t power, tmp; 2033 u_int i, chan; 2034 2035 chan = ieee80211_chan2ieee(ic, c); 2036 KASSERT(chan != 0 && chan != IEEE80211_CHAN_ANY, ("chan 0x%x", chan)); 2037 2038 if (IEEE80211_IS_CHAN_2GHZ(c)) 2039 power = min(sc->txpow[chan - 1], 31); 2040 else 2041 power = 31; 2042 2043 /* adjust txpower using ifconfig settings */ 2044 power -= (100 - ic->ic_txpowlimit) / 8; 2045 2046 DPRINTFN(sc, 2, "setting channel to %u, txpower to %u\n", chan, power); 2047 2048 switch (sc->rf_rev) { 2049 case RT2560_RF_2522: 2050 rt2560_rf_write(sc, RAL_RF1, 0x00814); 2051 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]); 2052 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2053 break; 2054 2055 case RT2560_RF_2523: 2056 rt2560_rf_write(sc, RAL_RF1, 0x08804); 2057 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]); 2058 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044); 2059 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2060 break; 2061 2062 case RT2560_RF_2524: 2063 rt2560_rf_write(sc, RAL_RF1, 0x0c808); 2064 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]); 2065 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2066 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2067 break; 2068 2069 case RT2560_RF_2525: 2070 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2071 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]); 2072 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2073 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2074 2075 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2076 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]); 2077 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2078 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2079 break; 2080 2081 case RT2560_RF_2525E: 2082 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2083 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]); 2084 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2085 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282); 2086 break; 2087 2088 case RT2560_RF_2526: 2089 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]); 2090 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 2091 rt2560_rf_write(sc, RAL_RF1, 0x08804); 2092 2093 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]); 2094 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2095 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 2096 break; 2097 2098 /* dual-band RF */ 2099 case RT2560_RF_5222: 2100 for (i = 0; rt2560_rf5222[i].chan != chan; i++); 2101 2102 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1); 2103 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2); 2104 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2105 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4); 2106 break; 2107 default: 2108 printf("unknown ral rev=%d\n", sc->rf_rev); 2109 } 2110 2111 /* XXX */ 2112 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 2113 /* set Japan filter bit for channel 14 */ 2114 tmp = rt2560_bbp_read(sc, 70); 2115 2116 tmp &= ~RT2560_JAPAN_FILTER; 2117 if (chan == 14) 2118 tmp |= RT2560_JAPAN_FILTER; 2119 2120 rt2560_bbp_write(sc, 70, tmp); 2121 2122 /* clear CRC errors */ 2123 RAL_READ(sc, RT2560_CNT0); 2124 } 2125 } 2126 2127 static void 2128 rt2560_getradiocaps(struct ieee80211com *ic, 2129 int maxchans, int *nchans, struct ieee80211_channel chans[]) 2130 { 2131 struct rt2560_softc *sc = ic->ic_softc; 2132 uint8_t bands[IEEE80211_MODE_BYTES]; 2133 2134 memset(bands, 0, sizeof(bands)); 2135 setbit(bands, IEEE80211_MODE_11B); 2136 setbit(bands, IEEE80211_MODE_11G); 2137 ieee80211_add_channels_default_2ghz(chans, maxchans, nchans, bands, 0); 2138 2139 if (sc->rf_rev == RT2560_RF_5222) { 2140 setbit(bands, IEEE80211_MODE_11A); 2141 ieee80211_add_channel_list_5ghz(chans, maxchans, nchans, 2142 rt2560_chan_5ghz, nitems(rt2560_chan_5ghz), bands, 0); 2143 } 2144 } 2145 2146 static void 2147 rt2560_set_channel(struct ieee80211com *ic) 2148 { 2149 struct rt2560_softc *sc = ic->ic_softc; 2150 2151 RAL_LOCK(sc); 2152 rt2560_set_chan(sc, ic->ic_curchan); 2153 RAL_UNLOCK(sc); 2154 2155 } 2156 2157 #if 0 2158 /* 2159 * Disable RF auto-tuning. 2160 */ 2161 static void 2162 rt2560_disable_rf_tune(struct rt2560_softc *sc) 2163 { 2164 uint32_t tmp; 2165 2166 if (sc->rf_rev != RT2560_RF_2523) { 2167 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE; 2168 rt2560_rf_write(sc, RAL_RF1, tmp); 2169 } 2170 2171 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE; 2172 rt2560_rf_write(sc, RAL_RF3, tmp); 2173 2174 DPRINTFN(sc, 2, "%s", "disabling RF autotune\n"); 2175 } 2176 #endif 2177 2178 /* 2179 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF 2180 * synchronization. 2181 */ 2182 static void 2183 rt2560_enable_tsf_sync(struct rt2560_softc *sc) 2184 { 2185 struct ieee80211com *ic = &sc->sc_ic; 2186 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2187 uint16_t logcwmin, preload; 2188 uint32_t tmp; 2189 2190 /* first, disable TSF synchronization */ 2191 RAL_WRITE(sc, RT2560_CSR14, 0); 2192 2193 tmp = 16 * vap->iv_bss->ni_intval; 2194 RAL_WRITE(sc, RT2560_CSR12, tmp); 2195 2196 RAL_WRITE(sc, RT2560_CSR13, 0); 2197 2198 logcwmin = 5; 2199 preload = (vap->iv_opmode == IEEE80211_M_STA) ? 384 : 1024; 2200 tmp = logcwmin << 16 | preload; 2201 RAL_WRITE(sc, RT2560_BCNOCSR, tmp); 2202 2203 /* finally, enable TSF synchronization */ 2204 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN; 2205 if (ic->ic_opmode == IEEE80211_M_STA) 2206 tmp |= RT2560_ENABLE_TSF_SYNC(1); 2207 else 2208 tmp |= RT2560_ENABLE_TSF_SYNC(2) | 2209 RT2560_ENABLE_BEACON_GENERATOR; 2210 RAL_WRITE(sc, RT2560_CSR14, tmp); 2211 2212 DPRINTF(sc, "%s", "enabling TSF synchronization\n"); 2213 } 2214 2215 static void 2216 rt2560_enable_tsf(struct rt2560_softc *sc) 2217 { 2218 RAL_WRITE(sc, RT2560_CSR14, 0); 2219 RAL_WRITE(sc, RT2560_CSR14, 2220 RT2560_ENABLE_TSF_SYNC(2) | RT2560_ENABLE_TSF); 2221 } 2222 2223 static void 2224 rt2560_update_plcp(struct rt2560_softc *sc) 2225 { 2226 struct ieee80211com *ic = &sc->sc_ic; 2227 2228 /* no short preamble for 1Mbps */ 2229 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400); 2230 2231 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) { 2232 /* values taken from the reference driver */ 2233 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401); 2234 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402); 2235 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403); 2236 } else { 2237 /* same values as above or'ed 0x8 */ 2238 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409); 2239 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a); 2240 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b); 2241 } 2242 2243 DPRINTF(sc, "updating PLCP for %s preamble\n", 2244 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"); 2245 } 2246 2247 /* 2248 * This function can be called by ieee80211_set_shortslottime(). Refer to 2249 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed. 2250 */ 2251 static void 2252 rt2560_update_slot(struct ieee80211com *ic) 2253 { 2254 struct rt2560_softc *sc = ic->ic_softc; 2255 uint8_t slottime; 2256 uint16_t tx_sifs, tx_pifs, tx_difs, eifs; 2257 uint32_t tmp; 2258 2259 #ifndef FORCE_SLOTTIME 2260 slottime = IEEE80211_GET_SLOTTIME(ic); 2261 #else 2262 /* 2263 * Setting slot time according to "short slot time" capability 2264 * in beacon/probe_resp seems to cause problem to acknowledge 2265 * certain AP's data frames transimitted at CCK/DS rates: the 2266 * problematic AP keeps retransmitting data frames, probably 2267 * because MAC level acks are not received by hardware. 2268 * So we cheat a little bit here by claiming we are capable of 2269 * "short slot time" but setting hardware slot time to the normal 2270 * slot time. ral(4) does not seem to have trouble to receive 2271 * frames transmitted using short slot time even if hardware 2272 * slot time is set to normal slot time. If we didn't use this 2273 * trick, we would have to claim that short slot time is not 2274 * supported; this would give relative poor RX performance 2275 * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short 2276 * slot time. 2277 */ 2278 slottime = IEEE80211_DUR_SLOT; 2279 #endif 2280 2281 /* update the MAC slot boundaries */ 2282 tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND; 2283 tx_pifs = tx_sifs + slottime; 2284 tx_difs = IEEE80211_DUR_DIFS(tx_sifs, slottime); 2285 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60; 2286 2287 tmp = RAL_READ(sc, RT2560_CSR11); 2288 tmp = (tmp & ~0x1f00) | slottime << 8; 2289 RAL_WRITE(sc, RT2560_CSR11, tmp); 2290 2291 tmp = tx_pifs << 16 | tx_sifs; 2292 RAL_WRITE(sc, RT2560_CSR18, tmp); 2293 2294 tmp = eifs << 16 | tx_difs; 2295 RAL_WRITE(sc, RT2560_CSR19, tmp); 2296 2297 DPRINTF(sc, "setting slottime to %uus\n", slottime); 2298 } 2299 2300 static void 2301 rt2560_set_basicrates(struct rt2560_softc *sc, 2302 const struct ieee80211_rateset *rs) 2303 { 2304 struct ieee80211com *ic = &sc->sc_ic; 2305 uint32_t mask = 0; 2306 uint8_t rate; 2307 int i; 2308 2309 for (i = 0; i < rs->rs_nrates; i++) { 2310 rate = rs->rs_rates[i]; 2311 2312 if (!(rate & IEEE80211_RATE_BASIC)) 2313 continue; 2314 2315 mask |= 1 << ieee80211_legacy_rate_lookup(ic->ic_rt, 2316 IEEE80211_RV(rate)); 2317 } 2318 2319 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, mask); 2320 2321 DPRINTF(sc, "Setting basic rate mask to 0x%x\n", mask); 2322 } 2323 2324 static void 2325 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2) 2326 { 2327 uint32_t tmp; 2328 2329 /* set ON period to 70ms and OFF period to 30ms */ 2330 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30; 2331 RAL_WRITE(sc, RT2560_LEDCSR, tmp); 2332 } 2333 2334 static void 2335 rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid) 2336 { 2337 uint32_t tmp; 2338 2339 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 2340 RAL_WRITE(sc, RT2560_CSR5, tmp); 2341 2342 tmp = bssid[4] | bssid[5] << 8; 2343 RAL_WRITE(sc, RT2560_CSR6, tmp); 2344 2345 DPRINTF(sc, "setting BSSID to %6D\n", bssid, ":"); 2346 } 2347 2348 static void 2349 rt2560_set_macaddr(struct rt2560_softc *sc, const uint8_t *addr) 2350 { 2351 uint32_t tmp; 2352 2353 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 2354 RAL_WRITE(sc, RT2560_CSR3, tmp); 2355 2356 tmp = addr[4] | addr[5] << 8; 2357 RAL_WRITE(sc, RT2560_CSR4, tmp); 2358 2359 DPRINTF(sc, "setting MAC address to %6D\n", addr, ":"); 2360 } 2361 2362 static void 2363 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr) 2364 { 2365 uint32_t tmp; 2366 2367 tmp = RAL_READ(sc, RT2560_CSR3); 2368 addr[0] = tmp & 0xff; 2369 addr[1] = (tmp >> 8) & 0xff; 2370 addr[2] = (tmp >> 16) & 0xff; 2371 addr[3] = (tmp >> 24); 2372 2373 tmp = RAL_READ(sc, RT2560_CSR4); 2374 addr[4] = tmp & 0xff; 2375 addr[5] = (tmp >> 8) & 0xff; 2376 } 2377 2378 static void 2379 rt2560_update_promisc(struct ieee80211com *ic) 2380 { 2381 struct rt2560_softc *sc = ic->ic_softc; 2382 uint32_t tmp; 2383 2384 tmp = RAL_READ(sc, RT2560_RXCSR0); 2385 2386 tmp &= ~RT2560_DROP_NOT_TO_ME; 2387 if (ic->ic_promisc == 0) 2388 tmp |= RT2560_DROP_NOT_TO_ME; 2389 2390 RAL_WRITE(sc, RT2560_RXCSR0, tmp); 2391 2392 DPRINTF(sc, "%s promiscuous mode\n", 2393 (ic->ic_promisc > 0) ? "entering" : "leaving"); 2394 } 2395 2396 static const char * 2397 rt2560_get_rf(int rev) 2398 { 2399 switch (rev) { 2400 case RT2560_RF_2522: return "RT2522"; 2401 case RT2560_RF_2523: return "RT2523"; 2402 case RT2560_RF_2524: return "RT2524"; 2403 case RT2560_RF_2525: return "RT2525"; 2404 case RT2560_RF_2525E: return "RT2525e"; 2405 case RT2560_RF_2526: return "RT2526"; 2406 case RT2560_RF_5222: return "RT5222"; 2407 default: return "unknown"; 2408 } 2409 } 2410 2411 static void 2412 rt2560_read_config(struct rt2560_softc *sc) 2413 { 2414 uint16_t val; 2415 int i; 2416 2417 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0); 2418 sc->rf_rev = (val >> 11) & 0x7; 2419 sc->hw_radio = (val >> 10) & 0x1; 2420 sc->led_mode = (val >> 6) & 0x7; 2421 sc->rx_ant = (val >> 4) & 0x3; 2422 sc->tx_ant = (val >> 2) & 0x3; 2423 sc->nb_ant = val & 0x3; 2424 2425 /* read default values for BBP registers */ 2426 for (i = 0; i < 16; i++) { 2427 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i); 2428 if (val == 0 || val == 0xffff) 2429 continue; 2430 2431 sc->bbp_prom[i].reg = val >> 8; 2432 sc->bbp_prom[i].val = val & 0xff; 2433 } 2434 2435 /* read Tx power for all b/g channels */ 2436 for (i = 0; i < 14 / 2; i++) { 2437 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i); 2438 sc->txpow[i * 2] = val & 0xff; 2439 sc->txpow[i * 2 + 1] = val >> 8; 2440 } 2441 for (i = 0; i < 14; ++i) { 2442 if (sc->txpow[i] > 31) 2443 sc->txpow[i] = 24; 2444 } 2445 2446 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE); 2447 if ((val & 0xff) == 0xff) 2448 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR; 2449 else 2450 sc->rssi_corr = val & 0xff; 2451 DPRINTF(sc, "rssi correction %d, calibrate 0x%02x\n", 2452 sc->rssi_corr, val); 2453 } 2454 2455 static void 2456 rt2560_scan_start(struct ieee80211com *ic) 2457 { 2458 struct rt2560_softc *sc = ic->ic_softc; 2459 2460 /* abort TSF synchronization */ 2461 RAL_WRITE(sc, RT2560_CSR14, 0); 2462 rt2560_set_bssid(sc, ieee80211broadcastaddr); 2463 } 2464 2465 static void 2466 rt2560_scan_end(struct ieee80211com *ic) 2467 { 2468 struct rt2560_softc *sc = ic->ic_softc; 2469 struct ieee80211vap *vap = ic->ic_scan->ss_vap; 2470 2471 rt2560_enable_tsf_sync(sc); 2472 /* XXX keep local copy */ 2473 rt2560_set_bssid(sc, vap->iv_bss->ni_bssid); 2474 } 2475 2476 static int 2477 rt2560_bbp_init(struct rt2560_softc *sc) 2478 { 2479 int i, ntries; 2480 2481 /* wait for BBP to be ready */ 2482 for (ntries = 0; ntries < 100; ntries++) { 2483 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0) 2484 break; 2485 DELAY(1); 2486 } 2487 if (ntries == 100) { 2488 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 2489 return EIO; 2490 } 2491 2492 /* initialize BBP registers to default values */ 2493 for (i = 0; i < nitems(rt2560_def_bbp); i++) { 2494 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg, 2495 rt2560_def_bbp[i].val); 2496 } 2497 2498 /* initialize BBP registers to values stored in EEPROM */ 2499 for (i = 0; i < 16; i++) { 2500 if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0) 2501 break; 2502 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 2503 } 2504 rt2560_bbp_write(sc, 17, 0x48); /* XXX restore bbp17 */ 2505 2506 return 0; 2507 } 2508 2509 static void 2510 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna) 2511 { 2512 uint32_t tmp; 2513 uint8_t tx; 2514 2515 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK; 2516 if (antenna == 1) 2517 tx |= RT2560_BBP_ANTA; 2518 else if (antenna == 2) 2519 tx |= RT2560_BBP_ANTB; 2520 else 2521 tx |= RT2560_BBP_DIVERSITY; 2522 2523 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */ 2524 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 || 2525 sc->rf_rev == RT2560_RF_5222) 2526 tx |= RT2560_BBP_FLIPIQ; 2527 2528 rt2560_bbp_write(sc, RT2560_BBP_TX, tx); 2529 2530 /* update values for CCK and OFDM in BBPCSR1 */ 2531 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007; 2532 tmp |= (tx & 0x7) << 16 | (tx & 0x7); 2533 RAL_WRITE(sc, RT2560_BBPCSR1, tmp); 2534 } 2535 2536 static void 2537 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna) 2538 { 2539 uint8_t rx; 2540 2541 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK; 2542 if (antenna == 1) 2543 rx |= RT2560_BBP_ANTA; 2544 else if (antenna == 2) 2545 rx |= RT2560_BBP_ANTB; 2546 else 2547 rx |= RT2560_BBP_DIVERSITY; 2548 2549 /* need to force no I/Q flip for RF 2525e and 2526 */ 2550 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526) 2551 rx &= ~RT2560_BBP_FLIPIQ; 2552 2553 rt2560_bbp_write(sc, RT2560_BBP_RX, rx); 2554 } 2555 2556 static void 2557 rt2560_init_locked(struct rt2560_softc *sc) 2558 { 2559 struct ieee80211com *ic = &sc->sc_ic; 2560 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2561 uint32_t tmp; 2562 int i; 2563 2564 RAL_LOCK_ASSERT(sc); 2565 2566 rt2560_stop_locked(sc); 2567 2568 /* setup tx rings */ 2569 tmp = RT2560_PRIO_RING_COUNT << 24 | 2570 RT2560_ATIM_RING_COUNT << 16 | 2571 RT2560_TX_RING_COUNT << 8 | 2572 RT2560_TX_DESC_SIZE; 2573 2574 /* rings must be initialized in this exact order */ 2575 RAL_WRITE(sc, RT2560_TXCSR2, tmp); 2576 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr); 2577 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr); 2578 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr); 2579 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr); 2580 2581 /* setup rx ring */ 2582 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE; 2583 2584 RAL_WRITE(sc, RT2560_RXCSR1, tmp); 2585 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr); 2586 2587 /* initialize MAC registers to default values */ 2588 for (i = 0; i < nitems(rt2560_def_mac); i++) 2589 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val); 2590 2591 rt2560_set_macaddr(sc, vap ? vap->iv_myaddr : ic->ic_macaddr); 2592 2593 /* set basic rate set (will be updated later) */ 2594 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153); 2595 2596 rt2560_update_slot(ic); 2597 rt2560_update_plcp(sc); 2598 rt2560_update_led(sc, 0, 0); 2599 2600 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC); 2601 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY); 2602 2603 if (rt2560_bbp_init(sc) != 0) { 2604 rt2560_stop_locked(sc); 2605 return; 2606 } 2607 2608 rt2560_set_txantenna(sc, sc->tx_ant); 2609 rt2560_set_rxantenna(sc, sc->rx_ant); 2610 2611 /* set default BSS channel */ 2612 rt2560_set_chan(sc, ic->ic_curchan); 2613 2614 /* kick Rx */ 2615 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR; 2616 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2617 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR; 2618 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 2619 ic->ic_opmode != IEEE80211_M_MBSS) 2620 tmp |= RT2560_DROP_TODS; 2621 if (ic->ic_promisc == 0) 2622 tmp |= RT2560_DROP_NOT_TO_ME; 2623 } 2624 RAL_WRITE(sc, RT2560_RXCSR0, tmp); 2625 2626 /* clear old FCS and Rx FIFO errors */ 2627 RAL_READ(sc, RT2560_CNT0); 2628 RAL_READ(sc, RT2560_CNT4); 2629 2630 /* clear any pending interrupts */ 2631 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff); 2632 2633 /* enable interrupts */ 2634 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK); 2635 2636 sc->sc_flags |= RT2560_F_RUNNING; 2637 2638 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog, sc); 2639 } 2640 2641 static void 2642 rt2560_init(void *priv) 2643 { 2644 struct rt2560_softc *sc = priv; 2645 struct ieee80211com *ic = &sc->sc_ic; 2646 2647 RAL_LOCK(sc); 2648 rt2560_init_locked(sc); 2649 RAL_UNLOCK(sc); 2650 2651 if (sc->sc_flags & RT2560_F_RUNNING) 2652 ieee80211_start_all(ic); /* start all vap's */ 2653 } 2654 2655 static void 2656 rt2560_stop_locked(struct rt2560_softc *sc) 2657 { 2658 volatile int *flags = &sc->sc_flags; 2659 2660 RAL_LOCK_ASSERT(sc); 2661 2662 while (*flags & RT2560_F_INPUT_RUNNING) 2663 msleep(sc, &sc->sc_mtx, 0, "ralrunning", hz/10); 2664 2665 callout_stop(&sc->watchdog_ch); 2666 sc->sc_tx_timer = 0; 2667 2668 if (sc->sc_flags & RT2560_F_RUNNING) { 2669 sc->sc_flags &= ~RT2560_F_RUNNING; 2670 2671 /* abort Tx */ 2672 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX); 2673 2674 /* disable Rx */ 2675 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX); 2676 2677 /* reset ASIC (imply reset BBP) */ 2678 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC); 2679 RAL_WRITE(sc, RT2560_CSR1, 0); 2680 2681 /* disable interrupts */ 2682 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff); 2683 2684 /* reset Tx and Rx rings */ 2685 rt2560_reset_tx_ring(sc, &sc->txq); 2686 rt2560_reset_tx_ring(sc, &sc->atimq); 2687 rt2560_reset_tx_ring(sc, &sc->prioq); 2688 rt2560_reset_tx_ring(sc, &sc->bcnq); 2689 rt2560_reset_rx_ring(sc, &sc->rxq); 2690 } 2691 } 2692 2693 void 2694 rt2560_stop(void *arg) 2695 { 2696 struct rt2560_softc *sc = arg; 2697 2698 RAL_LOCK(sc); 2699 rt2560_stop_locked(sc); 2700 RAL_UNLOCK(sc); 2701 } 2702 2703 static int 2704 rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2705 const struct ieee80211_bpf_params *params) 2706 { 2707 struct ieee80211com *ic = ni->ni_ic; 2708 struct rt2560_softc *sc = ic->ic_softc; 2709 2710 RAL_LOCK(sc); 2711 2712 /* prevent management frames from being sent if we're not ready */ 2713 if (!(sc->sc_flags & RT2560_F_RUNNING)) { 2714 RAL_UNLOCK(sc); 2715 m_freem(m); 2716 return ENETDOWN; 2717 } 2718 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) { 2719 RAL_UNLOCK(sc); 2720 m_freem(m); 2721 return ENOBUFS; /* XXX */ 2722 } 2723 2724 if (params == NULL) { 2725 /* 2726 * Legacy path; interpret frame contents to decide 2727 * precisely how to send the frame. 2728 */ 2729 if (rt2560_tx_mgt(sc, m, ni) != 0) 2730 goto bad; 2731 } else { 2732 /* 2733 * Caller supplied explicit parameters to use in 2734 * sending the frame. 2735 */ 2736 if (rt2560_tx_raw(sc, m, ni, params)) 2737 goto bad; 2738 } 2739 sc->sc_tx_timer = 5; 2740 2741 RAL_UNLOCK(sc); 2742 2743 return 0; 2744 bad: 2745 RAL_UNLOCK(sc); 2746 return EIO; /* XXX */ 2747 } 2748