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