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