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