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