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