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