1 /* $FreeBSD: head/sys/dev/ral/rt2560.c 195618 2009-07-11 15:02:45Z rpaulo $ */ 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 * $FreeBSD: head/sys/dev/ral/rt2560.c 195618 2009-07-11 15:02:45Z rpaulo $ 20 */ 21 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 #include <sys/rman.h> 42 43 #include <net/bpf.h> 44 #include <net/if.h> 45 #include <net/if_arp.h> 46 #include <net/ethernet.h> 47 #include <net/if_dl.h> 48 #include <net/if_media.h> 49 #include <net/if_types.h> 50 #include <net/ifq_var.h> 51 52 #include <netproto/802_11/ieee80211_var.h> 53 #include <netproto/802_11/ieee80211_radiotap.h> 54 #include <netproto/802_11/ieee80211_regdomain.h> 55 #include <netproto/802_11/ieee80211_ratectl.h> 56 57 #include <netinet/in.h> 58 #include <netinet/in_systm.h> 59 #include <netinet/in_var.h> 60 #include <netinet/ip.h> 61 #include <netinet/if_ether.h> 62 63 #include <dev/netif/ral/rt2560reg.h> 64 #include <dev/netif/ral/rt2560var.h> 65 66 #define RT2560_RSSI(sc, rssi) \ 67 ((rssi) > (RT2560_NOISE_FLOOR + (sc)->rssi_corr) ? \ 68 ((rssi) - RT2560_NOISE_FLOOR - (sc)->rssi_corr) : 0) 69 70 #define RAL_DEBUG 71 #ifdef RAL_DEBUG 72 #define DPRINTF(sc, fmt, ...) do { \ 73 if (sc->sc_debug > 0) \ 74 kprintf(fmt, __VA_ARGS__); \ 75 } while (0) 76 #define DPRINTFN(sc, n, fmt, ...) do { \ 77 if (sc->sc_debug >= (n)) \ 78 kprintf(fmt, __VA_ARGS__); \ 79 } while (0) 80 #else 81 #define DPRINTF(sc, fmt, ...) 82 #define DPRINTFN(sc, n, fmt, ...) 83 #endif 84 85 static struct ieee80211vap *rt2560_vap_create(struct ieee80211com *, 86 const char name[IFNAMSIZ], int unit, int opmode, 87 int flags, const uint8_t bssid[IEEE80211_ADDR_LEN], 88 const uint8_t mac[IEEE80211_ADDR_LEN]); 89 static void rt2560_vap_delete(struct ieee80211vap *); 90 static void rt2560_dma_map_addr(void *, bus_dma_segment_t *, int, 91 int); 92 static int rt2560_alloc_tx_ring(struct rt2560_softc *, 93 struct rt2560_tx_ring *, int); 94 static void rt2560_reset_tx_ring(struct rt2560_softc *, 95 struct rt2560_tx_ring *); 96 static void rt2560_free_tx_ring(struct rt2560_softc *, 97 struct rt2560_tx_ring *); 98 static int rt2560_alloc_rx_ring(struct rt2560_softc *, 99 struct rt2560_rx_ring *, int); 100 static void rt2560_reset_rx_ring(struct rt2560_softc *, 101 struct rt2560_rx_ring *); 102 static void rt2560_free_rx_ring(struct rt2560_softc *, 103 struct rt2560_rx_ring *); 104 static int rt2560_newstate(struct ieee80211vap *, 105 enum ieee80211_state, int); 106 static uint16_t rt2560_eeprom_read(struct rt2560_softc *, uint8_t); 107 static void rt2560_encryption_intr(struct rt2560_softc *); 108 static void rt2560_tx_intr(struct rt2560_softc *); 109 static void rt2560_prio_intr(struct rt2560_softc *); 110 static void rt2560_decryption_intr(struct rt2560_softc *); 111 static void rt2560_rx_intr(struct rt2560_softc *); 112 static void rt2560_beacon_update(struct ieee80211vap *, int item); 113 static void rt2560_beacon_expire(struct rt2560_softc *); 114 static void rt2560_wakeup_expire(struct rt2560_softc *); 115 static void rt2560_scan_start(struct ieee80211com *); 116 static void rt2560_scan_end(struct ieee80211com *); 117 static void rt2560_set_channel(struct ieee80211com *); 118 static void rt2560_setup_tx_desc(struct rt2560_softc *, 119 struct rt2560_tx_desc *, uint32_t, int, int, int, 120 bus_addr_t); 121 static int rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *, 122 struct ieee80211_node *); 123 static int rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *, 124 struct ieee80211_node *); 125 static int rt2560_tx_data(struct rt2560_softc *, struct mbuf *, 126 struct ieee80211_node *); 127 static void rt2560_start_locked(struct ifnet *); 128 static void rt2560_start(struct ifnet *); 129 static void rt2560_watchdog_callout(void *); 130 static int rt2560_ioctl(struct ifnet *, u_long, caddr_t, 131 struct ucred *); 132 static void rt2560_bbp_write(struct rt2560_softc *, uint8_t, 133 uint8_t); 134 static uint8_t rt2560_bbp_read(struct rt2560_softc *, uint8_t); 135 static void rt2560_rf_write(struct rt2560_softc *, uint8_t, 136 uint32_t); 137 static void rt2560_set_chan(struct rt2560_softc *, 138 struct ieee80211_channel *); 139 #if 0 140 static void rt2560_disable_rf_tune(struct rt2560_softc *); 141 #endif 142 static void rt2560_enable_tsf_sync(struct rt2560_softc *); 143 static void rt2560_enable_tsf(struct rt2560_softc *); 144 static void rt2560_update_plcp(struct rt2560_softc *); 145 static void rt2560_update_slot(struct ifnet *); 146 static void rt2560_set_basicrates(struct rt2560_softc *); 147 static void rt2560_update_led(struct rt2560_softc *, int, int); 148 static void rt2560_set_bssid(struct rt2560_softc *, const uint8_t *); 149 static void rt2560_set_macaddr(struct rt2560_softc *, uint8_t *); 150 static void rt2560_get_macaddr(struct rt2560_softc *, uint8_t *); 151 static void rt2560_update_promisc(struct ifnet *); 152 static const char *rt2560_get_rf(int); 153 static void rt2560_read_config(struct rt2560_softc *); 154 static int rt2560_bbp_init(struct rt2560_softc *); 155 static void rt2560_set_txantenna(struct rt2560_softc *, int); 156 static void rt2560_set_rxantenna(struct rt2560_softc *, int); 157 static void rt2560_init_locked(struct rt2560_softc *); 158 static void rt2560_init(void *); 159 static void rt2560_stop_locked(struct rt2560_softc *); 160 static int rt2560_raw_xmit(struct ieee80211_node *, struct mbuf *, 161 const struct ieee80211_bpf_params *); 162 163 static const struct { 164 uint32_t reg; 165 uint32_t val; 166 } rt2560_def_mac[] = { 167 RT2560_DEF_MAC 168 }; 169 170 static const struct { 171 uint8_t reg; 172 uint8_t val; 173 } rt2560_def_bbp[] = { 174 RT2560_DEF_BBP 175 }; 176 177 static const uint32_t rt2560_rf2522_r2[] = RT2560_RF2522_R2; 178 static const uint32_t rt2560_rf2523_r2[] = RT2560_RF2523_R2; 179 static const uint32_t rt2560_rf2524_r2[] = RT2560_RF2524_R2; 180 static const uint32_t rt2560_rf2525_r2[] = RT2560_RF2525_R2; 181 static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2; 182 static const uint32_t rt2560_rf2525e_r2[] = RT2560_RF2525E_R2; 183 static const uint32_t rt2560_rf2526_r2[] = RT2560_RF2526_R2; 184 static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2; 185 186 static const struct { 187 uint8_t chan; 188 uint32_t r1, r2, r4; 189 } rt2560_rf5222[] = { 190 RT2560_RF5222 191 }; 192 193 int 194 rt2560_attach(device_t dev, int id) 195 { 196 struct rt2560_softc *sc = device_get_softc(dev); 197 struct ieee80211com *ic; 198 struct ifnet *ifp; 199 int error; 200 uint8_t bands; 201 uint8_t macaddr[IEEE80211_ADDR_LEN]; 202 struct sysctl_ctx_list *ctx; 203 struct sysctl_oid *tree; 204 205 sc->sc_dev = dev; 206 207 callout_init(&sc->watchdog_ch); 208 209 /* retrieve RT2560 rev. no */ 210 sc->asic_rev = RAL_READ(sc, RT2560_CSR0); 211 212 /* retrieve RF rev. no and various other things from EEPROM */ 213 rt2560_read_config(sc); 214 215 device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n", 216 sc->asic_rev, rt2560_get_rf(sc->rf_rev)); 217 218 /* 219 * Allocate Tx and Rx rings. 220 */ 221 error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT); 222 if (error != 0) { 223 device_printf(sc->sc_dev, "could not allocate Tx ring\n"); 224 goto fail1; 225 } 226 227 error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT); 228 if (error != 0) { 229 device_printf(sc->sc_dev, "could not allocate ATIM ring\n"); 230 goto fail2; 231 } 232 233 error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT); 234 if (error != 0) { 235 device_printf(sc->sc_dev, "could not allocate Prio ring\n"); 236 goto fail3; 237 } 238 239 error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT); 240 if (error != 0) { 241 device_printf(sc->sc_dev, "could not allocate Beacon ring\n"); 242 goto fail4; 243 } 244 245 error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT); 246 if (error != 0) { 247 device_printf(sc->sc_dev, "could not allocate Rx ring\n"); 248 goto fail5; 249 } 250 251 ifp = sc->sc_ifp = if_alloc(IFT_IEEE80211); 252 if (ifp == NULL) { 253 device_printf(sc->sc_dev, "can not if_alloc()\n"); 254 goto fail6; 255 } 256 ic = ifp->if_l2com; 257 258 /* retrieve MAC address */ 259 rt2560_get_macaddr(sc, macaddr); 260 261 ifp->if_softc = sc; 262 if_initname(ifp, device_get_name(dev), device_get_unit(dev)); 263 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST; 264 ifp->if_init = rt2560_init; 265 ifp->if_ioctl = rt2560_ioctl; 266 ifp->if_start = rt2560_start; 267 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN); 268 ifq_set_ready(&ifp->if_snd); 269 270 ic->ic_ifp = ifp; 271 ic->ic_opmode = IEEE80211_M_STA; 272 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */ 273 274 /* set device capabilities */ 275 ic->ic_caps = 276 IEEE80211_C_STA /* station mode */ 277 | IEEE80211_C_IBSS /* ibss, nee adhoc, mode */ 278 | IEEE80211_C_HOSTAP /* hostap mode */ 279 | IEEE80211_C_MONITOR /* monitor mode */ 280 | IEEE80211_C_AHDEMO /* adhoc demo mode */ 281 | IEEE80211_C_WDS /* 4-address traffic works */ 282 | IEEE80211_C_MBSS /* mesh point link mode */ 283 | IEEE80211_C_SHPREAMBLE /* short preamble supported */ 284 | IEEE80211_C_SHSLOT /* short slot time supported */ 285 | IEEE80211_C_WPA /* capable of WPA1+WPA2 */ 286 | IEEE80211_C_BGSCAN /* capable of bg scanning */ 287 #ifdef notyet 288 | IEEE80211_C_TXFRAG /* handle tx frags */ 289 #endif 290 ; 291 292 bands = 0; 293 setbit(&bands, IEEE80211_MODE_11B); 294 setbit(&bands, IEEE80211_MODE_11G); 295 if (sc->rf_rev == RT2560_RF_5222) 296 setbit(&bands, IEEE80211_MODE_11A); 297 ieee80211_init_channels(ic, NULL, &bands); 298 299 ieee80211_ifattach(ic, macaddr); 300 ic->ic_raw_xmit = rt2560_raw_xmit; 301 ic->ic_updateslot = rt2560_update_slot; 302 ic->ic_update_promisc = rt2560_update_promisc; 303 ic->ic_scan_start = rt2560_scan_start; 304 ic->ic_scan_end = rt2560_scan_end; 305 ic->ic_set_channel = rt2560_set_channel; 306 307 ic->ic_vap_create = rt2560_vap_create; 308 ic->ic_vap_delete = rt2560_vap_delete; 309 310 ieee80211_radiotap_attach(ic, 311 &sc->sc_txtap.wt_ihdr, sizeof(sc->sc_txtap), 312 RT2560_TX_RADIOTAP_PRESENT, 313 &sc->sc_rxtap.wr_ihdr, sizeof(sc->sc_rxtap), 314 RT2560_RX_RADIOTAP_PRESENT); 315 316 /* 317 * Add a few sysctl knobs. 318 */ 319 ctx = &sc->sc_sysctl_ctx; 320 sysctl_ctx_init(ctx); 321 tree = SYSCTL_ADD_NODE(ctx, SYSCTL_STATIC_CHILDREN(_hw), 322 OID_AUTO, 323 device_get_nameunit(sc->sc_dev), 324 CTLFLAG_RD, 0, ""); 325 if (tree == NULL) { 326 device_printf(sc->sc_dev, "can't add sysctl node\n"); 327 goto fail6; 328 } 329 330 #ifdef RAL_DEBUG 331 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 332 "debug", CTLFLAG_RW, &sc->sc_debug, 0, "debug msgs"); 333 #endif 334 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 335 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)"); 336 337 SYSCTL_ADD_INT(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 338 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)"); 339 340 if (bootverbose) 341 ieee80211_announce(ic); 342 343 return 0; 344 345 fail6: rt2560_free_rx_ring(sc, &sc->rxq); 346 fail5: rt2560_free_tx_ring(sc, &sc->bcnq); 347 fail4: rt2560_free_tx_ring(sc, &sc->prioq); 348 fail3: rt2560_free_tx_ring(sc, &sc->atimq); 349 fail2: rt2560_free_tx_ring(sc, &sc->txq); 350 fail1: 351 352 return ENXIO; 353 } 354 355 int 356 rt2560_detach(void *xsc) 357 { 358 struct rt2560_softc *sc = xsc; 359 struct ifnet *ifp = sc->sc_ifp; 360 struct ieee80211com *ic = ifp->if_l2com; 361 362 rt2560_stop(sc); 363 364 ieee80211_ifdetach(ic); 365 366 rt2560_free_tx_ring(sc, &sc->txq); 367 rt2560_free_tx_ring(sc, &sc->atimq); 368 rt2560_free_tx_ring(sc, &sc->prioq); 369 rt2560_free_tx_ring(sc, &sc->bcnq); 370 rt2560_free_rx_ring(sc, &sc->rxq); 371 372 if_free(ifp); 373 374 return 0; 375 } 376 377 static struct ieee80211vap * 378 rt2560_vap_create(struct ieee80211com *ic, 379 const char name[IFNAMSIZ], int unit, int opmode, int flags, 380 const uint8_t bssid[IEEE80211_ADDR_LEN], 381 const uint8_t mac[IEEE80211_ADDR_LEN]) 382 { 383 struct ifnet *ifp = ic->ic_ifp; 384 struct rt2560_vap *rvp; 385 struct ieee80211vap *vap; 386 387 switch (opmode) { 388 case IEEE80211_M_STA: 389 case IEEE80211_M_IBSS: 390 case IEEE80211_M_AHDEMO: 391 case IEEE80211_M_MONITOR: 392 case IEEE80211_M_HOSTAP: 393 case IEEE80211_M_MBSS: 394 /* XXXRP: TBD */ 395 if (!TAILQ_EMPTY(&ic->ic_vaps)) { 396 if_printf(ifp, "only 1 vap supported\n"); 397 return NULL; 398 } 399 if (opmode == IEEE80211_M_STA) 400 flags |= IEEE80211_CLONE_NOBEACONS; 401 break; 402 case IEEE80211_M_WDS: 403 if (TAILQ_EMPTY(&ic->ic_vaps) || 404 ic->ic_opmode != IEEE80211_M_HOSTAP) { 405 if_printf(ifp, "wds only supported in ap mode\n"); 406 return NULL; 407 } 408 /* 409 * Silently remove any request for a unique 410 * bssid; WDS vap's always share the local 411 * mac address. 412 */ 413 flags &= ~IEEE80211_CLONE_BSSID; 414 break; 415 default: 416 if_printf(ifp, "unknown opmode %d\n", opmode); 417 return NULL; 418 } 419 rvp = (struct rt2560_vap *) kmalloc(sizeof(struct rt2560_vap), 420 M_80211_VAP, M_INTWAIT | M_ZERO); 421 if (rvp == NULL) 422 return NULL; 423 vap = &rvp->ral_vap; 424 ieee80211_vap_setup(ic, vap, name, unit, opmode, flags, bssid, mac); 425 426 /* override state transition machine */ 427 rvp->ral_newstate = vap->iv_newstate; 428 vap->iv_newstate = rt2560_newstate; 429 vap->iv_update_beacon = rt2560_beacon_update; 430 431 ieee80211_ratectl_init(vap); 432 /* complete setup */ 433 ieee80211_vap_attach(vap, ieee80211_media_change, ieee80211_media_status); 434 if (TAILQ_FIRST(&ic->ic_vaps) == vap) 435 ic->ic_opmode = opmode; 436 return vap; 437 } 438 439 static void 440 rt2560_vap_delete(struct ieee80211vap *vap) 441 { 442 struct rt2560_vap *rvp = RT2560_VAP(vap); 443 444 ieee80211_ratectl_deinit(vap); 445 ieee80211_vap_detach(vap); 446 kfree(rvp, M_80211_VAP); 447 } 448 449 void 450 rt2560_resume(void *xsc) 451 { 452 struct rt2560_softc *sc = xsc; 453 struct ifnet *ifp = sc->sc_ifp; 454 455 if (ifp->if_flags & IFF_UP) 456 rt2560_init(sc); 457 } 458 459 static void 460 rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error) 461 { 462 if (error != 0) 463 return; 464 465 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg)); 466 467 *(bus_addr_t *)arg = segs[0].ds_addr; 468 } 469 470 static int 471 rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring, 472 int count) 473 { 474 int i, error; 475 476 ring->count = count; 477 ring->queued = 0; 478 ring->cur = ring->next = 0; 479 ring->cur_encrypt = ring->next_encrypt = 0; 480 481 error = bus_dma_tag_create(ring->desc_dmat, 4, 0, 482 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 483 count * RT2560_TX_DESC_SIZE, 1, count * RT2560_TX_DESC_SIZE, 484 0, &ring->desc_dmat); 485 if (error != 0) { 486 device_printf(sc->sc_dev, "could not create desc DMA tag\n"); 487 goto fail; 488 } 489 490 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc, 491 BUS_DMA_NOWAIT | BUS_DMA_ZERO, &ring->desc_map); 492 if (error != 0) { 493 device_printf(sc->sc_dev, "could not allocate DMA memory\n"); 494 goto fail; 495 } 496 497 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc, 498 count * RT2560_TX_DESC_SIZE, rt2560_dma_map_addr, &ring->physaddr, 499 0); 500 if (error != 0) { 501 device_printf(sc->sc_dev, "could not load desc DMA map\n"); 502 goto fail; 503 } 504 505 ring->data = kmalloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF, 506 M_INTWAIT | M_ZERO); 507 if (ring->data == NULL) { 508 device_printf(sc->sc_dev, "could not allocate soft data\n"); 509 error = ENOMEM; 510 goto fail; 511 } 512 513 error = bus_dma_tag_create(ring->data_dmat, 1, 0, 514 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, 515 MCLBYTES, RT2560_MAX_SCATTER, MCLBYTES, 0, &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 kfree(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(ring->desc_dmat, 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, &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 = kmalloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF, 649 M_INTWAIT | 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(ring->data_dmat, 1, 0, 660 BUS_SPACE_MAXADDR_32BIT, BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 661 1, MCLBYTES, 0, &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(MB_DONTWAIT, 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 kfree(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); 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 kprintf("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 ni = NULL; 1005 1006 /* descriptor is no longer valid */ 1007 desc->flags &= ~htole32(RT2560_TX_VALID); 1008 1009 DPRINTFN(sc, 15, "tx done idx=%u\n", sc->txq.next); 1010 1011 sc->txq.queued--; 1012 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT; 1013 } 1014 1015 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 1016 BUS_DMASYNC_PREWRITE); 1017 1018 if (sc->prioq.queued == 0 && sc->txq.queued == 0) 1019 sc->sc_tx_timer = 0; 1020 1021 if (sc->txq.queued < RT2560_TX_RING_COUNT - 1) { 1022 sc->sc_flags &= ~RT2560_F_DATA_OACTIVE; 1023 if ((sc->sc_flags & 1024 (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0) 1025 ifp->if_flags &= ~IFF_OACTIVE; 1026 rt2560_start_locked(ifp); 1027 } 1028 } 1029 1030 static void 1031 rt2560_prio_intr(struct rt2560_softc *sc) 1032 { 1033 struct ifnet *ifp = sc->sc_ifp; 1034 struct rt2560_tx_desc *desc; 1035 struct rt2560_tx_data *data; 1036 struct ieee80211_node *ni; 1037 struct mbuf *m; 1038 int flags; 1039 1040 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1041 BUS_DMASYNC_POSTREAD); 1042 1043 for (;;) { 1044 desc = &sc->prioq.desc[sc->prioq.next]; 1045 data = &sc->prioq.data[sc->prioq.next]; 1046 1047 flags = le32toh(desc->flags); 1048 if ((flags & RT2560_TX_BUSY) || (flags & RT2560_TX_VALID) == 0) 1049 break; 1050 1051 switch (flags & RT2560_TX_RESULT_MASK) { 1052 case RT2560_TX_SUCCESS: 1053 DPRINTFN(sc, 10, "%s\n", "mgt frame sent successfully"); 1054 break; 1055 1056 case RT2560_TX_SUCCESS_RETRY: 1057 DPRINTFN(sc, 9, "mgt frame sent after %u retries\n", 1058 (flags >> 5) & 0x7); 1059 break; 1060 1061 case RT2560_TX_FAIL_RETRY: 1062 DPRINTFN(sc, 9, "%s\n", 1063 "sending mgt frame failed (too much retries)"); 1064 break; 1065 1066 case RT2560_TX_FAIL_INVALID: 1067 case RT2560_TX_FAIL_OTHER: 1068 default: 1069 device_printf(sc->sc_dev, "sending mgt frame failed " 1070 "0x%08x\n", flags); 1071 break; 1072 } 1073 1074 bus_dmamap_sync(sc->prioq.data_dmat, data->map, 1075 BUS_DMASYNC_POSTWRITE); 1076 bus_dmamap_unload(sc->prioq.data_dmat, data->map); 1077 1078 m = data->m; 1079 data->m = NULL; 1080 ni = data->ni; 1081 data->ni = NULL; 1082 1083 /* descriptor is no longer valid */ 1084 desc->flags &= ~htole32(RT2560_TX_VALID); 1085 1086 DPRINTFN(sc, 15, "prio done idx=%u\n", sc->prioq.next); 1087 1088 sc->prioq.queued--; 1089 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT; 1090 1091 if (m->m_flags & M_TXCB) 1092 ieee80211_process_callback(ni, m, 1093 (flags & RT2560_TX_RESULT_MASK) &~ 1094 (RT2560_TX_SUCCESS | RT2560_TX_SUCCESS_RETRY)); 1095 m_freem(m); 1096 ieee80211_free_node(ni); 1097 } 1098 1099 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1100 BUS_DMASYNC_PREWRITE); 1101 1102 if (sc->prioq.queued == 0 && sc->txq.queued == 0) 1103 sc->sc_tx_timer = 0; 1104 1105 if (sc->prioq.queued < RT2560_PRIO_RING_COUNT) { 1106 sc->sc_flags &= ~RT2560_F_PRIO_OACTIVE; 1107 if ((sc->sc_flags & 1108 (RT2560_F_DATA_OACTIVE | RT2560_F_PRIO_OACTIVE)) == 0) 1109 ifp->if_flags &= ~IFF_OACTIVE; 1110 rt2560_start_locked(ifp); 1111 } 1112 } 1113 1114 /* 1115 * Some frames were processed by the hardware cipher engine and are ready for 1116 * handoff to the IEEE802.11 layer. 1117 */ 1118 static void 1119 rt2560_decryption_intr(struct rt2560_softc *sc) 1120 { 1121 struct ifnet *ifp = sc->sc_ifp; 1122 struct ieee80211com *ic = ifp->if_l2com; 1123 struct rt2560_rx_desc *desc; 1124 struct rt2560_rx_data *data; 1125 bus_addr_t physaddr; 1126 struct ieee80211_frame *wh; 1127 struct ieee80211_node *ni; 1128 struct mbuf *mnew, *m; 1129 int hw, error; 1130 int8_t rssi, nf; 1131 1132 /* retrieve last decriptor index processed by cipher engine */ 1133 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr; 1134 hw /= RT2560_RX_DESC_SIZE; 1135 1136 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map, 1137 BUS_DMASYNC_POSTREAD); 1138 1139 for (; sc->rxq.cur_decrypt != hw;) { 1140 desc = &sc->rxq.desc[sc->rxq.cur_decrypt]; 1141 data = &sc->rxq.data[sc->rxq.cur_decrypt]; 1142 1143 if ((le32toh(desc->flags) & RT2560_RX_BUSY) || 1144 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY)) 1145 break; 1146 1147 if (data->drop) { 1148 ifp->if_ierrors++; 1149 goto skip; 1150 } 1151 1152 if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 && 1153 (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) { 1154 ifp->if_ierrors++; 1155 goto skip; 1156 } 1157 1158 /* 1159 * Try to allocate a new mbuf for this ring element and load it 1160 * before processing the current mbuf. If the ring element 1161 * cannot be loaded, drop the received packet and reuse the old 1162 * mbuf. In the unlikely case that the old mbuf can't be 1163 * reloaded either, explicitly panic. 1164 */ 1165 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR); 1166 if (mnew == NULL) { 1167 ifp->if_ierrors++; 1168 goto skip; 1169 } 1170 1171 bus_dmamap_sync(sc->rxq.data_dmat, data->map, 1172 BUS_DMASYNC_POSTREAD); 1173 bus_dmamap_unload(sc->rxq.data_dmat, data->map); 1174 1175 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1176 mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr, 1177 &physaddr, 0); 1178 if (error != 0) { 1179 m_freem(mnew); 1180 1181 /* try to reload the old mbuf */ 1182 error = bus_dmamap_load(sc->rxq.data_dmat, data->map, 1183 mtod(data->m, void *), MCLBYTES, 1184 rt2560_dma_map_addr, &physaddr, 0); 1185 if (error != 0) { 1186 /* very unlikely that it will fail... */ 1187 panic("%s: could not load old rx mbuf", 1188 device_get_name(sc->sc_dev)); 1189 } 1190 ifp->if_ierrors++; 1191 goto skip; 1192 } 1193 1194 /* 1195 * New mbuf successfully loaded, update Rx ring and continue 1196 * processing. 1197 */ 1198 m = data->m; 1199 data->m = mnew; 1200 desc->physaddr = htole32(physaddr); 1201 1202 /* finalize mbuf */ 1203 m->m_pkthdr.rcvif = ifp; 1204 m->m_pkthdr.len = m->m_len = 1205 (le32toh(desc->flags) >> 16) & 0xfff; 1206 1207 rssi = RT2560_RSSI(sc, desc->rssi); 1208 nf = RT2560_NOISE_FLOOR; 1209 if (ieee80211_radiotap_active(ic)) { 1210 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap; 1211 uint32_t tsf_lo, tsf_hi; 1212 1213 /* get timestamp (low and high 32 bits) */ 1214 tsf_hi = RAL_READ(sc, RT2560_CSR17); 1215 tsf_lo = RAL_READ(sc, RT2560_CSR16); 1216 1217 tap->wr_tsf = 1218 htole64(((uint64_t)tsf_hi << 32) | tsf_lo); 1219 tap->wr_flags = 0; 1220 tap->wr_rate = ieee80211_plcp2rate(desc->rate, 1221 (desc->flags & htole32(RT2560_RX_OFDM)) ? 1222 IEEE80211_T_OFDM : IEEE80211_T_CCK); 1223 tap->wr_antenna = sc->rx_ant; 1224 tap->wr_antsignal = nf + rssi; 1225 tap->wr_antnoise = nf; 1226 } 1227 1228 sc->sc_flags |= RT2560_F_INPUT_RUNNING; 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 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 /* disable interrupts */ 1367 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff); 1368 1369 /* don't re-enable interrupts if we're shutting down */ 1370 if (!(ifp->if_flags & IFF_RUNNING)) { 1371 return; 1372 } 1373 1374 r = RAL_READ(sc, RT2560_CSR7); 1375 RAL_WRITE(sc, RT2560_CSR7, r); 1376 1377 if (r & RT2560_BEACON_EXPIRE) 1378 rt2560_beacon_expire(sc); 1379 1380 if (r & RT2560_WAKEUP_EXPIRE) 1381 rt2560_wakeup_expire(sc); 1382 1383 if (r & RT2560_ENCRYPTION_DONE) 1384 rt2560_encryption_intr(sc); 1385 1386 if (r & RT2560_TX_DONE) 1387 rt2560_tx_intr(sc); 1388 1389 if (r & RT2560_PRIO_DONE) 1390 rt2560_prio_intr(sc); 1391 1392 if (r & RT2560_DECRYPTION_DONE) 1393 rt2560_decryption_intr(sc); 1394 1395 if (r & RT2560_RX_DONE) { 1396 rt2560_rx_intr(sc); 1397 rt2560_encryption_intr(sc); 1398 } 1399 1400 /* re-enable interrupts */ 1401 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK); 1402 } 1403 1404 #define RAL_SIFS 10 /* us */ 1405 1406 #define RT2560_TXRX_TURNAROUND 10 /* us */ 1407 1408 static uint8_t 1409 rt2560_plcp_signal(int rate) 1410 { 1411 switch (rate) { 1412 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */ 1413 case 12: return 0xb; 1414 case 18: return 0xf; 1415 case 24: return 0xa; 1416 case 36: return 0xe; 1417 case 48: return 0x9; 1418 case 72: return 0xd; 1419 case 96: return 0x8; 1420 case 108: return 0xc; 1421 1422 /* CCK rates (NB: not IEEE std, device-specific) */ 1423 case 2: return 0x0; 1424 case 4: return 0x1; 1425 case 11: return 0x2; 1426 case 22: return 0x3; 1427 } 1428 return 0xff; /* XXX unsupported/unknown rate */ 1429 } 1430 1431 static void 1432 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc, 1433 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr) 1434 { 1435 struct ifnet *ifp = sc->sc_ifp; 1436 struct ieee80211com *ic = ifp->if_l2com; 1437 uint16_t plcp_length; 1438 int remainder; 1439 1440 desc->flags = htole32(flags); 1441 desc->flags |= htole32(len << 16); 1442 1443 desc->physaddr = htole32(physaddr); 1444 desc->wme = htole16( 1445 RT2560_AIFSN(2) | 1446 RT2560_LOGCWMIN(3) | 1447 RT2560_LOGCWMAX(8)); 1448 1449 /* setup PLCP fields */ 1450 desc->plcp_signal = rt2560_plcp_signal(rate); 1451 desc->plcp_service = 4; 1452 1453 len += IEEE80211_CRC_LEN; 1454 if (ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) { 1455 desc->flags |= htole32(RT2560_TX_OFDM); 1456 1457 plcp_length = len & 0xfff; 1458 desc->plcp_length_hi = plcp_length >> 6; 1459 desc->plcp_length_lo = plcp_length & 0x3f; 1460 } else { 1461 plcp_length = (16 * len + rate - 1) / rate; 1462 if (rate == 22) { 1463 remainder = (16 * len) % 22; 1464 if (remainder != 0 && remainder < 7) 1465 desc->plcp_service |= RT2560_PLCP_LENGEXT; 1466 } 1467 desc->plcp_length_hi = plcp_length >> 8; 1468 desc->plcp_length_lo = plcp_length & 0xff; 1469 1470 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE)) 1471 desc->plcp_signal |= 0x08; 1472 } 1473 1474 if (!encrypt) 1475 desc->flags |= htole32(RT2560_TX_VALID); 1476 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY) 1477 : htole32(RT2560_TX_BUSY); 1478 } 1479 1480 static int 1481 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0, 1482 struct ieee80211_node *ni) 1483 { 1484 struct ieee80211vap *vap = ni->ni_vap; 1485 struct rt2560_tx_desc *desc; 1486 struct rt2560_tx_data *data; 1487 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1488 int nsegs, rate, error; 1489 1490 desc = &sc->bcnq.desc[sc->bcnq.cur]; 1491 data = &sc->bcnq.data[sc->bcnq.cur]; 1492 1493 /* XXX maybe a separate beacon rate? */ 1494 rate = vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)].mgmtrate; 1495 1496 error = bus_dmamap_load_mbuf_segment(sc->bcnq.data_dmat, data->map, m0, 1497 segs, 1, &nsegs, BUS_DMA_NOWAIT); 1498 if (error != 0) { 1499 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1500 error); 1501 m_freem(m0); 1502 return error; 1503 } 1504 1505 if (ieee80211_radiotap_active_vap(vap)) { 1506 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1507 1508 tap->wt_flags = 0; 1509 tap->wt_rate = rate; 1510 tap->wt_antenna = sc->tx_ant; 1511 1512 ieee80211_radiotap_tx(vap, m0); 1513 } 1514 1515 data->m = m0; 1516 data->ni = ni; 1517 1518 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF | 1519 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, segs->ds_addr); 1520 1521 DPRINTFN(sc, 10, "sending beacon frame len=%u idx=%u rate=%u\n", 1522 m0->m_pkthdr.len, sc->bcnq.cur, rate); 1523 1524 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1525 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map, 1526 BUS_DMASYNC_PREWRITE); 1527 1528 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT; 1529 1530 return 0; 1531 } 1532 1533 static int 1534 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0, 1535 struct ieee80211_node *ni) 1536 { 1537 struct ieee80211vap *vap = ni->ni_vap; 1538 struct ieee80211com *ic = ni->ni_ic; 1539 struct rt2560_tx_desc *desc; 1540 struct rt2560_tx_data *data; 1541 struct ieee80211_frame *wh; 1542 struct ieee80211_key *k; 1543 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1544 uint16_t dur; 1545 uint32_t flags = 0; 1546 int nsegs, rate, error; 1547 1548 desc = &sc->prioq.desc[sc->prioq.cur]; 1549 data = &sc->prioq.data[sc->prioq.cur]; 1550 1551 rate = vap->iv_txparms[ieee80211_chan2mode(ic->ic_curchan)].mgmtrate; 1552 1553 wh = mtod(m0, struct ieee80211_frame *); 1554 1555 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1556 k = ieee80211_crypto_encap(ni, m0); 1557 if (k == NULL) { 1558 m_freem(m0); 1559 return ENOBUFS; 1560 } 1561 } 1562 1563 error = bus_dmamap_load_mbuf_segment(sc->prioq.data_dmat, data->map, m0, 1564 segs, 1, &nsegs, 0); 1565 if (error != 0) { 1566 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1567 error); 1568 m_freem(m0); 1569 return error; 1570 } 1571 1572 if (ieee80211_radiotap_active_vap(vap)) { 1573 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1574 1575 tap->wt_flags = 0; 1576 tap->wt_rate = rate; 1577 tap->wt_antenna = sc->tx_ant; 1578 1579 ieee80211_radiotap_tx(vap, m0); 1580 } 1581 1582 data->m = m0; 1583 data->ni = ni; 1584 /* management frames are not taken into account for amrr */ 1585 data->rix = IEEE80211_FIXED_RATE_NONE; 1586 1587 wh = mtod(m0, struct ieee80211_frame *); 1588 1589 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1590 flags |= RT2560_TX_ACK; 1591 1592 dur = ieee80211_ack_duration(ic->ic_rt, 1593 rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1594 *(uint16_t *)wh->i_dur = htole16(dur); 1595 1596 /* tell hardware to add timestamp for probe responses */ 1597 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) == 1598 IEEE80211_FC0_TYPE_MGT && 1599 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) == 1600 IEEE80211_FC0_SUBTYPE_PROBE_RESP) 1601 flags |= RT2560_TX_TIMESTAMP; 1602 } 1603 1604 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0, 1605 segs->ds_addr); 1606 1607 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1608 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1609 BUS_DMASYNC_PREWRITE); 1610 1611 DPRINTFN(sc, 10, "sending mgt frame len=%u idx=%u rate=%u\n", 1612 m0->m_pkthdr.len, sc->prioq.cur, rate); 1613 1614 /* kick prio */ 1615 sc->prioq.queued++; 1616 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT; 1617 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO); 1618 1619 return 0; 1620 } 1621 1622 static int 1623 rt2560_sendprot(struct rt2560_softc *sc, 1624 const struct mbuf *m, struct ieee80211_node *ni, int prot, int rate) 1625 { 1626 struct ieee80211com *ic = ni->ni_ic; 1627 const struct ieee80211_frame *wh; 1628 struct rt2560_tx_desc *desc; 1629 struct rt2560_tx_data *data; 1630 struct mbuf *mprot; 1631 int protrate, ackrate, pktlen, flags, isshort, error; 1632 uint16_t dur; 1633 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1634 int nsegs; 1635 1636 KASSERT(prot == IEEE80211_PROT_RTSCTS || prot == IEEE80211_PROT_CTSONLY, 1637 ("protection %d", prot)); 1638 1639 wh = mtod(m, const struct ieee80211_frame *); 1640 pktlen = m->m_pkthdr.len + IEEE80211_CRC_LEN; 1641 1642 protrate = ieee80211_ctl_rate(ic->ic_rt, rate); 1643 ackrate = ieee80211_ack_rate(ic->ic_rt, rate); 1644 1645 isshort = (ic->ic_flags & IEEE80211_F_SHPREAMBLE) != 0; 1646 dur = ieee80211_compute_duration(ic->ic_rt, pktlen, rate, isshort) 1647 + ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1648 flags = RT2560_TX_MORE_FRAG; 1649 if (prot == IEEE80211_PROT_RTSCTS) { 1650 /* NB: CTS is the same size as an ACK */ 1651 dur += ieee80211_ack_duration(ic->ic_rt, rate, isshort); 1652 flags |= RT2560_TX_ACK; 1653 mprot = ieee80211_alloc_rts(ic, wh->i_addr1, wh->i_addr2, dur); 1654 } else { 1655 mprot = ieee80211_alloc_cts(ic, ni->ni_vap->iv_myaddr, dur); 1656 } 1657 if (mprot == NULL) { 1658 /* XXX stat + msg */ 1659 return ENOBUFS; 1660 } 1661 1662 desc = &sc->txq.desc[sc->txq.cur_encrypt]; 1663 data = &sc->txq.data[sc->txq.cur_encrypt]; 1664 1665 error = bus_dmamap_load_mbuf_segment(sc->txq.data_dmat, data->map, 1666 mprot, segs, 1, &nsegs, 0); 1667 if (error != 0) { 1668 device_printf(sc->sc_dev, 1669 "could not map mbuf (error %d)\n", error); 1670 m_freem(mprot); 1671 return error; 1672 } 1673 1674 data->m = mprot; 1675 data->ni = ieee80211_ref_node(ni); 1676 /* ctl frames are not taken into account for amrr */ 1677 data->rix = IEEE80211_FIXED_RATE_NONE; 1678 1679 rt2560_setup_tx_desc(sc, desc, flags, mprot->m_pkthdr.len, protrate, 1, 1680 segs->ds_addr); 1681 1682 bus_dmamap_sync(sc->txq.data_dmat, data->map, 1683 BUS_DMASYNC_PREWRITE); 1684 1685 sc->txq.queued++; 1686 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT; 1687 1688 return 0; 1689 } 1690 1691 static int 1692 rt2560_tx_raw(struct rt2560_softc *sc, struct mbuf *m0, 1693 struct ieee80211_node *ni, const struct ieee80211_bpf_params *params) 1694 { 1695 struct ieee80211vap *vap = ni->ni_vap; 1696 struct ieee80211com *ic = ni->ni_ic; 1697 struct rt2560_tx_desc *desc; 1698 struct rt2560_tx_data *data; 1699 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1700 uint32_t flags; 1701 int nsegs, rate, error; 1702 1703 desc = &sc->prioq.desc[sc->prioq.cur]; 1704 data = &sc->prioq.data[sc->prioq.cur]; 1705 1706 rate = params->ibp_rate0; 1707 if (!ieee80211_isratevalid(ic->ic_rt, rate)) { 1708 /* XXX fall back to mcast/mgmt rate? */ 1709 m_freem(m0); 1710 return EINVAL; 1711 } 1712 1713 flags = 0; 1714 if ((params->ibp_flags & IEEE80211_BPF_NOACK) == 0) 1715 flags |= RT2560_TX_ACK; 1716 if (params->ibp_flags & (IEEE80211_BPF_RTS|IEEE80211_BPF_CTS)) { 1717 error = rt2560_sendprot(sc, m0, ni, 1718 params->ibp_flags & IEEE80211_BPF_RTS ? 1719 IEEE80211_PROT_RTSCTS : IEEE80211_PROT_CTSONLY, 1720 rate); 1721 if (error) { 1722 m_freem(m0); 1723 return error; 1724 } 1725 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS; 1726 } 1727 1728 error = bus_dmamap_load_mbuf_segment(sc->prioq.data_dmat, data->map, m0, 1729 segs, 1, &nsegs, 0); 1730 if (error != 0) { 1731 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1732 error); 1733 m_freem(m0); 1734 return error; 1735 } 1736 1737 if (ieee80211_radiotap_active_vap(vap)) { 1738 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1739 1740 tap->wt_flags = 0; 1741 tap->wt_rate = rate; 1742 tap->wt_antenna = sc->tx_ant; 1743 1744 ieee80211_radiotap_tx(ni->ni_vap, m0); 1745 } 1746 1747 data->m = m0; 1748 data->ni = ni; 1749 1750 /* XXX need to setup descriptor ourself */ 1751 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, 1752 rate, (params->ibp_flags & IEEE80211_BPF_CRYPTO) != 0, 1753 segs->ds_addr); 1754 1755 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1756 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map, 1757 BUS_DMASYNC_PREWRITE); 1758 1759 DPRINTFN(sc, 10, "sending raw frame len=%u idx=%u rate=%u\n", 1760 m0->m_pkthdr.len, sc->prioq.cur, rate); 1761 1762 /* kick prio */ 1763 sc->prioq.queued++; 1764 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT; 1765 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO); 1766 1767 return 0; 1768 } 1769 1770 static int 1771 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0, 1772 struct ieee80211_node *ni) 1773 { 1774 struct ieee80211vap *vap = ni->ni_vap; 1775 struct ieee80211com *ic = ni->ni_ic; 1776 struct rt2560_tx_desc *desc; 1777 struct rt2560_tx_data *data; 1778 struct ieee80211_frame *wh; 1779 const struct ieee80211_txparam *tp; 1780 struct ieee80211_key *k; 1781 struct mbuf *mnew; 1782 bus_dma_segment_t segs[RT2560_MAX_SCATTER]; 1783 uint16_t dur; 1784 uint32_t flags; 1785 int nsegs, rate, error; 1786 1787 wh = mtod(m0, struct ieee80211_frame *); 1788 1789 tp = &vap->iv_txparms[ieee80211_chan2mode(ni->ni_chan)]; 1790 if (IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1791 rate = tp->mcastrate; 1792 } else if (m0->m_flags & M_EAPOL) { 1793 rate = tp->mgmtrate; 1794 } else if (tp->ucastrate != IEEE80211_FIXED_RATE_NONE) { 1795 rate = tp->ucastrate; 1796 } else { 1797 (void) ieee80211_ratectl_rate(ni, NULL, 0); 1798 rate = ni->ni_txrate; 1799 } 1800 1801 if (wh->i_fc[1] & IEEE80211_FC1_WEP) { 1802 k = ieee80211_crypto_encap(ni, m0); 1803 if (k == NULL) { 1804 m_freem(m0); 1805 return ENOBUFS; 1806 } 1807 1808 /* packet header may have moved, reset our local pointer */ 1809 wh = mtod(m0, struct ieee80211_frame *); 1810 } 1811 1812 flags = 0; 1813 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1814 int prot = IEEE80211_PROT_NONE; 1815 if (m0->m_pkthdr.len + IEEE80211_CRC_LEN > vap->iv_rtsthreshold) 1816 prot = IEEE80211_PROT_RTSCTS; 1817 else if ((ic->ic_flags & IEEE80211_F_USEPROT) && 1818 ieee80211_rate2phytype(ic->ic_rt, rate) == IEEE80211_T_OFDM) 1819 prot = ic->ic_protmode; 1820 if (prot != IEEE80211_PROT_NONE) { 1821 error = rt2560_sendprot(sc, m0, ni, prot, rate); 1822 if (error) { 1823 m_freem(m0); 1824 return error; 1825 } 1826 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS; 1827 } 1828 } 1829 1830 data = &sc->txq.data[sc->txq.cur_encrypt]; 1831 desc = &sc->txq.desc[sc->txq.cur_encrypt]; 1832 1833 error = bus_dmamap_load_mbuf_segment(sc->txq.data_dmat, data->map, m0, 1834 segs, 1, &nsegs, 0); 1835 if (error != 0 && error != EFBIG) { 1836 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n", 1837 error); 1838 m_freem(m0); 1839 return error; 1840 } 1841 if (error != 0) { 1842 mnew = m_defrag(m0, MB_DONTWAIT); 1843 if (mnew == NULL) { 1844 device_printf(sc->sc_dev, 1845 "could not defragment mbuf\n"); 1846 m_freem(m0); 1847 return ENOBUFS; 1848 } 1849 m0 = mnew; 1850 1851 error = bus_dmamap_load_mbuf_segment(sc->txq.data_dmat, data->map, 1852 m0, segs, 1, &nsegs, 0); 1853 if (error != 0) { 1854 device_printf(sc->sc_dev, 1855 "could not map mbuf (error %d)\n", error); 1856 m_freem(m0); 1857 return error; 1858 } 1859 1860 /* packet header may have moved, reset our local pointer */ 1861 wh = mtod(m0, struct ieee80211_frame *); 1862 } 1863 1864 if (ieee80211_radiotap_active_vap(vap)) { 1865 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap; 1866 1867 tap->wt_flags = 0; 1868 tap->wt_rate = rate; 1869 tap->wt_antenna = sc->tx_ant; 1870 1871 ieee80211_radiotap_tx(vap, m0); 1872 } 1873 1874 data->m = m0; 1875 data->ni = ni; 1876 1877 /* remember link conditions for rate adaptation algorithm */ 1878 if (tp->ucastrate == IEEE80211_FIXED_RATE_NONE) { 1879 data->rix = ni->ni_txrate; 1880 /* XXX probably need last rssi value and not avg */ 1881 data->rssi = ic->ic_node_getrssi(ni); 1882 } else 1883 data->rix = IEEE80211_FIXED_RATE_NONE; 1884 1885 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) { 1886 flags |= RT2560_TX_ACK; 1887 1888 dur = ieee80211_ack_duration(ic->ic_rt, 1889 rate, ic->ic_flags & IEEE80211_F_SHPREAMBLE); 1890 *(uint16_t *)wh->i_dur = htole16(dur); 1891 } 1892 1893 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1, 1894 segs->ds_addr); 1895 1896 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE); 1897 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map, 1898 BUS_DMASYNC_PREWRITE); 1899 1900 DPRINTFN(sc, 10, "sending data frame len=%u idx=%u rate=%u\n", 1901 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate); 1902 1903 /* kick encrypt */ 1904 sc->txq.queued++; 1905 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT; 1906 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT); 1907 1908 return 0; 1909 } 1910 1911 static void 1912 rt2560_start_locked(struct ifnet *ifp) 1913 { 1914 struct rt2560_softc *sc = ifp->if_softc; 1915 struct mbuf *m; 1916 struct ieee80211_node *ni; 1917 1918 for (;;) { 1919 IF_DEQUEUE(&ifp->if_snd, m); 1920 if (m == NULL) 1921 break; 1922 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) { 1923 IF_PREPEND(&ifp->if_snd, m); 1924 ifp->if_flags |= IFF_OACTIVE; 1925 sc->sc_flags |= RT2560_F_DATA_OACTIVE; 1926 break; 1927 } 1928 ni = (struct ieee80211_node *) m->m_pkthdr.rcvif; 1929 if (rt2560_tx_data(sc, m, ni) != 0) { 1930 ieee80211_free_node(ni); 1931 ifp->if_oerrors++; 1932 break; 1933 } 1934 1935 sc->sc_tx_timer = 5; 1936 } 1937 } 1938 1939 static void 1940 rt2560_start(struct ifnet *ifp) 1941 { 1942 rt2560_start_locked(ifp); 1943 } 1944 1945 static void 1946 rt2560_watchdog_callout(void *arg) 1947 { 1948 struct rt2560_softc *sc = arg; 1949 struct ifnet *ifp = sc->sc_ifp; 1950 1951 KASSERT(ifp->if_flags & IFF_RUNNING, ("not running")); 1952 1953 if (sc->sc_invalid) /* card ejected */ 1954 return; 1955 1956 rt2560_encryption_intr(sc); 1957 rt2560_tx_intr(sc); 1958 1959 if (sc->sc_tx_timer > 0 && --sc->sc_tx_timer == 0) { 1960 if_printf(ifp, "device timeout\n"); 1961 rt2560_init_locked(sc); 1962 ifp->if_oerrors++; 1963 /* NB: callout is reset in rt2560_init() */ 1964 return; 1965 } 1966 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog_callout, sc); 1967 } 1968 1969 static int 1970 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *ucred) 1971 { 1972 struct rt2560_softc *sc = ifp->if_softc; 1973 struct ieee80211com *ic = ifp->if_l2com; 1974 struct ifreq *ifr = (struct ifreq *) data; 1975 int error = 0, startall = 0; 1976 1977 switch (cmd) { 1978 case SIOCSIFFLAGS: 1979 if (ifp->if_flags & IFF_UP) { 1980 if ((ifp->if_flags & IFF_RUNNING) == 0) { 1981 rt2560_init_locked(sc); 1982 startall = 1; 1983 } else 1984 rt2560_update_promisc(ifp); 1985 } else { 1986 if (ifp->if_flags & IFF_RUNNING) 1987 rt2560_stop_locked(sc); 1988 } 1989 if (startall) 1990 ieee80211_start_all(ic); 1991 break; 1992 case SIOCGIFMEDIA: 1993 error = ifmedia_ioctl(ifp, ifr, &ic->ic_media, cmd); 1994 break; 1995 case SIOCGIFADDR: 1996 error = ether_ioctl(ifp, cmd, data); 1997 break; 1998 default: 1999 error = EINVAL; 2000 break; 2001 } 2002 return error; 2003 } 2004 2005 static void 2006 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val) 2007 { 2008 uint32_t tmp; 2009 int ntries; 2010 2011 for (ntries = 0; ntries < 100; ntries++) { 2012 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY)) 2013 break; 2014 DELAY(1); 2015 } 2016 if (ntries == 100) { 2017 device_printf(sc->sc_dev, "could not write to BBP\n"); 2018 return; 2019 } 2020 2021 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val; 2022 RAL_WRITE(sc, RT2560_BBPCSR, tmp); 2023 2024 DPRINTFN(sc, 15, "BBP R%u <- 0x%02x\n", reg, val); 2025 } 2026 2027 static uint8_t 2028 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg) 2029 { 2030 uint32_t val; 2031 int ntries; 2032 2033 for (ntries = 0; ntries < 100; ntries++) { 2034 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY)) 2035 break; 2036 DELAY(1); 2037 } 2038 if (ntries == 100) { 2039 device_printf(sc->sc_dev, "could not read from BBP\n"); 2040 return 0; 2041 } 2042 2043 val = RT2560_BBP_BUSY | reg << 8; 2044 RAL_WRITE(sc, RT2560_BBPCSR, val); 2045 2046 for (ntries = 0; ntries < 100; ntries++) { 2047 val = RAL_READ(sc, RT2560_BBPCSR); 2048 if (!(val & RT2560_BBP_BUSY)) 2049 return val & 0xff; 2050 DELAY(1); 2051 } 2052 2053 device_printf(sc->sc_dev, "could not read from BBP\n"); 2054 return 0; 2055 } 2056 2057 static void 2058 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val) 2059 { 2060 uint32_t tmp; 2061 int ntries; 2062 2063 for (ntries = 0; ntries < 100; ntries++) { 2064 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY)) 2065 break; 2066 DELAY(1); 2067 } 2068 if (ntries == 100) { 2069 device_printf(sc->sc_dev, "could not write to RF\n"); 2070 return; 2071 } 2072 2073 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 | 2074 (reg & 0x3); 2075 RAL_WRITE(sc, RT2560_RFCSR, tmp); 2076 2077 /* remember last written value in sc */ 2078 sc->rf_regs[reg] = val; 2079 2080 DPRINTFN(sc, 15, "RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff); 2081 } 2082 2083 static void 2084 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c) 2085 { 2086 struct ifnet *ifp = sc->sc_ifp; 2087 struct ieee80211com *ic = ifp->if_l2com; 2088 uint8_t power, tmp; 2089 u_int i, chan; 2090 2091 chan = ieee80211_chan2ieee(ic, c); 2092 KASSERT(chan != 0 && chan != IEEE80211_CHAN_ANY, ("chan 0x%x", chan)); 2093 2094 if (IEEE80211_IS_CHAN_2GHZ(c)) 2095 power = min(sc->txpow[chan - 1], 31); 2096 else 2097 power = 31; 2098 2099 /* adjust txpower using ifconfig settings */ 2100 power -= (100 - ic->ic_txpowlimit) / 8; 2101 2102 DPRINTFN(sc, 2, "setting channel to %u, txpower to %u\n", chan, power); 2103 2104 switch (sc->rf_rev) { 2105 case RT2560_RF_2522: 2106 rt2560_rf_write(sc, RAL_RF1, 0x00814); 2107 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]); 2108 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2109 break; 2110 2111 case RT2560_RF_2523: 2112 rt2560_rf_write(sc, RAL_RF1, 0x08804); 2113 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]); 2114 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044); 2115 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2116 break; 2117 2118 case RT2560_RF_2524: 2119 rt2560_rf_write(sc, RAL_RF1, 0x0c808); 2120 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]); 2121 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2122 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2123 break; 2124 2125 case RT2560_RF_2525: 2126 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2127 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]); 2128 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2129 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2130 2131 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2132 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]); 2133 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2134 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286); 2135 break; 2136 2137 case RT2560_RF_2525E: 2138 rt2560_rf_write(sc, RAL_RF1, 0x08808); 2139 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]); 2140 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2141 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282); 2142 break; 2143 2144 case RT2560_RF_2526: 2145 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]); 2146 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 2147 rt2560_rf_write(sc, RAL_RF1, 0x08804); 2148 2149 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]); 2150 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044); 2151 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381); 2152 break; 2153 2154 /* dual-band RF */ 2155 case RT2560_RF_5222: 2156 for (i = 0; rt2560_rf5222[i].chan != chan; i++); 2157 2158 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1); 2159 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2); 2160 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040); 2161 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4); 2162 break; 2163 default: 2164 kprintf("unknown ral rev=%d\n", sc->rf_rev); 2165 } 2166 2167 /* XXX */ 2168 if ((ic->ic_flags & IEEE80211_F_SCAN) == 0) { 2169 /* set Japan filter bit for channel 14 */ 2170 tmp = rt2560_bbp_read(sc, 70); 2171 2172 tmp &= ~RT2560_JAPAN_FILTER; 2173 if (chan == 14) 2174 tmp |= RT2560_JAPAN_FILTER; 2175 2176 rt2560_bbp_write(sc, 70, tmp); 2177 2178 /* clear CRC errors */ 2179 RAL_READ(sc, RT2560_CNT0); 2180 } 2181 } 2182 2183 static void 2184 rt2560_set_channel(struct ieee80211com *ic) 2185 { 2186 struct ifnet *ifp = ic->ic_ifp; 2187 struct rt2560_softc *sc = ifp->if_softc; 2188 2189 rt2560_set_chan(sc, ic->ic_curchan); 2190 2191 } 2192 2193 #if 0 2194 /* 2195 * Disable RF auto-tuning. 2196 */ 2197 static void 2198 rt2560_disable_rf_tune(struct rt2560_softc *sc) 2199 { 2200 uint32_t tmp; 2201 2202 if (sc->rf_rev != RT2560_RF_2523) { 2203 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE; 2204 rt2560_rf_write(sc, RAL_RF1, tmp); 2205 } 2206 2207 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE; 2208 rt2560_rf_write(sc, RAL_RF3, tmp); 2209 2210 DPRINTFN(sc, 2, "%s", "disabling RF autotune\n"); 2211 } 2212 #endif 2213 2214 /* 2215 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF 2216 * synchronization. 2217 */ 2218 static void 2219 rt2560_enable_tsf_sync(struct rt2560_softc *sc) 2220 { 2221 struct ifnet *ifp = sc->sc_ifp; 2222 struct ieee80211com *ic = ifp->if_l2com; 2223 struct ieee80211vap *vap = TAILQ_FIRST(&ic->ic_vaps); 2224 uint16_t logcwmin, preload; 2225 uint32_t tmp; 2226 2227 /* first, disable TSF synchronization */ 2228 RAL_WRITE(sc, RT2560_CSR14, 0); 2229 2230 tmp = 16 * vap->iv_bss->ni_intval; 2231 RAL_WRITE(sc, RT2560_CSR12, tmp); 2232 2233 RAL_WRITE(sc, RT2560_CSR13, 0); 2234 2235 logcwmin = 5; 2236 preload = (vap->iv_opmode == IEEE80211_M_STA) ? 384 : 1024; 2237 tmp = logcwmin << 16 | preload; 2238 RAL_WRITE(sc, RT2560_BCNOCSR, tmp); 2239 2240 /* finally, enable TSF synchronization */ 2241 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN; 2242 if (ic->ic_opmode == IEEE80211_M_STA) 2243 tmp |= RT2560_ENABLE_TSF_SYNC(1); 2244 else 2245 tmp |= RT2560_ENABLE_TSF_SYNC(2) | 2246 RT2560_ENABLE_BEACON_GENERATOR; 2247 RAL_WRITE(sc, RT2560_CSR14, tmp); 2248 2249 DPRINTF(sc, "%s", "enabling TSF synchronization\n"); 2250 } 2251 2252 static void 2253 rt2560_enable_tsf(struct rt2560_softc *sc) 2254 { 2255 RAL_WRITE(sc, RT2560_CSR14, 0); 2256 RAL_WRITE(sc, RT2560_CSR14, 2257 RT2560_ENABLE_TSF_SYNC(2) | RT2560_ENABLE_TSF); 2258 } 2259 2260 static void 2261 rt2560_update_plcp(struct rt2560_softc *sc) 2262 { 2263 struct ifnet *ifp = sc->sc_ifp; 2264 struct ieee80211com *ic = ifp->if_l2com; 2265 2266 /* no short preamble for 1Mbps */ 2267 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400); 2268 2269 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) { 2270 /* values taken from the reference driver */ 2271 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401); 2272 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402); 2273 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403); 2274 } else { 2275 /* same values as above or'ed 0x8 */ 2276 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409); 2277 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a); 2278 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b); 2279 } 2280 2281 DPRINTF(sc, "updating PLCP for %s preamble\n", 2282 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"); 2283 } 2284 2285 /* 2286 * This function can be called by ieee80211_set_shortslottime(). Refer to 2287 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed. 2288 */ 2289 static void 2290 rt2560_update_slot(struct ifnet *ifp) 2291 { 2292 struct rt2560_softc *sc = ifp->if_softc; 2293 struct ieee80211com *ic = ifp->if_l2com; 2294 uint8_t slottime; 2295 uint16_t tx_sifs, tx_pifs, tx_difs, eifs; 2296 uint32_t tmp; 2297 2298 #ifndef FORCE_SLOTTIME 2299 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20; 2300 #else 2301 /* 2302 * Setting slot time according to "short slot time" capability 2303 * in beacon/probe_resp seems to cause problem to acknowledge 2304 * certain AP's data frames transimitted at CCK/DS rates: the 2305 * problematic AP keeps retransmitting data frames, probably 2306 * because MAC level acks are not received by hardware. 2307 * So we cheat a little bit here by claiming we are capable of 2308 * "short slot time" but setting hardware slot time to the normal 2309 * slot time. ral(4) does not seem to have trouble to receive 2310 * frames transmitted using short slot time even if hardware 2311 * slot time is set to normal slot time. If we didn't use this 2312 * trick, we would have to claim that short slot time is not 2313 * supported; this would give relative poor RX performance 2314 * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short 2315 * slot time. 2316 */ 2317 slottime = 20; 2318 #endif 2319 2320 /* update the MAC slot boundaries */ 2321 tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND; 2322 tx_pifs = tx_sifs + slottime; 2323 tx_difs = tx_sifs + 2 * slottime; 2324 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60; 2325 2326 tmp = RAL_READ(sc, RT2560_CSR11); 2327 tmp = (tmp & ~0x1f00) | slottime << 8; 2328 RAL_WRITE(sc, RT2560_CSR11, tmp); 2329 2330 tmp = tx_pifs << 16 | tx_sifs; 2331 RAL_WRITE(sc, RT2560_CSR18, tmp); 2332 2333 tmp = eifs << 16 | tx_difs; 2334 RAL_WRITE(sc, RT2560_CSR19, tmp); 2335 2336 DPRINTF(sc, "setting slottime to %uus\n", slottime); 2337 } 2338 2339 static void 2340 rt2560_set_basicrates(struct rt2560_softc *sc) 2341 { 2342 struct ifnet *ifp = sc->sc_ifp; 2343 struct ieee80211com *ic = ifp->if_l2com; 2344 2345 /* update basic rate set */ 2346 if (ic->ic_curmode == IEEE80211_MODE_11B) { 2347 /* 11b basic rates: 1, 2Mbps */ 2348 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3); 2349 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) { 2350 /* 11a basic rates: 6, 12, 24Mbps */ 2351 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150); 2352 } else { 2353 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */ 2354 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f); 2355 } 2356 } 2357 2358 static void 2359 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2) 2360 { 2361 uint32_t tmp; 2362 2363 /* set ON period to 70ms and OFF period to 30ms */ 2364 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30; 2365 RAL_WRITE(sc, RT2560_LEDCSR, tmp); 2366 } 2367 2368 static void 2369 rt2560_set_bssid(struct rt2560_softc *sc, const uint8_t *bssid) 2370 { 2371 uint32_t tmp; 2372 2373 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24; 2374 RAL_WRITE(sc, RT2560_CSR5, tmp); 2375 2376 tmp = bssid[4] | bssid[5] << 8; 2377 RAL_WRITE(sc, RT2560_CSR6, tmp); 2378 2379 DPRINTF(sc, "setting BSSID to %6D\n", bssid, ":"); 2380 } 2381 2382 static void 2383 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr) 2384 { 2385 uint32_t tmp; 2386 2387 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24; 2388 RAL_WRITE(sc, RT2560_CSR3, tmp); 2389 2390 tmp = addr[4] | addr[5] << 8; 2391 RAL_WRITE(sc, RT2560_CSR4, tmp); 2392 2393 DPRINTF(sc, "setting MAC address to %6D\n", addr, ":"); 2394 } 2395 2396 static void 2397 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr) 2398 { 2399 uint32_t tmp; 2400 2401 tmp = RAL_READ(sc, RT2560_CSR3); 2402 addr[0] = tmp & 0xff; 2403 addr[1] = (tmp >> 8) & 0xff; 2404 addr[2] = (tmp >> 16) & 0xff; 2405 addr[3] = (tmp >> 24); 2406 2407 tmp = RAL_READ(sc, RT2560_CSR4); 2408 addr[4] = tmp & 0xff; 2409 addr[5] = (tmp >> 8) & 0xff; 2410 } 2411 2412 static void 2413 rt2560_update_promisc(struct ifnet *ifp) 2414 { 2415 struct rt2560_softc *sc = ifp->if_softc; 2416 uint32_t tmp; 2417 2418 tmp = RAL_READ(sc, RT2560_RXCSR0); 2419 2420 tmp &= ~RT2560_DROP_NOT_TO_ME; 2421 if (!(ifp->if_flags & IFF_PROMISC)) 2422 tmp |= RT2560_DROP_NOT_TO_ME; 2423 2424 RAL_WRITE(sc, RT2560_RXCSR0, tmp); 2425 2426 DPRINTF(sc, "%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ? 2427 "entering" : "leaving"); 2428 } 2429 2430 static const char * 2431 rt2560_get_rf(int rev) 2432 { 2433 switch (rev) { 2434 case RT2560_RF_2522: return "RT2522"; 2435 case RT2560_RF_2523: return "RT2523"; 2436 case RT2560_RF_2524: return "RT2524"; 2437 case RT2560_RF_2525: return "RT2525"; 2438 case RT2560_RF_2525E: return "RT2525e"; 2439 case RT2560_RF_2526: return "RT2526"; 2440 case RT2560_RF_5222: return "RT5222"; 2441 default: return "unknown"; 2442 } 2443 } 2444 2445 static void 2446 rt2560_read_config(struct rt2560_softc *sc) 2447 { 2448 uint16_t val; 2449 int i; 2450 2451 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0); 2452 sc->rf_rev = (val >> 11) & 0x7; 2453 sc->hw_radio = (val >> 10) & 0x1; 2454 sc->led_mode = (val >> 6) & 0x7; 2455 sc->rx_ant = (val >> 4) & 0x3; 2456 sc->tx_ant = (val >> 2) & 0x3; 2457 sc->nb_ant = val & 0x3; 2458 2459 /* read default values for BBP registers */ 2460 for (i = 0; i < 16; i++) { 2461 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i); 2462 if (val == 0 || val == 0xffff) 2463 continue; 2464 2465 sc->bbp_prom[i].reg = val >> 8; 2466 sc->bbp_prom[i].val = val & 0xff; 2467 } 2468 2469 /* read Tx power for all b/g channels */ 2470 for (i = 0; i < 14 / 2; i++) { 2471 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i); 2472 sc->txpow[i * 2] = val & 0xff; 2473 sc->txpow[i * 2 + 1] = val >> 8; 2474 } 2475 for (i = 0; i < 14; ++i) { 2476 if (sc->txpow[i] > 31) 2477 sc->txpow[i] = 24; 2478 } 2479 2480 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE); 2481 if ((val & 0xff) == 0xff) 2482 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR; 2483 else 2484 sc->rssi_corr = val & 0xff; 2485 DPRINTF(sc, "rssi correction %d, calibrate 0x%02x\n", 2486 sc->rssi_corr, val); 2487 } 2488 2489 2490 static void 2491 rt2560_scan_start(struct ieee80211com *ic) 2492 { 2493 struct ifnet *ifp = ic->ic_ifp; 2494 struct rt2560_softc *sc = ifp->if_softc; 2495 2496 /* abort TSF synchronization */ 2497 RAL_WRITE(sc, RT2560_CSR14, 0); 2498 rt2560_set_bssid(sc, ifp->if_broadcastaddr); 2499 } 2500 2501 static void 2502 rt2560_scan_end(struct ieee80211com *ic) 2503 { 2504 struct ifnet *ifp = ic->ic_ifp; 2505 struct rt2560_softc *sc = ifp->if_softc; 2506 struct ieee80211vap *vap = ic->ic_scan->ss_vap; 2507 2508 rt2560_enable_tsf_sync(sc); 2509 /* XXX keep local copy */ 2510 rt2560_set_bssid(sc, vap->iv_bss->ni_bssid); 2511 } 2512 2513 static int 2514 rt2560_bbp_init(struct rt2560_softc *sc) 2515 { 2516 #define N(a) (sizeof (a) / sizeof ((a)[0])) 2517 int i, ntries; 2518 2519 /* wait for BBP to be ready */ 2520 for (ntries = 0; ntries < 100; ntries++) { 2521 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0) 2522 break; 2523 DELAY(1); 2524 } 2525 if (ntries == 100) { 2526 device_printf(sc->sc_dev, "timeout waiting for BBP\n"); 2527 return EIO; 2528 } 2529 2530 /* initialize BBP registers to default values */ 2531 for (i = 0; i < N(rt2560_def_bbp); i++) { 2532 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg, 2533 rt2560_def_bbp[i].val); 2534 } 2535 2536 /* initialize BBP registers to values stored in EEPROM */ 2537 for (i = 0; i < 16; i++) { 2538 if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0) 2539 break; 2540 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val); 2541 } 2542 rt2560_bbp_write(sc, 17, 0x48); /* XXX restore bbp17 */ 2543 2544 return 0; 2545 #undef N 2546 } 2547 2548 static void 2549 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna) 2550 { 2551 uint32_t tmp; 2552 uint8_t tx; 2553 2554 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK; 2555 if (antenna == 1) 2556 tx |= RT2560_BBP_ANTA; 2557 else if (antenna == 2) 2558 tx |= RT2560_BBP_ANTB; 2559 else 2560 tx |= RT2560_BBP_DIVERSITY; 2561 2562 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */ 2563 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 || 2564 sc->rf_rev == RT2560_RF_5222) 2565 tx |= RT2560_BBP_FLIPIQ; 2566 2567 rt2560_bbp_write(sc, RT2560_BBP_TX, tx); 2568 2569 /* update values for CCK and OFDM in BBPCSR1 */ 2570 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007; 2571 tmp |= (tx & 0x7) << 16 | (tx & 0x7); 2572 RAL_WRITE(sc, RT2560_BBPCSR1, tmp); 2573 } 2574 2575 static void 2576 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna) 2577 { 2578 uint8_t rx; 2579 2580 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK; 2581 if (antenna == 1) 2582 rx |= RT2560_BBP_ANTA; 2583 else if (antenna == 2) 2584 rx |= RT2560_BBP_ANTB; 2585 else 2586 rx |= RT2560_BBP_DIVERSITY; 2587 2588 /* need to force no I/Q flip for RF 2525e and 2526 */ 2589 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526) 2590 rx &= ~RT2560_BBP_FLIPIQ; 2591 2592 rt2560_bbp_write(sc, RT2560_BBP_RX, rx); 2593 } 2594 2595 static void 2596 rt2560_init_locked(struct rt2560_softc *sc) 2597 { 2598 #define N(a) (sizeof (a) / sizeof ((a)[0])) 2599 struct ifnet *ifp = sc->sc_ifp; 2600 struct ieee80211com *ic = ifp->if_l2com; 2601 uint32_t tmp; 2602 int i; 2603 2604 rt2560_stop_locked(sc); 2605 2606 /* setup tx rings */ 2607 tmp = RT2560_PRIO_RING_COUNT << 24 | 2608 RT2560_ATIM_RING_COUNT << 16 | 2609 RT2560_TX_RING_COUNT << 8 | 2610 RT2560_TX_DESC_SIZE; 2611 2612 /* rings must be initialized in this exact order */ 2613 RAL_WRITE(sc, RT2560_TXCSR2, tmp); 2614 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr); 2615 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr); 2616 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr); 2617 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr); 2618 2619 /* setup rx ring */ 2620 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE; 2621 2622 RAL_WRITE(sc, RT2560_RXCSR1, tmp); 2623 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr); 2624 2625 /* initialize MAC registers to default values */ 2626 for (i = 0; i < N(rt2560_def_mac); i++) 2627 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val); 2628 2629 rt2560_set_macaddr(sc, IF_LLADDR(ifp)); 2630 2631 /* set basic rate set (will be updated later) */ 2632 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153); 2633 2634 rt2560_update_slot(ifp); 2635 rt2560_update_plcp(sc); 2636 rt2560_update_led(sc, 0, 0); 2637 2638 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC); 2639 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY); 2640 2641 if (rt2560_bbp_init(sc) != 0) { 2642 rt2560_stop(sc); 2643 return; 2644 } 2645 2646 rt2560_set_txantenna(sc, sc->tx_ant); 2647 rt2560_set_rxantenna(sc, sc->rx_ant); 2648 2649 /* set default BSS channel */ 2650 rt2560_set_chan(sc, ic->ic_curchan); 2651 2652 /* kick Rx */ 2653 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR; 2654 if (ic->ic_opmode != IEEE80211_M_MONITOR) { 2655 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR; 2656 if (ic->ic_opmode != IEEE80211_M_HOSTAP && 2657 ic->ic_opmode != IEEE80211_M_MBSS) 2658 tmp |= RT2560_DROP_TODS; 2659 if (!(ifp->if_flags & IFF_PROMISC)) 2660 tmp |= RT2560_DROP_NOT_TO_ME; 2661 } 2662 RAL_WRITE(sc, RT2560_RXCSR0, tmp); 2663 2664 /* clear old FCS and Rx FIFO errors */ 2665 RAL_READ(sc, RT2560_CNT0); 2666 RAL_READ(sc, RT2560_CNT4); 2667 2668 /* clear any pending interrupts */ 2669 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff); 2670 2671 /* enable interrupts */ 2672 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK); 2673 2674 ifp->if_flags &= ~IFF_OACTIVE; 2675 ifp->if_flags |= IFF_RUNNING; 2676 2677 callout_reset(&sc->watchdog_ch, hz, rt2560_watchdog_callout, sc); 2678 #undef N 2679 } 2680 2681 static void 2682 rt2560_init(void *priv) 2683 { 2684 struct rt2560_softc *sc = priv; 2685 struct ifnet *ifp = sc->sc_ifp; 2686 struct ieee80211com *ic = ifp->if_l2com; 2687 2688 rt2560_init_locked(sc); 2689 2690 if (ifp->if_flags & IFF_RUNNING) 2691 ieee80211_start_all(ic); /* start all vap's */ 2692 } 2693 2694 static void 2695 rt2560_stop_locked(struct rt2560_softc *sc) 2696 { 2697 struct ifnet *ifp = sc->sc_ifp; 2698 volatile int *flags = &sc->sc_flags; 2699 2700 while (*flags & RT2560_F_INPUT_RUNNING) 2701 zsleep(sc, &wlan_global_serializer, 0, "ralrunning", hz/10); 2702 2703 callout_stop(&sc->watchdog_ch); 2704 sc->sc_tx_timer = 0; 2705 2706 if (ifp->if_flags & IFF_RUNNING) { 2707 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE); 2708 2709 /* abort Tx */ 2710 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX); 2711 2712 /* disable Rx */ 2713 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX); 2714 2715 /* reset ASIC (imply reset BBP) */ 2716 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC); 2717 RAL_WRITE(sc, RT2560_CSR1, 0); 2718 2719 /* disable interrupts */ 2720 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff); 2721 2722 /* reset Tx and Rx rings */ 2723 rt2560_reset_tx_ring(sc, &sc->txq); 2724 rt2560_reset_tx_ring(sc, &sc->atimq); 2725 rt2560_reset_tx_ring(sc, &sc->prioq); 2726 rt2560_reset_tx_ring(sc, &sc->bcnq); 2727 rt2560_reset_rx_ring(sc, &sc->rxq); 2728 } 2729 sc->sc_flags &= ~(RT2560_F_PRIO_OACTIVE | RT2560_F_DATA_OACTIVE); 2730 } 2731 2732 void 2733 rt2560_stop(void *arg) 2734 { 2735 struct rt2560_softc *sc = arg; 2736 2737 rt2560_stop_locked(sc); 2738 } 2739 2740 static int 2741 rt2560_raw_xmit(struct ieee80211_node *ni, struct mbuf *m, 2742 const struct ieee80211_bpf_params *params) 2743 { 2744 struct ieee80211com *ic = ni->ni_ic; 2745 struct ifnet *ifp = ic->ic_ifp; 2746 struct rt2560_softc *sc = ifp->if_softc; 2747 2748 /* prevent management frames from being sent if we're not ready */ 2749 if (!(ifp->if_flags & IFF_RUNNING)) { 2750 m_freem(m); 2751 ieee80211_free_node(ni); 2752 return ENETDOWN; 2753 } 2754 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) { 2755 ifp->if_flags |= IFF_OACTIVE; 2756 sc->sc_flags |= RT2560_F_PRIO_OACTIVE; 2757 m_freem(m); 2758 ieee80211_free_node(ni); 2759 return ENOBUFS; /* XXX */ 2760 } 2761 2762 ifp->if_opackets++; 2763 2764 if (params == NULL) { 2765 /* 2766 * Legacy path; interpret frame contents to decide 2767 * precisely how to send the frame. 2768 */ 2769 if (rt2560_tx_mgt(sc, m, ni) != 0) 2770 goto bad; 2771 } else { 2772 /* 2773 * Caller supplied explicit parameters to use in 2774 * sending the frame. 2775 */ 2776 if (rt2560_tx_raw(sc, m, ni, params)) 2777 goto bad; 2778 } 2779 sc->sc_tx_timer = 5; 2780 2781 return 0; 2782 bad: 2783 ifp->if_oerrors++; 2784 ieee80211_free_node(ni); 2785 return EIO; /* XXX */ 2786 } 2787