3 * Damien Bergamini <damien.bergamini@free.fr>
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.
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.
17 * $FreeBSD: src/sys/dev/ral/rt2661.c,v 1.4 2006/03/21 21:15:43 damien Exp $
18 * $DragonFly: src/sys/dev/netif/ral/rt2661.c,v 1.13 2007/02/07 12:34:26 sephe Exp $
22 * Ralink Technology RT2561, RT2561S and RT2661 chipset driver
23 * http://www.ralinktech.com/
26 #include <sys/param.h>
28 #include <sys/endian.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
32 #include <sys/module.h>
33 #include <sys/queue.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 #include <sys/sysctl.h>
38 #include <sys/serialize.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>
48 #include <netproto/802_11/ieee80211_var.h>
49 #include <netproto/802_11/ieee80211_radiotap.h>
51 #include <dev/netif/ral/if_ralrate.h>
52 #include <dev/netif/ral/rt2661reg.h>
53 #include <dev/netif/ral/rt2661var.h>
54 #include <dev/netif/ral/rt2661_ucode.h>
57 #define DPRINTF(x) do { if (ral_debug > 0) kprintf x; } while (0)
58 #define DPRINTFN(n, x) do { if (ral_debug >= (n)) kprintf x; } while (0)
60 SYSCTL_INT(_debug, OID_AUTO, ral, CTLFLAG_RW, &ral_debug, 0, "ral debug level");
63 #define DPRINTFN(n, x)
66 MALLOC_DEFINE(M_RT2661, "rt2661_ratectl", "rt2661 rate control data");
68 static void rt2661_dma_map_addr(void *, bus_dma_segment_t *, int,
70 static void rt2661_dma_map_mbuf(void *, bus_dma_segment_t *, int,
72 static int rt2661_alloc_tx_ring(struct rt2661_softc *,
73 struct rt2661_tx_ring *, int);
74 static void rt2661_reset_tx_ring(struct rt2661_softc *,
75 struct rt2661_tx_ring *);
76 static void rt2661_free_tx_ring(struct rt2661_softc *,
77 struct rt2661_tx_ring *);
78 static int rt2661_alloc_rx_ring(struct rt2661_softc *,
79 struct rt2661_rx_ring *, int);
80 static void rt2661_reset_rx_ring(struct rt2661_softc *,
81 struct rt2661_rx_ring *);
82 static void rt2661_free_rx_ring(struct rt2661_softc *,
83 struct rt2661_rx_ring *);
84 static struct ieee80211_node *rt2661_node_alloc(
85 struct ieee80211_node_table *);
86 static int rt2661_media_change(struct ifnet *);
87 static void rt2661_next_scan(void *);
88 static int rt2661_newstate(struct ieee80211com *,
89 enum ieee80211_state, int);
90 static uint16_t rt2661_eeprom_read(struct rt2661_softc *, uint8_t);
91 static void rt2661_rx_intr(struct rt2661_softc *);
92 static void rt2661_tx_intr(struct rt2661_softc *);
93 static void rt2661_tx_dma_intr(struct rt2661_softc *,
94 struct rt2661_tx_ring *);
95 static void rt2661_mcu_beacon_expire(struct rt2661_softc *);
96 static void rt2661_mcu_wakeup(struct rt2661_softc *);
97 static void rt2661_mcu_cmd_intr(struct rt2661_softc *);
98 static int rt2661_ack_rate(struct ieee80211com *, int);
99 static uint16_t rt2661_txtime(int, int, uint32_t);
100 static uint8_t rt2661_rxrate(struct rt2661_rx_desc *);
101 static uint8_t rt2661_plcp_signal(int);
102 static void rt2661_setup_tx_desc(struct rt2661_softc *,
103 struct rt2661_tx_desc *, uint32_t, uint16_t, int,
104 int, const bus_dma_segment_t *, int, int, int);
105 static struct mbuf * rt2661_get_rts(struct rt2661_softc *,
106 struct ieee80211_frame *, uint16_t);
107 static int rt2661_tx_data(struct rt2661_softc *, struct mbuf *,
108 struct ieee80211_node *, int);
109 static int rt2661_tx_mgt(struct rt2661_softc *, struct mbuf *,
110 struct ieee80211_node *);
111 static void rt2661_start(struct ifnet *);
112 static void rt2661_watchdog(struct ifnet *);
113 static int rt2661_reset(struct ifnet *);
114 static int rt2661_ioctl(struct ifnet *, u_long, caddr_t,
116 static void rt2661_bbp_write(struct rt2661_softc *, uint8_t,
118 static uint8_t rt2661_bbp_read(struct rt2661_softc *, uint8_t);
119 static void rt2661_rf_write(struct rt2661_softc *, uint8_t,
121 static int rt2661_tx_cmd(struct rt2661_softc *, uint8_t,
123 static void rt2661_select_antenna(struct rt2661_softc *);
124 static void rt2661_enable_mrr(struct rt2661_softc *);
125 static void rt2661_set_txpreamble(struct rt2661_softc *);
126 static void rt2661_set_basicrates(struct rt2661_softc *,
127 const struct ieee80211_rateset *);
128 static void rt2661_select_band(struct rt2661_softc *,
129 struct ieee80211_channel *);
130 static void rt2661_set_chan(struct rt2661_softc *,
131 struct ieee80211_channel *);
132 static void rt2661_set_bssid(struct rt2661_softc *,
134 static void rt2661_set_macaddr(struct rt2661_softc *,
136 static void rt2661_update_promisc(struct rt2661_softc *);
137 static int rt2661_wme_update(struct ieee80211com *) __unused;
138 static void rt2661_update_slot(struct ifnet *);
139 static const char *rt2661_get_rf(int);
140 static void rt2661_read_eeprom(struct rt2661_softc *);
141 static int rt2661_bbp_init(struct rt2661_softc *);
142 static void rt2661_init(void *);
143 static void rt2661_stop(void *);
144 static void rt2661_intr(void *);
145 static int rt2661_load_microcode(struct rt2661_softc *,
146 const uint8_t *, int);
148 static void rt2661_rx_tune(struct rt2661_softc *);
149 static void rt2661_radar_start(struct rt2661_softc *);
150 static int rt2661_radar_stop(struct rt2661_softc *);
152 static int rt2661_prepare_beacon(struct rt2661_softc *);
153 static void rt2661_enable_tsf_sync(struct rt2661_softc *);
154 static int rt2661_get_rssi(struct rt2661_softc *, uint8_t);
155 static void rt2661_led_newstate(struct rt2661_softc *,
156 enum ieee80211_state);
159 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
161 static const struct ieee80211_rateset rt2661_rateset_11a =
162 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
164 static const struct ieee80211_rateset rt2661_rateset_11b =
165 { 4, { 2, 4, 11, 22 } };
167 static const struct ieee80211_rateset rt2661_rateset_11g =
168 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
170 static const struct {
173 } rt2661_def_mac[] = {
177 static const struct {
180 } rt2661_def_bbp[] = {
184 static const struct rfprog {
186 uint32_t r1, r2, r3, r4;
187 } rt2661_rf5225_1[] = {
189 }, rt2661_rf5225_2[] = {
193 #define LED_EE2MCU(bit) { \
194 .ee_bit = RT2661_EE_LED_##bit, \
195 .mcu_bit = RT2661_MCU_LED_##bit \
197 static const struct {
212 struct rt2661_dmamap {
213 bus_dma_segment_t segs[RT2661_MAX_SCATTER];
218 rt2661_attach(device_t dev, int id)
220 struct rt2661_softc *sc = device_get_softc(dev);
221 struct ieee80211com *ic = &sc->sc_ic;
222 struct ifnet *ifp = &ic->ic_if;
224 const uint8_t *ucode = NULL;
225 int error, i, ac, ntries, size = 0;
227 callout_init(&sc->scan_ch);
228 callout_init(&sc->rssadapt_ch);
231 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irq_rid,
232 RF_ACTIVE | RF_SHAREABLE);
233 if (sc->sc_irq == NULL) {
234 device_printf(dev, "could not allocate interrupt resource\n");
238 /* wait for NIC to initialize */
239 for (ntries = 0; ntries < 1000; ntries++) {
240 if ((val = RAL_READ(sc, RT2661_MAC_CSR0)) != 0)
244 if (ntries == 1000) {
245 device_printf(sc->sc_dev,
246 "timeout waiting for NIC to initialize\n");
251 /* retrieve RF rev. no and various other things from EEPROM */
252 rt2661_read_eeprom(sc);
254 device_printf(dev, "MAC/BBP RT%X, RF %s\n", val,
255 rt2661_get_rf(sc->rf_rev));
258 * Load 8051 microcode into NIC.
262 ucode = rt2561s_ucode;
263 size = sizeof rt2561s_ucode;
266 ucode = rt2561_ucode;
267 size = sizeof rt2561_ucode;
270 ucode = rt2661_ucode;
271 size = sizeof rt2661_ucode;
275 error = rt2661_load_microcode(sc, ucode, size);
277 device_printf(sc->sc_dev, "could not load 8051 microcode\n");
282 * Allocate Tx and Rx rings.
284 for (ac = 0; ac < 4; ac++) {
285 error = rt2661_alloc_tx_ring(sc, &sc->txq[ac],
286 RT2661_TX_RING_COUNT);
288 device_printf(sc->sc_dev,
289 "could not allocate Tx ring %d\n", ac);
294 error = rt2661_alloc_tx_ring(sc, &sc->mgtq, RT2661_MGT_RING_COUNT);
296 device_printf(sc->sc_dev, "could not allocate Mgt ring\n");
300 error = rt2661_alloc_rx_ring(sc, &sc->rxq, RT2661_RX_RING_COUNT);
302 device_printf(sc->sc_dev, "could not allocate Rx ring\n");
306 STAILQ_INIT(&sc->tx_ratectl);
308 sysctl_ctx_init(&sc->sysctl_ctx);
309 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
310 SYSCTL_STATIC_CHILDREN(_hw),
312 device_get_nameunit(dev),
314 if (sc->sysctl_tree == NULL) {
315 device_printf(dev, "could not add sysctl node\n");
321 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
322 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
323 ifp->if_init = rt2661_init;
324 ifp->if_ioctl = rt2661_ioctl;
325 ifp->if_start = rt2661_start;
326 ifp->if_watchdog = rt2661_watchdog;
327 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
328 ifq_set_ready(&ifp->if_snd);
330 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
331 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
332 ic->ic_state = IEEE80211_S_INIT;
333 rt2661_led_newstate(sc, IEEE80211_S_INIT);
335 /* set device capabilities */
337 IEEE80211_C_IBSS | /* IBSS mode supported */
338 IEEE80211_C_MONITOR | /* monitor mode supported */
339 IEEE80211_C_HOSTAP | /* HostAp mode supported */
340 IEEE80211_C_TXPMGT | /* tx power management */
341 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
342 IEEE80211_C_SHSLOT | /* short slot time supported */
344 IEEE80211_C_WME | /* 802.11e */
346 IEEE80211_C_WEP | /* WEP */
347 IEEE80211_C_WPA; /* 802.11i */
349 if (sc->rf_rev == RT2661_RF_5225 || sc->rf_rev == RT2661_RF_5325) {
350 /* set supported .11a rates */
351 ic->ic_sup_rates[IEEE80211_MODE_11A] = rt2661_rateset_11a;
353 /* set supported .11a channels */
354 for (i = 36; i <= 64; i += 4) {
355 ic->ic_channels[i].ic_freq =
356 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
357 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
359 for (i = 100; i <= 140; i += 4) {
360 ic->ic_channels[i].ic_freq =
361 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
362 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
364 for (i = 149; i <= 165; i += 4) {
365 ic->ic_channels[i].ic_freq =
366 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
367 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
371 /* set supported .11b and .11g rates */
372 ic->ic_sup_rates[IEEE80211_MODE_11B] = rt2661_rateset_11b;
373 ic->ic_sup_rates[IEEE80211_MODE_11G] = rt2661_rateset_11g;
375 /* set supported .11b and .11g channels (1 through 14) */
376 for (i = 1; i <= 14; i++) {
377 ic->ic_channels[i].ic_freq =
378 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
379 ic->ic_channels[i].ic_flags =
380 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
381 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
384 ieee80211_ifattach(ic);
385 ic->ic_node_alloc = rt2661_node_alloc;
386 /* ic->ic_wme.wme_update = rt2661_wme_update;*/
387 ic->ic_updateslot = rt2661_update_slot;
388 ic->ic_reset = rt2661_reset;
389 /* enable s/w bmiss handling in sta mode */
390 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
392 /* override state transition machine */
393 sc->sc_newstate = ic->ic_newstate;
394 ic->ic_newstate = rt2661_newstate;
395 ieee80211_media_init(ic, rt2661_media_change, ieee80211_media_status);
397 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
398 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
400 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
401 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
402 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2661_RX_RADIOTAP_PRESENT);
404 sc->sc_txtap_len = sizeof sc->sc_txtapu;
405 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
406 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2661_TX_RADIOTAP_PRESENT);
409 * Add a few sysctl knobs.
413 SYSCTL_ADD_INT(&sc->sysctl_ctx,
414 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "dwell",
415 CTLFLAG_RW, &sc->dwelltime, 0,
416 "channel dwell time (ms) for AP/station scanning");
418 error = bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE, rt2661_intr,
419 sc, &sc->sc_ih, ifp->if_serializer);
421 device_printf(dev, "could not set up interrupt\n");
423 ieee80211_ifdetach(ic);
428 ieee80211_announce(ic);
436 rt2661_detach(void *xsc)
438 struct rt2661_softc *sc = xsc;
439 struct ieee80211com *ic = &sc->sc_ic;
440 struct ifnet *ifp = &ic->ic_if;
442 if (device_is_attached(sc->sc_dev)) {
443 lwkt_serialize_enter(ifp->if_serializer);
445 callout_stop(&sc->scan_ch);
446 callout_stop(&sc->rssadapt_ch);
448 bus_teardown_intr(sc->sc_dev, sc->sc_irq, sc->sc_ih);
450 lwkt_serialize_exit(ifp->if_serializer);
453 ieee80211_ifdetach(ic);
456 rt2661_free_tx_ring(sc, &sc->txq[0]);
457 rt2661_free_tx_ring(sc, &sc->txq[1]);
458 rt2661_free_tx_ring(sc, &sc->txq[2]);
459 rt2661_free_tx_ring(sc, &sc->txq[3]);
460 rt2661_free_tx_ring(sc, &sc->mgtq);
461 rt2661_free_rx_ring(sc, &sc->rxq);
463 if (sc->sc_irq != NULL) {
464 bus_release_resource(sc->sc_dev, SYS_RES_IRQ, sc->sc_irq_rid,
468 if (sc->sysctl_tree != NULL)
469 sysctl_ctx_free(&sc->sysctl_ctx);
475 rt2661_shutdown(void *xsc)
477 struct rt2661_softc *sc = xsc;
478 struct ifnet *ifp = &sc->sc_ic.ic_if;
480 lwkt_serialize_enter(ifp->if_serializer);
482 lwkt_serialize_exit(ifp->if_serializer);
486 rt2661_suspend(void *xsc)
488 struct rt2661_softc *sc = xsc;
489 struct ifnet *ifp = &sc->sc_ic.ic_if;
491 lwkt_serialize_enter(ifp->if_serializer);
493 lwkt_serialize_exit(ifp->if_serializer);
497 rt2661_resume(void *xsc)
499 struct rt2661_softc *sc = xsc;
500 struct ifnet *ifp = sc->sc_ic.ic_ifp;
502 lwkt_serialize_enter(ifp->if_serializer);
503 if (ifp->if_flags & IFF_UP) {
504 ifp->if_init(ifp->if_softc);
505 if (ifp->if_flags & IFF_RUNNING)
508 lwkt_serialize_exit(ifp->if_serializer);
512 rt2661_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
517 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
519 *(bus_addr_t *)arg = segs[0].ds_addr;
523 rt2661_alloc_tx_ring(struct rt2661_softc *sc, struct rt2661_tx_ring *ring,
530 ring->cur = ring->next = 0;
532 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
533 BUS_SPACE_MAXADDR, NULL, NULL, count * RT2661_TX_DESC_SIZE, 1,
534 count * RT2661_TX_DESC_SIZE, 0, &ring->desc_dmat);
536 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
540 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
541 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
543 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
547 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
548 count * RT2661_TX_DESC_SIZE, rt2661_dma_map_addr, &ring->physaddr,
551 device_printf(sc->sc_dev, "could not load desc DMA map\n");
553 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
558 ring->data = kmalloc(count * sizeof (struct rt2661_data), M_DEVBUF,
561 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
562 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES * RT2661_MAX_SCATTER,
563 RT2661_MAX_SCATTER, MCLBYTES, 0, &ring->data_dmat);
565 device_printf(sc->sc_dev, "could not create data DMA tag\n");
569 for (i = 0; i < count; i++) {
570 error = bus_dmamap_create(ring->data_dmat, 0,
573 device_printf(sc->sc_dev, "could not create DMA map\n");
579 fail: rt2661_free_tx_ring(sc, ring);
584 rt2661_reset_tx_ring(struct rt2661_softc *sc, struct rt2661_tx_ring *ring)
586 struct rt2661_tx_desc *desc;
587 struct rt2661_data *data;
590 for (i = 0; i < ring->count; i++) {
591 desc = &ring->desc[i];
592 data = &ring->data[i];
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);
605 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
608 ring->cur = ring->next = 0;
612 rt2661_free_tx_ring(struct rt2661_softc *sc, struct rt2661_tx_ring *ring)
614 struct rt2661_data *data;
617 if (ring->desc != NULL) {
618 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
619 BUS_DMASYNC_POSTWRITE);
620 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
621 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
625 if (ring->desc_dmat != NULL) {
626 bus_dma_tag_destroy(ring->desc_dmat);
627 ring->desc_dmat = NULL;
630 if (ring->data != NULL) {
631 for (i = 0; i < ring->count; i++) {
632 data = &ring->data[i];
634 if (data->m != NULL) {
635 bus_dmamap_sync(ring->data_dmat, data->map,
636 BUS_DMASYNC_POSTWRITE);
637 bus_dmamap_unload(ring->data_dmat, data->map);
642 if (data->map != NULL) {
643 bus_dmamap_destroy(ring->data_dmat, data->map);
648 kfree(ring->data, M_DEVBUF);
652 if (ring->data_dmat != NULL) {
653 bus_dma_tag_destroy(ring->data_dmat);
654 ring->data_dmat = NULL;
659 rt2661_alloc_rx_ring(struct rt2661_softc *sc, struct rt2661_rx_ring *ring,
662 struct rt2661_rx_desc *desc;
663 struct rt2661_data *data;
668 ring->cur = ring->next = 0;
670 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
671 BUS_SPACE_MAXADDR, NULL, NULL, count * RT2661_RX_DESC_SIZE, 1,
672 count * RT2661_RX_DESC_SIZE, 0, &ring->desc_dmat);
674 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
678 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
679 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
681 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
685 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
686 count * RT2661_RX_DESC_SIZE, rt2661_dma_map_addr, &ring->physaddr,
689 device_printf(sc->sc_dev, "could not load desc DMA map\n");
691 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
696 ring->data = kmalloc(count * sizeof (struct rt2661_data), M_DEVBUF,
700 * Pre-allocate Rx buffers and populate Rx ring.
702 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
703 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0,
706 device_printf(sc->sc_dev, "could not create data DMA tag\n");
710 for (i = 0; i < count; i++) {
711 desc = &sc->rxq.desc[i];
712 data = &sc->rxq.data[i];
714 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
716 device_printf(sc->sc_dev, "could not create DMA map\n");
720 data->m = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
721 if (data->m == NULL) {
722 device_printf(sc->sc_dev,
723 "could not allocate rx mbuf\n");
728 error = bus_dmamap_load(ring->data_dmat, data->map,
729 mtod(data->m, void *), MCLBYTES, rt2661_dma_map_addr,
732 device_printf(sc->sc_dev,
733 "could not load rx buf DMA map");
740 desc->flags = htole32(RT2661_RX_BUSY);
741 desc->physaddr = htole32(physaddr);
744 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
748 fail: rt2661_free_rx_ring(sc, ring);
753 rt2661_reset_rx_ring(struct rt2661_softc *sc, struct rt2661_rx_ring *ring)
757 for (i = 0; i < ring->count; i++)
758 ring->desc[i].flags = htole32(RT2661_RX_BUSY);
760 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
762 ring->cur = ring->next = 0;
766 rt2661_free_rx_ring(struct rt2661_softc *sc, struct rt2661_rx_ring *ring)
768 struct rt2661_data *data;
771 if (ring->desc != NULL) {
772 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
773 BUS_DMASYNC_POSTWRITE);
774 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
775 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
779 if (ring->desc_dmat != NULL) {
780 bus_dma_tag_destroy(ring->desc_dmat);
781 ring->desc_dmat = NULL;
784 if (ring->data != NULL) {
785 for (i = 0; i < ring->count; i++) {
786 data = &ring->data[i];
788 if (data->m != NULL) {
789 bus_dmamap_sync(ring->data_dmat, data->map,
790 BUS_DMASYNC_POSTREAD);
791 bus_dmamap_unload(ring->data_dmat, data->map);
796 if (data->map != NULL) {
797 bus_dmamap_destroy(ring->data_dmat, data->map);
802 kfree(ring->data, M_DEVBUF);
806 if (ring->data_dmat != NULL) {
807 bus_dma_tag_destroy(ring->data_dmat);
808 ring->data_dmat = NULL;
812 static struct ieee80211_node *
813 rt2661_node_alloc(struct ieee80211_node_table *nt)
815 struct rt2661_node *rn;
817 rn = kmalloc(sizeof (struct rt2661_node), M_80211_NODE,
820 return (rn != NULL) ? &rn->ni : NULL;
824 rt2661_media_change(struct ifnet *ifp)
826 struct rt2661_softc *sc = ifp->if_softc;
829 error = ieee80211_media_change(ifp);
830 if (error != ENETRESET)
833 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
839 * This function is called periodically (every 200ms) during scanning to
840 * switch from one channel to another.
843 rt2661_next_scan(void *arg)
845 struct rt2661_softc *sc = arg;
846 struct ieee80211com *ic = &sc->sc_ic;
847 struct ifnet *ifp = &ic->ic_if;
849 lwkt_serialize_enter(ifp->if_serializer);
850 if (ic->ic_state == IEEE80211_S_SCAN)
851 ieee80211_next_scan(ic);
852 lwkt_serialize_exit(ifp->if_serializer);
856 * This function is called for each node present in the node station table.
859 rt2661_iter_func(void *arg, struct ieee80211_node *ni)
861 struct rt2661_node *rn = (struct rt2661_node *)ni;
863 ral_rssadapt_updatestats(&rn->rssadapt);
867 * This function is called periodically (every 100ms) in RUN state to update
868 * the rate adaptation statistics.
871 rt2661_update_rssadapt(void *arg)
873 struct rt2661_softc *sc = arg;
874 struct ieee80211com *ic = &sc->sc_ic;
875 struct ifnet *ifp = &ic->ic_if;
877 lwkt_serialize_enter(ifp->if_serializer);
879 ieee80211_iterate_nodes(&ic->ic_sta, rt2661_iter_func, arg);
880 callout_reset(&sc->rssadapt_ch, hz / 10, rt2661_update_rssadapt, sc);
882 lwkt_serialize_exit(ifp->if_serializer);
886 rt2661_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
888 struct rt2661_softc *sc = ic->ic_ifp->if_softc;
889 enum ieee80211_state ostate;
890 struct ieee80211_node *ni;
894 ostate = ic->ic_state;
895 callout_stop(&sc->scan_ch);
897 if (ostate != nstate)
898 rt2661_led_newstate(sc, nstate);
901 case IEEE80211_S_INIT:
902 callout_stop(&sc->rssadapt_ch);
904 if (ostate == IEEE80211_S_RUN) {
905 /* abort TSF synchronization */
906 tmp = RAL_READ(sc, RT2661_TXRX_CSR9);
907 RAL_WRITE(sc, RT2661_TXRX_CSR9, tmp & ~0x00ffffff);
911 case IEEE80211_S_SCAN:
912 rt2661_set_chan(sc, ic->ic_curchan);
913 callout_reset(&sc->scan_ch, (sc->dwelltime * hz) / 1000,
914 rt2661_next_scan, sc);
917 case IEEE80211_S_AUTH:
918 case IEEE80211_S_ASSOC:
919 rt2661_set_chan(sc, ic->ic_curchan);
922 case IEEE80211_S_RUN:
923 rt2661_set_chan(sc, ic->ic_curchan);
927 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
928 rt2661_enable_mrr(sc);
929 rt2661_set_txpreamble(sc);
930 rt2661_set_basicrates(sc, &ni->ni_rates);
931 rt2661_set_bssid(sc, ni->ni_bssid);
934 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
935 ic->ic_opmode == IEEE80211_M_IBSS) {
936 if ((error = rt2661_prepare_beacon(sc)) != 0)
940 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
941 callout_reset(&sc->rssadapt_ch, hz / 10,
942 rt2661_update_rssadapt, sc);
943 rt2661_enable_tsf_sync(sc);
948 return (error != 0) ? error : sc->sc_newstate(ic, nstate, arg);
952 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
956 rt2661_eeprom_read(struct rt2661_softc *sc, uint8_t addr)
962 /* clock C once before the first command */
963 RT2661_EEPROM_CTL(sc, 0);
965 RT2661_EEPROM_CTL(sc, RT2661_S);
966 RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_C);
967 RT2661_EEPROM_CTL(sc, RT2661_S);
969 /* write start bit (1) */
970 RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_D);
971 RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_D | RT2661_C);
973 /* write READ opcode (10) */
974 RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_D);
975 RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_D | RT2661_C);
976 RT2661_EEPROM_CTL(sc, RT2661_S);
977 RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_C);
979 /* write address (A5-A0 or A7-A0) */
980 n = (RAL_READ(sc, RT2661_E2PROM_CSR) & RT2661_93C46) ? 5 : 7;
981 for (; n >= 0; n--) {
982 RT2661_EEPROM_CTL(sc, RT2661_S |
983 (((addr >> n) & 1) << RT2661_SHIFT_D));
984 RT2661_EEPROM_CTL(sc, RT2661_S |
985 (((addr >> n) & 1) << RT2661_SHIFT_D) | RT2661_C);
988 RT2661_EEPROM_CTL(sc, RT2661_S);
990 /* read data Q15-Q0 */
992 for (n = 15; n >= 0; n--) {
993 RT2661_EEPROM_CTL(sc, RT2661_S | RT2661_C);
994 tmp = RAL_READ(sc, RT2661_E2PROM_CSR);
995 val |= ((tmp & RT2661_Q) >> RT2661_SHIFT_Q) << n;
996 RT2661_EEPROM_CTL(sc, RT2661_S);
999 RT2661_EEPROM_CTL(sc, 0);
1001 /* clear Chip Select and clock C */
1002 RT2661_EEPROM_CTL(sc, RT2661_S);
1003 RT2661_EEPROM_CTL(sc, 0);
1004 RT2661_EEPROM_CTL(sc, RT2661_C);
1010 rt2661_tx_intr(struct rt2661_softc *sc)
1012 struct ieee80211com *ic = &sc->sc_ic;
1013 struct ifnet *ifp = ic->ic_ifp;
1014 struct rt2661_tx_ratectl *rctl;
1015 struct rt2661_node *rn;
1016 uint32_t val, result;
1020 val = RAL_READ(sc, RT2661_STA_CSR4);
1021 if (!(val & RT2661_TX_STAT_VALID))
1024 /* Gather statistics */
1025 result = RT2661_TX_RESULT(val);
1026 if (result == RT2661_TX_SUCCESS)
1031 /* No rate control */
1032 if (RT2661_TX_QID(val) == 0)
1035 /* retrieve rate control algorithm context */
1036 rctl = STAILQ_FIRST(&sc->tx_ratectl);
1040 * This really should not happen. Maybe we should
1041 * use assertion here? But why should we rely on
1042 * hardware to do the correct things? Even the
1043 * reference driver (RT61?) provided by Ralink does
1044 * not provide enough clue that this kind of interrupt
1045 * is promised to be generated for each packet. So
1046 * just print a message and keep going ...
1048 if_printf(ifp, "WARNING: no rate control information\n");
1051 STAILQ_REMOVE_HEAD(&sc->tx_ratectl, link);
1053 rn = (struct rt2661_node *)rctl->ni;
1056 case RT2661_TX_SUCCESS:
1057 retrycnt = RT2661_TX_RETRYCNT(val);
1059 DPRINTFN(10, ("data frame sent successfully after "
1060 "%d retries\n", retrycnt));
1061 if (retrycnt == 0 && rctl->id.id_node != NULL) {
1062 ral_rssadapt_raise_rate(ic, &rn->rssadapt,
1067 case RT2661_TX_RETRY_FAIL:
1068 DPRINTFN(9, ("sending data frame failed (too much "
1070 if (rctl->id.id_node != NULL) {
1071 ral_rssadapt_lower_rate(ic, rctl->ni,
1072 &rn->rssadapt, &rctl->id);
1078 device_printf(sc->sc_dev,
1079 "sending data frame failed 0x%08x\n", val);
1083 ieee80211_free_node(rctl->ni);
1085 kfree(rctl, M_RT2661);
1090 rt2661_tx_dma_intr(struct rt2661_softc *sc, struct rt2661_tx_ring *txq)
1092 struct rt2661_tx_desc *desc;
1093 struct rt2661_data *data;
1095 bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_POSTREAD);
1098 desc = &txq->desc[txq->next];
1099 data = &txq->data[txq->next];
1101 if ((le32toh(desc->flags) & RT2661_TX_BUSY) ||
1102 !(le32toh(desc->flags) & RT2661_TX_VALID))
1105 bus_dmamap_sync(txq->data_dmat, data->map,
1106 BUS_DMASYNC_POSTWRITE);
1107 bus_dmamap_unload(txq->data_dmat, data->map);
1111 /* descriptor is no longer valid */
1112 desc->flags &= ~htole32(RT2661_TX_VALID);
1114 DPRINTFN(15, ("tx dma done q=%p idx=%u\n", txq, txq->next));
1117 if (++txq->next >= txq->count) /* faster than % count */
1121 bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
1123 if (txq->queued < txq->count) {
1124 struct ifnet *ifp = &sc->sc_ic.ic_if;
1126 sc->sc_tx_timer = 0;
1127 ifp->if_flags &= ~IFF_OACTIVE;
1133 rt2661_rx_intr(struct rt2661_softc *sc)
1135 struct ieee80211com *ic = &sc->sc_ic;
1136 struct ifnet *ifp = ic->ic_ifp;
1137 struct rt2661_rx_desc *desc;
1138 struct rt2661_data *data;
1139 bus_addr_t physaddr;
1140 struct ieee80211_frame_min *wh;
1141 struct ieee80211_node *ni;
1142 struct rt2661_node *rn;
1143 struct mbuf *mnew, *m;
1146 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1147 BUS_DMASYNC_POSTREAD);
1152 desc = &sc->rxq.desc[sc->rxq.cur];
1153 data = &sc->rxq.data[sc->rxq.cur];
1155 if (le32toh(desc->flags) & RT2661_RX_BUSY)
1158 if ((le32toh(desc->flags) & RT2661_RX_PHY_ERROR) ||
1159 (le32toh(desc->flags) & RT2661_RX_CRC_ERROR)) {
1161 * This should not happen since we did not request
1162 * to receive those frames when we filled TXRX_CSR0.
1164 DPRINTFN(5, ("PHY or CRC error flags 0x%08x\n",
1165 le32toh(desc->flags)));
1170 if ((le32toh(desc->flags) & RT2661_RX_CIPHER_MASK) != 0) {
1176 * Try to allocate a new mbuf for this ring element and load it
1177 * before processing the current mbuf. If the ring element
1178 * cannot be loaded, drop the received packet and reuse the old
1179 * mbuf. In the unlikely case that the old mbuf can't be
1180 * reloaded either, explicitly panic.
1182 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1188 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1189 BUS_DMASYNC_POSTREAD);
1190 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1192 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1193 mtod(mnew, void *), MCLBYTES, rt2661_dma_map_addr,
1198 /* try to reload the old mbuf */
1199 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1200 mtod(data->m, void *), MCLBYTES,
1201 rt2661_dma_map_addr, &physaddr, 0);
1203 /* very unlikely that it will fail... */
1204 panic("%s: could not load old rx mbuf",
1205 device_get_name(sc->sc_dev));
1212 * New mbuf successfully loaded, update Rx ring and continue
1217 desc->physaddr = htole32(physaddr);
1220 m->m_pkthdr.rcvif = ifp;
1221 m->m_pkthdr.len = m->m_len =
1222 (le32toh(desc->flags) >> 16) & 0xfff;
1224 rssi = rt2661_get_rssi(sc, desc->rssi);
1226 wh = mtod(m, struct ieee80211_frame_min *);
1227 ni = ieee80211_find_rxnode(ic, wh);
1229 /* Error happened during RSSI conversion. */
1233 if (sc->sc_drvbpf != NULL) {
1234 struct rt2661_rx_radiotap_header *tap = &sc->sc_rxtap;
1235 uint32_t tsf_lo, tsf_hi;
1237 /* get timestamp (low and high 32 bits) */
1238 tsf_hi = RAL_READ(sc, RT2661_TXRX_CSR13);
1239 tsf_lo = RAL_READ(sc, RT2661_TXRX_CSR12);
1242 htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1244 tap->wr_rate = rt2661_rxrate(desc);
1245 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1246 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1247 tap->wr_antsignal = rssi;
1249 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1252 /* send the frame to the 802.11 layer */
1253 ieee80211_input(ic, m, ni, rssi, 0);
1255 /* give rssi to the rate adatation algorithm */
1256 rn = (struct rt2661_node *)ni;
1257 ral_rssadapt_input(ic, ni, &rn->rssadapt, rssi);
1259 /* node is no longer needed */
1260 ieee80211_free_node(ni);
1262 skip: desc->flags |= htole32(RT2661_RX_BUSY);
1264 DPRINTFN(15, ("rx intr idx=%u\n", sc->rxq.cur));
1266 sc->rxq.cur = (sc->rxq.cur + 1) % RT2661_RX_RING_COUNT;
1269 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1270 BUS_DMASYNC_PREWRITE);
1275 rt2661_mcu_beacon_expire(struct rt2661_softc *sc)
1281 rt2661_mcu_wakeup(struct rt2661_softc *sc)
1283 RAL_WRITE(sc, RT2661_MAC_CSR11, 5 << 16);
1285 RAL_WRITE(sc, RT2661_SOFT_RESET_CSR, 0x7);
1286 RAL_WRITE(sc, RT2661_IO_CNTL_CSR, 0x18);
1287 RAL_WRITE(sc, RT2661_PCI_USEC_CSR, 0x20);
1289 /* send wakeup command to MCU */
1290 rt2661_tx_cmd(sc, RT2661_MCU_CMD_WAKEUP, 0);
1294 rt2661_mcu_cmd_intr(struct rt2661_softc *sc)
1296 RAL_READ(sc, RT2661_M2H_CMD_DONE_CSR);
1297 RAL_WRITE(sc, RT2661_M2H_CMD_DONE_CSR, 0xffffffff);
1301 rt2661_intr(void *arg)
1303 struct rt2661_softc *sc = arg;
1304 struct ifnet *ifp = &sc->sc_ic.ic_if;
1307 /* disable MAC and MCU interrupts */
1308 RAL_WRITE(sc, RT2661_INT_MASK_CSR, 0xffffff7f);
1309 RAL_WRITE(sc, RT2661_MCU_INT_MASK_CSR, 0xffffffff);
1311 /* don't re-enable interrupts if we're shutting down */
1312 if (!(ifp->if_flags & IFF_RUNNING))
1315 r1 = RAL_READ(sc, RT2661_INT_SOURCE_CSR);
1316 RAL_WRITE(sc, RT2661_INT_SOURCE_CSR, r1);
1318 r2 = RAL_READ(sc, RT2661_MCU_INT_SOURCE_CSR);
1319 RAL_WRITE(sc, RT2661_MCU_INT_SOURCE_CSR, r2);
1321 if (r1 & RT2661_MGT_DONE)
1322 rt2661_tx_dma_intr(sc, &sc->mgtq);
1324 if (r1 & RT2661_RX_DONE)
1327 if (r1 & RT2661_TX0_DMA_DONE)
1328 rt2661_tx_dma_intr(sc, &sc->txq[0]);
1330 if (r1 & RT2661_TX1_DMA_DONE)
1331 rt2661_tx_dma_intr(sc, &sc->txq[1]);
1333 if (r1 & RT2661_TX2_DMA_DONE)
1334 rt2661_tx_dma_intr(sc, &sc->txq[2]);
1336 if (r1 & RT2661_TX3_DMA_DONE)
1337 rt2661_tx_dma_intr(sc, &sc->txq[3]);
1339 if (r1 & RT2661_TX_DONE)
1342 if (r2 & RT2661_MCU_CMD_DONE)
1343 rt2661_mcu_cmd_intr(sc);
1345 if (r2 & RT2661_MCU_BEACON_EXPIRE)
1346 rt2661_mcu_beacon_expire(sc);
1348 if (r2 & RT2661_MCU_WAKEUP)
1349 rt2661_mcu_wakeup(sc);
1351 /* re-enable MAC and MCU interrupts */
1352 RAL_WRITE(sc, RT2661_INT_MASK_CSR, 0x0000ff10);
1353 RAL_WRITE(sc, RT2661_MCU_INT_MASK_CSR, 0);
1356 /* quickly determine if a given rate is CCK or OFDM */
1357 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1359 #define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */
1360 #define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */
1362 #define RAL_SIFS 10 /* us */
1365 * This function is only used by the Rx radiotap code. It returns the rate at
1366 * which a given frame was received.
1369 rt2661_rxrate(struct rt2661_rx_desc *desc)
1371 if (le32toh(desc->flags) & RT2661_RX_OFDM) {
1372 /* reverse function of rt2661_plcp_signal */
1373 switch (desc->rate & 0xf) {
1374 case 0xb: return 12;
1375 case 0xf: return 18;
1376 case 0xa: return 24;
1377 case 0xe: return 36;
1378 case 0x9: return 48;
1379 case 0xd: return 72;
1380 case 0x8: return 96;
1381 case 0xc: return 108;
1384 if (desc->rate == 10)
1386 if (desc->rate == 20)
1388 if (desc->rate == 55)
1390 if (desc->rate == 110)
1393 return 2; /* should not get there */
1397 * Return the expected ack rate for a frame transmitted at rate `rate'.
1398 * XXX: this should depend on the destination node basic rate set.
1401 rt2661_ack_rate(struct ieee80211com *ic, int rate)
1410 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1426 /* default to 1Mbps */
1431 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1432 * The function automatically determines the operating mode depending on the
1433 * given rate. `flags' indicates whether short preamble is in use or not.
1436 rt2661_txtime(int len, int rate, uint32_t flags)
1440 if (RAL_RATE_IS_OFDM(rate)) {
1441 /* IEEE Std 802.11a-1999, pp. 37 */
1442 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1443 txtime = 16 + 4 + 4 * txtime + 6;
1445 /* IEEE Std 802.11b-1999, pp. 28 */
1446 txtime = (16 * len + rate - 1) / rate;
1447 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1457 rt2661_plcp_signal(int rate)
1460 /* CCK rates (returned values are device-dependent) */
1463 case 11: return 0x2;
1464 case 22: return 0x3;
1466 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1467 case 12: return 0xb;
1468 case 18: return 0xf;
1469 case 24: return 0xa;
1470 case 36: return 0xe;
1471 case 48: return 0x9;
1472 case 72: return 0xd;
1473 case 96: return 0x8;
1474 case 108: return 0xc;
1476 /* unsupported rates (should not get there) */
1477 default: return 0xff;
1482 rt2661_setup_tx_desc(struct rt2661_softc *sc, struct rt2661_tx_desc *desc,
1483 uint32_t flags, uint16_t xflags, int len, int rate,
1484 const bus_dma_segment_t *segs, int nsegs, int ac, int ratectl)
1486 struct ieee80211com *ic = &sc->sc_ic;
1487 uint16_t plcp_length;
1490 desc->flags = htole32(flags);
1491 desc->flags |= htole32(len << 16);
1492 desc->flags |= htole32(RT2661_TX_VALID);
1494 desc->xflags = htole16(xflags);
1495 desc->xflags |= htole16(nsegs << 13);
1497 desc->wme = htole16(
1500 RT2661_LOGCWMIN(4) |
1501 RT2661_LOGCWMAX(10));
1504 * Remember whether TX rate control information should be gathered.
1505 * This field is driver private data only. It will be made available
1506 * by the NIC in STA_CSR4 on Tx done interrupts.
1508 desc->qid = ratectl;
1510 /* setup PLCP fields */
1511 desc->plcp_signal = rt2661_plcp_signal(rate);
1512 desc->plcp_service = 4;
1514 len += IEEE80211_CRC_LEN;
1515 if (RAL_RATE_IS_OFDM(rate)) {
1516 desc->flags |= htole32(RT2661_TX_OFDM);
1518 plcp_length = len & 0xfff;
1519 desc->plcp_length_hi = plcp_length >> 6;
1520 desc->plcp_length_lo = plcp_length & 0x3f;
1522 plcp_length = (16 * len + rate - 1) / rate;
1524 remainder = (16 * len) % 22;
1525 if (remainder != 0 && remainder < 7)
1526 desc->plcp_service |= RT2661_PLCP_LENGEXT;
1528 desc->plcp_length_hi = plcp_length >> 8;
1529 desc->plcp_length_lo = plcp_length & 0xff;
1531 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1532 desc->plcp_signal |= 0x08;
1535 /* RT2x61 supports scatter with up to 5 segments */
1536 for (i = 0; i < nsegs; i++) {
1537 desc->addr[i] = htole32(segs[i].ds_addr);
1538 desc->len [i] = htole16(segs[i].ds_len);
1541 desc->flags |= htole32(RT2661_TX_BUSY);
1545 rt2661_tx_mgt(struct rt2661_softc *sc, struct mbuf *m0,
1546 struct ieee80211_node *ni)
1548 struct ieee80211com *ic = &sc->sc_ic;
1549 struct rt2661_tx_desc *desc;
1550 struct rt2661_data *data;
1551 struct ieee80211_frame *wh;
1552 struct rt2661_dmamap map;
1554 uint32_t flags = 0; /* XXX HWSEQ */
1557 desc = &sc->mgtq.desc[sc->mgtq.cur];
1558 data = &sc->mgtq.data[sc->mgtq.cur];
1560 /* send mgt frames at the lowest available rate */
1561 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1563 error = bus_dmamap_load_mbuf(sc->mgtq.data_dmat, data->map, m0,
1564 rt2661_dma_map_mbuf, &map, 0);
1566 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1572 if (sc->sc_drvbpf != NULL) {
1573 struct rt2661_tx_radiotap_header *tap = &sc->sc_txtap;
1576 tap->wt_rate = rate;
1577 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1578 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1580 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1585 wh = mtod(m0, struct ieee80211_frame *);
1587 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1588 flags |= RT2661_TX_NEED_ACK;
1590 dur = rt2661_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) +
1592 *(uint16_t *)wh->i_dur = htole16(dur);
1594 /* tell hardware to add timestamp in probe responses */
1596 (IEEE80211_FC0_TYPE_MASK | IEEE80211_FC0_SUBTYPE_MASK)) ==
1597 (IEEE80211_FC0_TYPE_MGT | IEEE80211_FC0_SUBTYPE_PROBE_RESP))
1598 flags |= RT2661_TX_TIMESTAMP;
1601 rt2661_setup_tx_desc(sc, desc, flags, 0 /* XXX HWSEQ */,
1602 m0->m_pkthdr.len, rate, map.segs, map.nseg, RT2661_QID_MGT, 0);
1604 bus_dmamap_sync(sc->mgtq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1605 bus_dmamap_sync(sc->mgtq.desc_dmat, sc->mgtq.desc_map,
1606 BUS_DMASYNC_PREWRITE);
1608 DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n",
1609 m0->m_pkthdr.len, sc->mgtq.cur, rate));
1613 sc->mgtq.cur = (sc->mgtq.cur + 1) % RT2661_MGT_RING_COUNT;
1614 RAL_WRITE(sc, RT2661_TX_CNTL_CSR, RT2661_KICK_MGT);
1616 ieee80211_free_node(ni);
1622 * Build a RTS control frame.
1624 static struct mbuf *
1625 rt2661_get_rts(struct rt2661_softc *sc, struct ieee80211_frame *wh,
1628 struct ieee80211_frame_rts *rts;
1631 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1633 sc->sc_ic.ic_stats.is_tx_nobuf++;
1634 device_printf(sc->sc_dev, "could not allocate RTS frame\n");
1638 rts = mtod(m, struct ieee80211_frame_rts *);
1640 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1641 IEEE80211_FC0_SUBTYPE_RTS;
1642 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1643 *(uint16_t *)rts->i_dur = htole16(dur);
1644 IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1);
1645 IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2);
1647 m->m_pkthdr.len = m->m_len = sizeof (struct ieee80211_frame_rts);
1653 rt2661_tx_data(struct rt2661_softc *sc, struct mbuf *m0,
1654 struct ieee80211_node *ni, int ac)
1656 struct ieee80211com *ic = &sc->sc_ic;
1657 struct rt2661_tx_ring *txq = &sc->txq[ac];
1658 struct rt2661_tx_desc *desc;
1659 struct rt2661_data *data;
1660 struct rt2661_tx_ratectl *rctl;
1661 struct rt2661_node *rn;
1662 struct ieee80211_rateset *rs;
1663 struct ieee80211_frame *wh;
1664 struct ieee80211_key *k;
1665 const struct chanAccParams *cap;
1667 struct rt2661_dmamap map;
1670 int error, rate, noack = 0;
1672 wh = mtod(m0, struct ieee80211_frame *);
1674 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
1675 rs = &ic->ic_sup_rates[ic->ic_curmode];
1676 rate = rs->rs_rates[ic->ic_fixed_rate];
1679 rn = (struct rt2661_node *)ni;
1680 ni->ni_txrate = ral_rssadapt_choose(&rn->rssadapt, rs,
1681 wh, m0->m_pkthdr.len, NULL, 0);
1682 rate = rs->rs_rates[ni->ni_txrate];
1684 rate &= IEEE80211_RATE_VAL;
1686 if (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_QOS) {
1687 cap = &ic->ic_wme.wme_chanParams;
1688 noack = cap->cap_wmeParams[ac].wmep_noackPolicy;
1691 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1692 k = ieee80211_crypto_encap(ic, ni, m0);
1698 /* packet header may have moved, reset our local pointer */
1699 wh = mtod(m0, struct ieee80211_frame *);
1703 * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange
1704 * for directed frames only when the length of the MPDU is greater
1705 * than the length threshold indicated by [...]" ic_rtsthreshold.
1707 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1708 m0->m_pkthdr.len > ic->ic_rtsthreshold) {
1711 int rtsrate, ackrate;
1713 rtsrate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1714 ackrate = rt2661_ack_rate(ic, rate);
1716 dur = rt2661_txtime(m0->m_pkthdr.len + 4, rate, ic->ic_flags) +
1717 rt2661_txtime(RAL_CTS_SIZE, rtsrate, ic->ic_flags) +
1718 /* XXX: noack (QoS)? */
1719 rt2661_txtime(RAL_ACK_SIZE, ackrate, ic->ic_flags) +
1722 m = rt2661_get_rts(sc, wh, dur);
1724 desc = &txq->desc[txq->cur];
1725 data = &txq->data[txq->cur];
1727 error = bus_dmamap_load_mbuf(txq->data_dmat, data->map, m,
1728 rt2661_dma_map_mbuf, &map, 0);
1730 device_printf(sc->sc_dev,
1731 "could not map mbuf (error %d)\n", error);
1739 rt2661_setup_tx_desc(sc, desc, RT2661_TX_NEED_ACK |
1740 RT2661_TX_MORE_FRAG, 0, m->m_pkthdr.len,
1741 rtsrate, map.segs, map.nseg, ac, 0);
1743 bus_dmamap_sync(txq->data_dmat, data->map,
1744 BUS_DMASYNC_PREWRITE);
1747 txq->cur = (txq->cur + 1) % RT2661_TX_RING_COUNT;
1750 * IEEE Std 802.11-1999: when an RTS/CTS exchange is used, the
1751 * asynchronous data frame shall be transmitted after the CTS
1752 * frame and a SIFS period.
1754 flags |= RT2661_TX_LONG_RETRY | RT2661_TX_IFS;
1757 data = &txq->data[txq->cur];
1758 desc = &txq->desc[txq->cur];
1760 error = bus_dmamap_load_mbuf(txq->data_dmat, data->map, m0,
1761 rt2661_dma_map_mbuf, &map, 0);
1762 if (error != 0 && error != EFBIG) {
1763 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1769 mnew = m_defrag(m0, MB_DONTWAIT);
1771 device_printf(sc->sc_dev,
1772 "could not defragment mbuf\n");
1778 error = bus_dmamap_load_mbuf(txq->data_dmat, data->map, m0,
1779 rt2661_dma_map_mbuf, &map, 0);
1781 device_printf(sc->sc_dev,
1782 "could not map mbuf (error %d)\n", error);
1787 /* packet header have moved, reset our local pointer */
1788 wh = mtod(m0, struct ieee80211_frame *);
1791 if (sc->sc_drvbpf != NULL) {
1792 struct rt2661_tx_radiotap_header *tap = &sc->sc_txtap;
1795 tap->wt_rate = rate;
1796 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1797 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1799 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1804 rctl = kmalloc(sizeof(*rctl), M_RT2661, M_NOWAIT);
1808 /* remember link conditions for rate adaptation algorithm */
1809 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
1810 rctl->id.id_len = m0->m_pkthdr.len;
1811 rctl->id.id_rateidx = ni->ni_txrate;
1812 rctl->id.id_node = ni;
1813 rctl->id.id_rssi = ni->ni_rssi;
1815 rctl->id.id_node = NULL;
1817 STAILQ_INSERT_TAIL(&sc->tx_ratectl, rctl, link);
1820 if (!noack && !IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1821 flags |= RT2661_TX_NEED_ACK;
1823 dur = rt2661_txtime(RAL_ACK_SIZE, rt2661_ack_rate(ic, rate),
1824 ic->ic_flags) + RAL_SIFS;
1825 *(uint16_t *)wh->i_dur = htole16(dur);
1828 rt2661_setup_tx_desc(sc, desc, flags, 0, m0->m_pkthdr.len, rate,
1829 map.segs, map.nseg, ac, rctl != NULL);
1831 bus_dmamap_sync(txq->data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1832 bus_dmamap_sync(txq->desc_dmat, txq->desc_map, BUS_DMASYNC_PREWRITE);
1834 DPRINTFN(10, ("sending data frame len=%u idx=%u rate=%u\n",
1835 m0->m_pkthdr.len, txq->cur, rate));
1839 txq->cur = (txq->cur + 1) % RT2661_TX_RING_COUNT;
1840 RAL_WRITE(sc, RT2661_TX_CNTL_CSR, 1 << ac);
1843 ieee80211_free_node(ni);
1849 rt2661_start(struct ifnet *ifp)
1851 struct rt2661_softc *sc = ifp->if_softc;
1852 struct ieee80211com *ic = &sc->sc_ic;
1854 struct ether_header *eh;
1855 struct ieee80211_node *ni;
1858 /* prevent management frames from being sent if we're not ready */
1859 if (!(ifp->if_flags & IFF_RUNNING))
1863 IF_POLL(&ic->ic_mgtq, m0);
1865 if (sc->mgtq.queued >= RT2661_MGT_RING_COUNT) {
1866 ifp->if_flags |= IFF_OACTIVE;
1869 IF_DEQUEUE(&ic->ic_mgtq, m0);
1871 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1872 m0->m_pkthdr.rcvif = NULL;
1874 if (ic->ic_rawbpf != NULL)
1875 bpf_mtap(ic->ic_rawbpf, m0);
1877 if (rt2661_tx_mgt(sc, m0, ni) != 0)
1881 if (ic->ic_state != IEEE80211_S_RUN)
1884 m0 = ifq_dequeue(&ifp->if_snd, NULL);
1888 if (m0->m_len < sizeof (struct ether_header) &&
1889 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1892 eh = mtod(m0, struct ether_header *);
1893 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1900 /* classify mbuf so we can find which tx ring to use */
1901 if (ieee80211_classify(ic, m0, ni) != 0) {
1903 ieee80211_free_node(ni);
1908 /* no QoS encapsulation for EAPOL frames */
1909 ac = (eh->ether_type != htons(ETHERTYPE_PAE)) ?
1910 M_WME_GETAC(m0) : WME_AC_BE;
1912 if (sc->txq[ac].queued >= RT2661_TX_RING_COUNT - 1) {
1913 /* there is no place left in this ring */
1914 ifp->if_flags |= IFF_OACTIVE;
1916 ieee80211_free_node(ni);
1922 m0 = ieee80211_encap(ic, m0, ni);
1924 ieee80211_free_node(ni);
1929 if (ic->ic_rawbpf != NULL)
1930 bpf_mtap(ic->ic_rawbpf, m0);
1932 if (rt2661_tx_data(sc, m0, ni, ac) != 0) {
1933 ieee80211_free_node(ni);
1939 sc->sc_tx_timer = 5;
1945 rt2661_watchdog(struct ifnet *ifp)
1947 struct rt2661_softc *sc = ifp->if_softc;
1948 struct ieee80211com *ic = &sc->sc_ic;
1952 if (sc->sc_tx_timer > 0) {
1953 if (--sc->sc_tx_timer == 0) {
1954 device_printf(sc->sc_dev, "device timeout\n");
1962 ieee80211_watchdog(ic);
1966 * This function allows for fast channel switching in monitor mode (used by
1967 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
1968 * generate a new beacon frame.
1971 rt2661_reset(struct ifnet *ifp)
1973 struct rt2661_softc *sc = ifp->if_softc;
1974 struct ieee80211com *ic = &sc->sc_ic;
1976 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1979 rt2661_set_chan(sc, ic->ic_curchan);
1985 rt2661_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1987 struct rt2661_softc *sc = ifp->if_softc;
1988 struct ieee80211com *ic = &sc->sc_ic;
1993 if (ifp->if_flags & IFF_UP) {
1994 if (ifp->if_flags & IFF_RUNNING)
1995 rt2661_update_promisc(sc);
1999 if (ifp->if_flags & IFF_RUNNING)
2005 error = ieee80211_ioctl(ic, cmd, data, cr);
2008 if (error == ENETRESET) {
2009 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
2010 (IFF_UP | IFF_RUNNING) &&
2011 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
2019 rt2661_bbp_write(struct rt2661_softc *sc, uint8_t reg, uint8_t val)
2024 for (ntries = 0; ntries < 100; ntries++) {
2025 if (!(RAL_READ(sc, RT2661_PHY_CSR3) & RT2661_BBP_BUSY))
2029 if (ntries == 100) {
2030 device_printf(sc->sc_dev, "could not write to BBP\n");
2034 tmp = RT2661_BBP_BUSY | (reg & 0x7f) << 8 | val;
2035 RAL_WRITE(sc, RT2661_PHY_CSR3, tmp);
2037 DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg, val));
2041 rt2661_bbp_read(struct rt2661_softc *sc, uint8_t reg)
2046 for (ntries = 0; ntries < 100; ntries++) {
2047 if (!(RAL_READ(sc, RT2661_PHY_CSR3) & RT2661_BBP_BUSY))
2051 if (ntries == 100) {
2052 device_printf(sc->sc_dev, "could not read from BBP\n");
2056 val = RT2661_BBP_BUSY | RT2661_BBP_READ | reg << 8;
2057 RAL_WRITE(sc, RT2661_PHY_CSR3, val);
2059 for (ntries = 0; ntries < 100; ntries++) {
2060 val = RAL_READ(sc, RT2661_PHY_CSR3);
2061 if (!(val & RT2661_BBP_BUSY))
2066 device_printf(sc->sc_dev, "could not read from BBP\n");
2071 rt2661_rf_write(struct rt2661_softc *sc, uint8_t reg, uint32_t val)
2076 for (ntries = 0; ntries < 100; ntries++) {
2077 if (!(RAL_READ(sc, RT2661_PHY_CSR4) & RT2661_RF_BUSY))
2081 if (ntries == 100) {
2082 device_printf(sc->sc_dev, "could not write to RF\n");
2086 tmp = RT2661_RF_BUSY | RT2661_RF_21BIT | (val & 0x1fffff) << 2 |
2088 RAL_WRITE(sc, RT2661_PHY_CSR4, tmp);
2090 /* remember last written value in sc */
2091 sc->rf_regs[reg] = val;
2093 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 3, val & 0x1fffff));
2097 rt2661_tx_cmd(struct rt2661_softc *sc, uint8_t cmd, uint16_t arg)
2099 if (RAL_READ(sc, RT2661_H2M_MAILBOX_CSR) & RT2661_H2M_BUSY)
2100 return EIO; /* there is already a command pending */
2102 RAL_WRITE(sc, RT2661_H2M_MAILBOX_CSR,
2103 RT2661_H2M_BUSY | RT2661_TOKEN_NO_INTR << 16 | arg);
2105 RAL_WRITE(sc, RT2661_HOST_CMD_CSR, RT2661_KICK_CMD | cmd);
2111 rt2661_select_antenna(struct rt2661_softc *sc)
2113 uint8_t bbp4, bbp77;
2116 bbp4 = rt2661_bbp_read(sc, 4);
2117 bbp77 = rt2661_bbp_read(sc, 77);
2121 /* make sure Rx is disabled before switching antenna */
2122 tmp = RAL_READ(sc, RT2661_TXRX_CSR0);
2123 RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp | RT2661_DISABLE_RX);
2125 rt2661_bbp_write(sc, 4, bbp4);
2126 rt2661_bbp_write(sc, 77, bbp77);
2128 /* restore Rx filter */
2129 RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp);
2133 * Enable multi-rate retries for frames sent at OFDM rates.
2134 * In 802.11b/g mode, allow fallback to CCK rates.
2137 rt2661_enable_mrr(struct rt2661_softc *sc)
2139 struct ieee80211com *ic = &sc->sc_ic;
2142 tmp = RAL_READ(sc, RT2661_TXRX_CSR4);
2144 tmp &= ~RT2661_MRR_CCK_FALLBACK;
2145 if (!IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan))
2146 tmp |= RT2661_MRR_CCK_FALLBACK;
2147 tmp |= RT2661_MRR_ENABLED;
2149 RAL_WRITE(sc, RT2661_TXRX_CSR4, tmp);
2153 rt2661_set_txpreamble(struct rt2661_softc *sc)
2157 tmp = RAL_READ(sc, RT2661_TXRX_CSR4);
2159 tmp &= ~RT2661_SHORT_PREAMBLE;
2160 if (sc->sc_ic.ic_flags & IEEE80211_F_SHPREAMBLE)
2161 tmp |= RT2661_SHORT_PREAMBLE;
2163 RAL_WRITE(sc, RT2661_TXRX_CSR4, tmp);
2167 rt2661_set_basicrates(struct rt2661_softc *sc,
2168 const struct ieee80211_rateset *rs)
2170 #define RV(r) ((r) & IEEE80211_RATE_VAL)
2175 for (i = 0; i < rs->rs_nrates; i++) {
2176 rate = rs->rs_rates[i];
2178 if (!(rate & IEEE80211_RATE_BASIC))
2182 * Find h/w rate index. We know it exists because the rate
2183 * set has already been negotiated.
2185 for (j = 0; rt2661_rateset_11g.rs_rates[j] != RV(rate); j++);
2190 RAL_WRITE(sc, RT2661_TXRX_CSR5, mask);
2192 DPRINTF(("Setting basic rate mask to 0x%x\n", mask));
2197 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
2201 rt2661_select_band(struct rt2661_softc *sc, struct ieee80211_channel *c)
2203 uint8_t bbp17, bbp35, bbp96, bbp97, bbp98, bbp104;
2206 /* update all BBP registers that depend on the band */
2207 bbp17 = 0x20; bbp96 = 0x48; bbp104 = 0x2c;
2208 bbp35 = 0x50; bbp97 = 0x48; bbp98 = 0x48;
2209 if (IEEE80211_IS_CHAN_5GHZ(c)) {
2210 bbp17 += 0x08; bbp96 += 0x10; bbp104 += 0x0c;
2211 bbp35 += 0x10; bbp97 += 0x10; bbp98 += 0x10;
2213 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2214 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2215 bbp17 += 0x10; bbp96 += 0x10; bbp104 += 0x10;
2218 rt2661_bbp_write(sc, 17, bbp17);
2219 rt2661_bbp_write(sc, 96, bbp96);
2220 rt2661_bbp_write(sc, 104, bbp104);
2222 if ((IEEE80211_IS_CHAN_2GHZ(c) && sc->ext_2ghz_lna) ||
2223 (IEEE80211_IS_CHAN_5GHZ(c) && sc->ext_5ghz_lna)) {
2224 rt2661_bbp_write(sc, 75, 0x80);
2225 rt2661_bbp_write(sc, 86, 0x80);
2226 rt2661_bbp_write(sc, 88, 0x80);
2229 rt2661_bbp_write(sc, 35, bbp35);
2230 rt2661_bbp_write(sc, 97, bbp97);
2231 rt2661_bbp_write(sc, 98, bbp98);
2233 tmp = RAL_READ(sc, RT2661_PHY_CSR0);
2234 tmp &= ~(RT2661_PA_PE_2GHZ | RT2661_PA_PE_5GHZ);
2235 if (IEEE80211_IS_CHAN_2GHZ(c))
2236 tmp |= RT2661_PA_PE_2GHZ;
2238 tmp |= RT2661_PA_PE_5GHZ;
2239 RAL_WRITE(sc, RT2661_PHY_CSR0, tmp);
2243 rt2661_set_chan(struct rt2661_softc *sc, struct ieee80211_channel *c)
2245 struct ieee80211com *ic = &sc->sc_ic;
2246 const struct rfprog *rfprog;
2247 uint8_t bbp3, bbp94 = RT2661_BBPR94_DEFAULT;
2251 chan = ieee80211_chan2ieee(ic, c);
2252 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2255 /* select the appropriate RF settings based on what EEPROM says */
2256 rfprog = (sc->rfprog == 0) ? rt2661_rf5225_1 : rt2661_rf5225_2;
2258 /* find the settings for this channel (we know it exists) */
2259 for (i = 0; rfprog[i].chan != chan; i++);
2261 power = sc->txpow[i];
2265 } else if (power > 31) {
2266 bbp94 += power - 31;
2271 * If we are switching from the 2GHz band to the 5GHz band or
2272 * vice-versa, BBP registers need to be reprogrammed.
2274 if (c->ic_flags != sc->sc_curchan->ic_flags) {
2275 rt2661_select_band(sc, c);
2276 rt2661_select_antenna(sc);
2280 rt2661_rf_write(sc, RAL_RF1, rfprog[i].r1);
2281 rt2661_rf_write(sc, RAL_RF2, rfprog[i].r2);
2282 rt2661_rf_write(sc, RAL_RF3, rfprog[i].r3 | power << 7);
2283 rt2661_rf_write(sc, RAL_RF4, rfprog[i].r4 | sc->rffreq << 10);
2287 rt2661_rf_write(sc, RAL_RF1, rfprog[i].r1);
2288 rt2661_rf_write(sc, RAL_RF2, rfprog[i].r2);
2289 rt2661_rf_write(sc, RAL_RF3, rfprog[i].r3 | power << 7 | 1);
2290 rt2661_rf_write(sc, RAL_RF4, rfprog[i].r4 | sc->rffreq << 10);
2294 rt2661_rf_write(sc, RAL_RF1, rfprog[i].r1);
2295 rt2661_rf_write(sc, RAL_RF2, rfprog[i].r2);
2296 rt2661_rf_write(sc, RAL_RF3, rfprog[i].r3 | power << 7);
2297 rt2661_rf_write(sc, RAL_RF4, rfprog[i].r4 | sc->rffreq << 10);
2299 /* enable smart mode for MIMO-capable RFs */
2300 bbp3 = rt2661_bbp_read(sc, 3);
2302 bbp3 &= ~RT2661_SMART_MODE;
2303 if (sc->rf_rev == RT2661_RF_5325 || sc->rf_rev == RT2661_RF_2529)
2304 bbp3 |= RT2661_SMART_MODE;
2306 rt2661_bbp_write(sc, 3, bbp3);
2308 if (bbp94 != RT2661_BBPR94_DEFAULT)
2309 rt2661_bbp_write(sc, 94, bbp94);
2311 /* 5GHz radio needs a 1ms delay here */
2312 if (IEEE80211_IS_CHAN_5GHZ(c))
2317 rt2661_set_bssid(struct rt2661_softc *sc, const uint8_t *bssid)
2321 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2322 RAL_WRITE(sc, RT2661_MAC_CSR4, tmp);
2324 tmp = bssid[4] | bssid[5] << 8 | RT2661_ONE_BSSID << 16;
2325 RAL_WRITE(sc, RT2661_MAC_CSR5, tmp);
2329 rt2661_set_macaddr(struct rt2661_softc *sc, const uint8_t *addr)
2333 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2334 RAL_WRITE(sc, RT2661_MAC_CSR2, tmp);
2336 tmp = addr[4] | addr[5] << 8;
2337 RAL_WRITE(sc, RT2661_MAC_CSR3, tmp);
2341 rt2661_update_promisc(struct rt2661_softc *sc)
2343 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2346 tmp = RAL_READ(sc, RT2661_TXRX_CSR0);
2348 tmp &= ~RT2661_DROP_NOT_TO_ME;
2349 if (!(ifp->if_flags & IFF_PROMISC))
2350 tmp |= RT2661_DROP_NOT_TO_ME;
2352 RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp);
2354 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2355 "entering" : "leaving"));
2359 * Update QoS (802.11e) settings for each h/w Tx ring.
2362 rt2661_wme_update(struct ieee80211com *ic)
2364 struct rt2661_softc *sc = ic->ic_ifp->if_softc;
2365 const struct wmeParams *wmep;
2367 wmep = ic->ic_wme.wme_chanParams.cap_wmeParams;
2369 /* XXX: not sure about shifts. */
2370 /* XXX: the reference driver plays with AC_VI settings too. */
2373 RAL_WRITE(sc, RT2661_AC_TXOP_CSR0,
2374 wmep[WME_AC_BE].wmep_txopLimit << 16 |
2375 wmep[WME_AC_BK].wmep_txopLimit);
2376 RAL_WRITE(sc, RT2661_AC_TXOP_CSR1,
2377 wmep[WME_AC_VI].wmep_txopLimit << 16 |
2378 wmep[WME_AC_VO].wmep_txopLimit);
2381 RAL_WRITE(sc, RT2661_CWMIN_CSR,
2382 wmep[WME_AC_BE].wmep_logcwmin << 12 |
2383 wmep[WME_AC_BK].wmep_logcwmin << 8 |
2384 wmep[WME_AC_VI].wmep_logcwmin << 4 |
2385 wmep[WME_AC_VO].wmep_logcwmin);
2388 RAL_WRITE(sc, RT2661_CWMAX_CSR,
2389 wmep[WME_AC_BE].wmep_logcwmax << 12 |
2390 wmep[WME_AC_BK].wmep_logcwmax << 8 |
2391 wmep[WME_AC_VI].wmep_logcwmax << 4 |
2392 wmep[WME_AC_VO].wmep_logcwmax);
2395 RAL_WRITE(sc, RT2661_AIFSN_CSR,
2396 wmep[WME_AC_BE].wmep_aifsn << 12 |
2397 wmep[WME_AC_BK].wmep_aifsn << 8 |
2398 wmep[WME_AC_VI].wmep_aifsn << 4 |
2399 wmep[WME_AC_VO].wmep_aifsn);
2405 rt2661_update_slot(struct ifnet *ifp)
2407 struct rt2661_softc *sc = ifp->if_softc;
2408 struct ieee80211com *ic = &sc->sc_ic;
2412 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2414 tmp = RAL_READ(sc, RT2661_MAC_CSR9);
2415 tmp = (tmp & ~0xff) | slottime;
2416 RAL_WRITE(sc, RT2661_MAC_CSR9, tmp);
2420 rt2661_get_rf(int rev)
2423 case RT2661_RF_5225: return "RT5225";
2424 case RT2661_RF_5325: return "RT5325 (MIMO XR)";
2425 case RT2661_RF_2527: return "RT2527";
2426 case RT2661_RF_2529: return "RT2529 (MIMO XR)";
2427 default: return "unknown";
2432 rt2661_read_eeprom(struct rt2661_softc *sc)
2434 struct ieee80211com *ic = &sc->sc_ic;
2438 /* read MAC address */
2439 val = rt2661_eeprom_read(sc, RT2661_EEPROM_MAC01);
2440 ic->ic_myaddr[0] = val & 0xff;
2441 ic->ic_myaddr[1] = val >> 8;
2443 val = rt2661_eeprom_read(sc, RT2661_EEPROM_MAC23);
2444 ic->ic_myaddr[2] = val & 0xff;
2445 ic->ic_myaddr[3] = val >> 8;
2447 val = rt2661_eeprom_read(sc, RT2661_EEPROM_MAC45);
2448 ic->ic_myaddr[4] = val & 0xff;
2449 ic->ic_myaddr[5] = val >> 8;
2451 val = rt2661_eeprom_read(sc, RT2661_EEPROM_ANTENNA);
2452 /* XXX: test if different from 0xffff? */
2453 sc->rf_rev = (val >> 11) & 0x1f;
2454 sc->hw_radio = (val >> 10) & 0x1;
2455 sc->rx_ant = (val >> 4) & 0x3;
2456 sc->tx_ant = (val >> 2) & 0x3;
2457 sc->nb_ant = val & 0x3;
2459 DPRINTF(("RF revision=%d\n", sc->rf_rev));
2461 val = rt2661_eeprom_read(sc, RT2661_EEPROM_CONFIG2);
2462 sc->ext_5ghz_lna = (val >> 6) & 0x1;
2463 sc->ext_2ghz_lna = (val >> 4) & 0x1;
2465 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
2466 sc->ext_2ghz_lna, sc->ext_5ghz_lna));
2468 val = rt2661_eeprom_read(sc, RT2661_EEPROM_RSSI_2GHZ_OFFSET);
2469 if ((val & 0xff) != 0xff)
2470 sc->rssi_2ghz_corr = (int8_t)(val & 0xff); /* signed */
2472 /* Only [-10, 10] is valid */
2473 if (sc->rssi_2ghz_corr < -10 || sc->rssi_2ghz_corr > 10)
2474 sc->rssi_2ghz_corr = 0;
2476 val = rt2661_eeprom_read(sc, RT2661_EEPROM_RSSI_5GHZ_OFFSET);
2477 if ((val & 0xff) != 0xff)
2478 sc->rssi_5ghz_corr = (int8_t)(val & 0xff); /* signed */
2480 /* Only [-10, 10] is valid */
2481 if (sc->rssi_5ghz_corr < -10 || sc->rssi_5ghz_corr > 10)
2482 sc->rssi_5ghz_corr = 0;
2484 /* adjust RSSI correction for external low-noise amplifier */
2485 if (sc->ext_2ghz_lna)
2486 sc->rssi_2ghz_corr -= 14;
2487 if (sc->ext_5ghz_lna)
2488 sc->rssi_5ghz_corr -= 14;
2490 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2491 sc->rssi_2ghz_corr, sc->rssi_5ghz_corr));
2493 val = rt2661_eeprom_read(sc, RT2661_EEPROM_FREQ_OFFSET);
2494 if ((val >> 8) != 0xff)
2495 sc->rfprog = (val >> 8) & 0x3;
2496 if ((val & 0xff) != 0xff)
2497 sc->rffreq = val & 0xff;
2499 DPRINTF(("RF prog=%d\nRF freq=%d\n", sc->rfprog, sc->rffreq));
2501 /* read Tx power for all a/b/g channels */
2502 for (i = 0; i < 19; i++) {
2503 val = rt2661_eeprom_read(sc, RT2661_EEPROM_TXPOWER + i);
2504 sc->txpow[i * 2] = (int8_t)(val >> 8); /* signed */
2505 DPRINTF(("Channel=%d Tx power=%d\n",
2506 rt2661_rf5225_1[i * 2].chan, sc->txpow[i * 2]));
2507 sc->txpow[i * 2 + 1] = (int8_t)(val & 0xff); /* signed */
2508 DPRINTF(("Channel=%d Tx power=%d\n",
2509 rt2661_rf5225_1[i * 2 + 1].chan, sc->txpow[i * 2 + 1]));
2512 /* read vendor-specific BBP values */
2513 for (i = 0; i < 16; i++) {
2514 val = rt2661_eeprom_read(sc, RT2661_EEPROM_BBP_BASE + i);
2515 if (val == 0 || val == 0xffff)
2516 continue; /* skip invalid entries */
2517 sc->bbp_prom[i].reg = val >> 8;
2518 sc->bbp_prom[i].val = val & 0xff;
2519 DPRINTF(("BBP R%d=%02x\n", sc->bbp_prom[i].reg,
2520 sc->bbp_prom[i].val));
2523 val = rt2661_eeprom_read(sc, RT2661_EEPROM_LED_OFFSET);
2524 DPRINTF(("LED %02x\n", val));
2525 if (val == 0xffff) {
2526 sc->mcu_led = RT2661_MCU_LED_DEFAULT;
2528 #define N(arr) (int)(sizeof(arr) / sizeof(arr[0]))
2530 for (i = 0; i < N(led_ee2mcu); ++i) {
2531 if (val & led_ee2mcu[i].ee_bit)
2532 sc->mcu_led |= led_ee2mcu[i].mcu_bit;
2537 sc->mcu_led |= ((val >> RT2661_EE_LED_MODE_SHIFT) &
2538 RT2661_EE_LED_MODE_MASK);
2543 rt2661_bbp_init(struct rt2661_softc *sc)
2545 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2549 /* wait for BBP to be ready */
2550 for (ntries = 0; ntries < 100; ntries++) {
2551 val = rt2661_bbp_read(sc, 0);
2552 if (val != 0 && val != 0xff)
2556 if (ntries == 100) {
2557 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2561 /* initialize BBP registers to default values */
2562 for (i = 0; i < N(rt2661_def_bbp); i++) {
2563 rt2661_bbp_write(sc, rt2661_def_bbp[i].reg,
2564 rt2661_def_bbp[i].val);
2567 /* write vendor-specific BBP values (from EEPROM) */
2568 for (i = 0; i < 16; i++) {
2569 if (sc->bbp_prom[i].reg == 0)
2571 rt2661_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2579 rt2661_init(void *priv)
2581 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2582 struct rt2661_softc *sc = priv;
2583 struct ieee80211com *ic = &sc->sc_ic;
2584 struct ifnet *ifp = ic->ic_ifp;
2585 uint32_t tmp, sta[3];
2590 /* initialize Tx rings */
2591 RAL_WRITE(sc, RT2661_AC1_BASE_CSR, sc->txq[1].physaddr);
2592 RAL_WRITE(sc, RT2661_AC0_BASE_CSR, sc->txq[0].physaddr);
2593 RAL_WRITE(sc, RT2661_AC2_BASE_CSR, sc->txq[2].physaddr);
2594 RAL_WRITE(sc, RT2661_AC3_BASE_CSR, sc->txq[3].physaddr);
2596 /* initialize Mgt ring */
2597 RAL_WRITE(sc, RT2661_MGT_BASE_CSR, sc->mgtq.physaddr);
2599 /* initialize Rx ring */
2600 RAL_WRITE(sc, RT2661_RX_BASE_CSR, sc->rxq.physaddr);
2602 /* initialize Tx rings sizes */
2603 RAL_WRITE(sc, RT2661_TX_RING_CSR0,
2604 RT2661_TX_RING_COUNT << 24 |
2605 RT2661_TX_RING_COUNT << 16 |
2606 RT2661_TX_RING_COUNT << 8 |
2607 RT2661_TX_RING_COUNT);
2609 RAL_WRITE(sc, RT2661_TX_RING_CSR1,
2610 RT2661_TX_DESC_WSIZE << 16 |
2611 RT2661_TX_RING_COUNT << 8 | /* XXX: HCCA ring unused */
2612 RT2661_MGT_RING_COUNT);
2614 /* initialize Rx rings */
2615 RAL_WRITE(sc, RT2661_RX_RING_CSR,
2616 RT2661_RX_DESC_BACK << 16 |
2617 RT2661_RX_DESC_WSIZE << 8 |
2618 RT2661_RX_RING_COUNT);
2620 /* XXX: some magic here */
2621 RAL_WRITE(sc, RT2661_TX_DMA_DST_CSR, 0xaa);
2623 /* load base addresses of all 5 Tx rings (4 data + 1 mgt) */
2624 RAL_WRITE(sc, RT2661_LOAD_TX_RING_CSR, 0x1f);
2626 /* load base address of Rx ring */
2627 RAL_WRITE(sc, RT2661_RX_CNTL_CSR, 2);
2629 /* initialize MAC registers to default values */
2630 for (i = 0; i < N(rt2661_def_mac); i++)
2631 RAL_WRITE(sc, rt2661_def_mac[i].reg, rt2661_def_mac[i].val);
2633 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2634 rt2661_set_macaddr(sc, ic->ic_myaddr);
2636 /* set host ready */
2637 RAL_WRITE(sc, RT2661_MAC_CSR1, 3);
2638 RAL_WRITE(sc, RT2661_MAC_CSR1, 0);
2640 /* wait for BBP/RF to wakeup */
2641 for (ntries = 0; ntries < 1000; ntries++) {
2642 if (RAL_READ(sc, RT2661_MAC_CSR12) & 8)
2646 if (ntries == 1000) {
2647 kprintf("timeout waiting for BBP/RF to wakeup\n");
2652 if (rt2661_bbp_init(sc) != 0) {
2657 /* select default channel */
2658 sc->sc_curchan = ic->ic_curchan;
2659 rt2661_select_band(sc, sc->sc_curchan);
2660 rt2661_select_antenna(sc);
2661 rt2661_set_chan(sc, sc->sc_curchan);
2663 /* update Rx filter */
2664 tmp = RAL_READ(sc, RT2661_TXRX_CSR0) & 0xffff;
2666 tmp |= RT2661_DROP_PHY_ERROR | RT2661_DROP_CRC_ERROR;
2667 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2668 tmp |= RT2661_DROP_CTL | RT2661_DROP_VER_ERROR |
2670 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2671 tmp |= RT2661_DROP_TODS;
2672 if (!(ifp->if_flags & IFF_PROMISC))
2673 tmp |= RT2661_DROP_NOT_TO_ME;
2676 RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp);
2678 /* clear STA registers */
2679 RAL_READ_REGION_4(sc, RT2661_STA_CSR0, sta, N(sta));
2681 /* initialize ASIC */
2682 RAL_WRITE(sc, RT2661_MAC_CSR1, 4);
2684 /* clear any pending interrupt */
2685 RAL_WRITE(sc, RT2661_INT_SOURCE_CSR, 0xffffffff);
2687 /* enable interrupts */
2688 RAL_WRITE(sc, RT2661_INT_MASK_CSR, 0x0000ff10);
2689 RAL_WRITE(sc, RT2661_MCU_INT_MASK_CSR, 0);
2692 RAL_WRITE(sc, RT2661_RX_CNTL_CSR, 1);
2694 ifp->if_flags &= ~IFF_OACTIVE;
2695 ifp->if_flags |= IFF_RUNNING;
2697 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2698 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2699 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2701 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2706 rt2661_stop(void *priv)
2708 struct rt2661_softc *sc = priv;
2709 struct ieee80211com *ic = &sc->sc_ic;
2710 struct ifnet *ifp = ic->ic_ifp;
2711 struct rt2661_tx_ratectl *rctl;
2714 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2716 sc->sc_tx_timer = 0;
2718 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2720 /* abort Tx (for all 5 Tx rings) */
2721 RAL_WRITE(sc, RT2661_TX_CNTL_CSR, 0x1f << 16);
2723 /* disable Rx (value remains after reset!) */
2724 tmp = RAL_READ(sc, RT2661_TXRX_CSR0);
2725 RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp | RT2661_DISABLE_RX);
2728 RAL_WRITE(sc, RT2661_MAC_CSR1, 3);
2729 RAL_WRITE(sc, RT2661_MAC_CSR1, 0);
2731 /* disable interrupts */
2732 RAL_WRITE(sc, RT2661_INT_MASK_CSR, 0xffffffff);
2733 RAL_WRITE(sc, RT2661_MCU_INT_MASK_CSR, 0xffffffff);
2735 /* clear any pending interrupt */
2736 RAL_WRITE(sc, RT2661_INT_SOURCE_CSR, 0xffffffff);
2737 RAL_WRITE(sc, RT2661_MCU_INT_SOURCE_CSR, 0xffffffff);
2739 while ((rctl = STAILQ_FIRST(&sc->tx_ratectl)) != NULL) {
2740 STAILQ_REMOVE_HEAD(&sc->tx_ratectl, link);
2741 ieee80211_free_node(rctl->ni);
2743 kfree(rctl, M_RT2661);
2746 /* reset Tx and Rx rings */
2747 rt2661_reset_tx_ring(sc, &sc->txq[0]);
2748 rt2661_reset_tx_ring(sc, &sc->txq[1]);
2749 rt2661_reset_tx_ring(sc, &sc->txq[2]);
2750 rt2661_reset_tx_ring(sc, &sc->txq[3]);
2751 rt2661_reset_tx_ring(sc, &sc->mgtq);
2752 rt2661_reset_rx_ring(sc, &sc->rxq);
2756 rt2661_load_microcode(struct rt2661_softc *sc, const uint8_t *ucode, int size)
2761 RAL_WRITE(sc, RT2661_MCU_CNTL_CSR, RT2661_MCU_RESET);
2763 /* cancel any pending Host to MCU command */
2764 RAL_WRITE(sc, RT2661_H2M_MAILBOX_CSR, 0);
2765 RAL_WRITE(sc, RT2661_M2H_CMD_DONE_CSR, 0xffffffff);
2766 RAL_WRITE(sc, RT2661_HOST_CMD_CSR, 0);
2768 /* write 8051's microcode */
2769 RAL_WRITE(sc, RT2661_MCU_CNTL_CSR, RT2661_MCU_RESET | RT2661_MCU_SEL);
2770 RAL_WRITE_REGION_1(sc, RT2661_MCU_CODE_BASE, ucode, size);
2771 RAL_WRITE(sc, RT2661_MCU_CNTL_CSR, RT2661_MCU_RESET);
2773 /* kick 8051's ass */
2774 RAL_WRITE(sc, RT2661_MCU_CNTL_CSR, 0);
2776 /* wait for 8051 to initialize */
2777 for (ntries = 0; ntries < 500; ntries++) {
2778 if (RAL_READ(sc, RT2661_MCU_CNTL_CSR) & RT2661_MCU_READY)
2782 if (ntries == 500) {
2783 kprintf("timeout waiting for MCU to initialize\n");
2791 * Dynamically tune Rx sensitivity (BBP register 17) based on average RSSI and
2792 * false CCA count. This function is called periodically (every seconds) when
2793 * in the RUN state. Values taken from the reference driver.
2796 rt2661_rx_tune(struct rt2661_softc *sc)
2803 * Tuning range depends on operating band and on the presence of an
2804 * external low-noise amplifier.
2807 if (IEEE80211_IS_CHAN_5GHZ(sc->sc_curchan))
2809 if ((IEEE80211_IS_CHAN_2GHZ(sc->sc_curchan) && sc->ext_2ghz_lna) ||
2810 (IEEE80211_IS_CHAN_5GHZ(sc->sc_curchan) && sc->ext_5ghz_lna))
2814 /* retrieve false CCA count since last call (clear on read) */
2815 cca = RAL_READ(sc, RT2661_STA_CSR1) & 0xffff;
2819 } else if (dbm >= -58) {
2821 } else if (dbm >= -66) {
2823 } else if (dbm >= -74) {
2826 /* RSSI < -74dBm, tune using false CCA count */
2828 bbp17 = sc->bbp17; /* current value */
2830 hi -= 2 * (-74 - dbm);
2837 } else if (cca > 512) {
2840 } else if (cca < 100) {
2846 if (bbp17 != sc->bbp17) {
2847 rt2661_bbp_write(sc, 17, bbp17);
2853 * Enter/Leave radar detection mode.
2854 * This is for 802.11h additional regulatory domains.
2857 rt2661_radar_start(struct rt2661_softc *sc)
2862 tmp = RAL_READ(sc, RT2661_TXRX_CSR0);
2863 RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp | RT2661_DISABLE_RX);
2865 rt2661_bbp_write(sc, 82, 0x20);
2866 rt2661_bbp_write(sc, 83, 0x00);
2867 rt2661_bbp_write(sc, 84, 0x40);
2869 /* save current BBP registers values */
2870 sc->bbp18 = rt2661_bbp_read(sc, 18);
2871 sc->bbp21 = rt2661_bbp_read(sc, 21);
2872 sc->bbp22 = rt2661_bbp_read(sc, 22);
2873 sc->bbp16 = rt2661_bbp_read(sc, 16);
2874 sc->bbp17 = rt2661_bbp_read(sc, 17);
2875 sc->bbp64 = rt2661_bbp_read(sc, 64);
2877 rt2661_bbp_write(sc, 18, 0xff);
2878 rt2661_bbp_write(sc, 21, 0x3f);
2879 rt2661_bbp_write(sc, 22, 0x3f);
2880 rt2661_bbp_write(sc, 16, 0xbd);
2881 rt2661_bbp_write(sc, 17, sc->ext_5ghz_lna ? 0x44 : 0x34);
2882 rt2661_bbp_write(sc, 64, 0x21);
2884 /* restore Rx filter */
2885 RAL_WRITE(sc, RT2661_TXRX_CSR0, tmp);
2889 rt2661_radar_stop(struct rt2661_softc *sc)
2893 /* read radar detection result */
2894 bbp66 = rt2661_bbp_read(sc, 66);
2896 /* restore BBP registers values */
2897 rt2661_bbp_write(sc, 16, sc->bbp16);
2898 rt2661_bbp_write(sc, 17, sc->bbp17);
2899 rt2661_bbp_write(sc, 18, sc->bbp18);
2900 rt2661_bbp_write(sc, 21, sc->bbp21);
2901 rt2661_bbp_write(sc, 22, sc->bbp22);
2902 rt2661_bbp_write(sc, 64, sc->bbp64);
2909 rt2661_prepare_beacon(struct rt2661_softc *sc)
2911 struct ieee80211com *ic = &sc->sc_ic;
2912 struct ieee80211_beacon_offsets bo;
2913 struct rt2661_tx_desc desc;
2917 m0 = ieee80211_beacon_alloc(ic, ic->ic_bss, &bo);
2919 device_printf(sc->sc_dev, "could not allocate beacon frame\n");
2923 /* send beacons at the lowest available rate */
2924 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_bss->ni_chan) ? 12 : 2;
2926 rt2661_setup_tx_desc(sc, &desc, RT2661_TX_TIMESTAMP, RT2661_TX_HWSEQ,
2927 m0->m_pkthdr.len, rate, NULL, 0, RT2661_QID_MGT, 0);
2929 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2930 RAL_WRITE_REGION_1(sc, RT2661_HW_BEACON_BASE0, (uint8_t *)&desc, 24);
2932 /* copy beacon header and payload into NIC memory */
2933 RAL_WRITE_REGION_1(sc, RT2661_HW_BEACON_BASE0 + 24,
2934 mtod(m0, uint8_t *), m0->m_pkthdr.len);
2941 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
2942 * and HostAP operating modes.
2945 rt2661_enable_tsf_sync(struct rt2661_softc *sc)
2947 struct ieee80211com *ic = &sc->sc_ic;
2950 if (ic->ic_opmode != IEEE80211_M_STA) {
2952 * Change default 16ms TBTT adjustment to 8ms.
2953 * Must be done before enabling beacon generation.
2955 RAL_WRITE(sc, RT2661_TXRX_CSR10, 1 << 12 | 8);
2958 tmp = RAL_READ(sc, RT2661_TXRX_CSR9) & 0xff000000;
2960 /* set beacon interval (in 1/16ms unit) */
2961 tmp |= ic->ic_bss->ni_intval * 16;
2963 tmp |= RT2661_TSF_TICKING | RT2661_ENABLE_TBTT;
2964 if (ic->ic_opmode == IEEE80211_M_STA)
2965 tmp |= RT2661_TSF_MODE(1);
2967 tmp |= RT2661_TSF_MODE(2) | RT2661_GENERATE_BEACON;
2969 RAL_WRITE(sc, RT2661_TXRX_CSR9, tmp);
2973 * Retrieve the "Received Signal Strength Indicator" from the raw values
2974 * contained in Rx descriptors. The computation depends on which band the
2975 * frame was received. Correction values taken from the reference driver.
2978 rt2661_get_rssi(struct rt2661_softc *sc, uint8_t raw)
2982 lna = (raw >> 5) & 0x3;
2989 * NB: Since RSSI is relative to noise floor, -1 is
2990 * adequate for caller to know error happened.
2995 rssi = (2 * agc) - RT2661_NOISE_FLOOR;
2997 if (IEEE80211_IS_CHAN_2GHZ(sc->sc_curchan)) {
2998 rssi += sc->rssi_2ghz_corr;
3007 rssi += sc->rssi_5ghz_corr;
3020 rt2661_dma_map_mbuf(void *arg, bus_dma_segment_t *seg, int nseg,
3021 bus_size_t map_size __unused, int error)
3023 struct rt2661_dmamap *map = arg;
3028 KASSERT(nseg <= RT2661_MAX_SCATTER, ("too many DMA segments"));
3030 bcopy(seg, map->segs, nseg * sizeof(bus_dma_segment_t));
3035 rt2661_led_newstate(struct rt2661_softc *sc, enum ieee80211_state nstate)
3037 struct ieee80211com *ic = &sc->sc_ic;
3039 uint32_t mail = sc->mcu_led;
3041 if (RAL_READ(sc, RT2661_H2M_MAILBOX_CSR) & RT2661_H2M_BUSY) {
3042 DPRINTF(("%s failed\n", __func__));
3047 case IEEE80211_S_INIT:
3048 mail &= ~(RT2661_MCU_LED_LINKA | RT2661_MCU_LED_LINKG |
3052 if (ic->ic_curchan == NULL)
3055 on = RT2661_MCU_LED_LINKG;
3056 off = RT2661_MCU_LED_LINKA;
3057 if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
3058 on = RT2661_MCU_LED_LINKA;
3059 off = RT2661_MCU_LED_LINKG;
3062 mail |= RT2661_MCU_LED_RF | on;
3067 RAL_WRITE(sc, RT2661_H2M_MAILBOX_CSR,
3068 RT2661_H2M_BUSY | RT2661_TOKEN_NO_INTR << 16 | mail);
3069 RAL_WRITE(sc, RT2661_HOST_CMD_CSR, RT2661_KICK_CMD | RT2661_MCU_SET_LED);