2 * Copyright (c) 2005, 2006
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/rt2560.c,v 1.3 2006/03/21 21:15:43 damien Exp $
18 * $DragonFly: src/sys/dev/netif/ral/rt2560.c,v 1.13 2007/04/01 13:59:40 sephe Exp $
22 * Ralink Technology RT2560 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>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
36 #include <sys/sysctl.h>
37 #include <sys/serialize.h>
41 #include <net/if_arp.h>
42 #include <net/ethernet.h>
43 #include <net/if_dl.h>
44 #include <net/if_media.h>
45 #include <net/ifq_var.h>
47 #include <netproto/802_11/ieee80211_var.h>
48 #include <netproto/802_11/ieee80211_radiotap.h>
50 #include <dev/netif/ral/rt2560reg.h>
51 #include <dev/netif/ral/rt2560var.h>
53 #define RT2560_RSSI(sc, rssi) \
54 ((rssi) > (RT2560_NOISE_FLOOR + (sc)->rssi_corr) ? \
55 ((rssi) - RT2560_NOISE_FLOOR - (sc)->rssi_corr) : 0)
58 #define DPRINTF(x) do { if (ral_debug > 0) kprintf x; } while (0)
59 #define DPRINTFN(n, x) do { if (ral_debug >= (n)) kprintf x; } while (0)
63 #define DPRINTFN(n, x)
66 static void rt2560_dma_map_addr(void *, bus_dma_segment_t *, int,
68 static void rt2560_dma_map_mbuf(void *, bus_dma_segment_t *, int,
70 static int rt2560_alloc_tx_ring(struct rt2560_softc *,
71 struct rt2560_tx_ring *, int);
72 static void rt2560_reset_tx_ring(struct rt2560_softc *,
73 struct rt2560_tx_ring *);
74 static void rt2560_free_tx_ring(struct rt2560_softc *,
75 struct rt2560_tx_ring *);
76 static int rt2560_alloc_rx_ring(struct rt2560_softc *,
77 struct rt2560_rx_ring *, int);
78 static void rt2560_reset_rx_ring(struct rt2560_softc *,
79 struct rt2560_rx_ring *);
80 static void rt2560_free_rx_ring(struct rt2560_softc *,
81 struct rt2560_rx_ring *);
82 static int rt2560_media_change(struct ifnet *);
83 static void rt2560_next_scan(void *);
84 static int rt2560_newstate(struct ieee80211com *,
85 enum ieee80211_state, int);
86 static uint16_t rt2560_eeprom_read(struct rt2560_softc *, uint8_t);
87 static void rt2560_encryption_intr(struct rt2560_softc *);
88 static void rt2560_tx_intr(struct rt2560_softc *);
89 static void rt2560_prio_intr(struct rt2560_softc *);
90 static void rt2560_decryption_intr(struct rt2560_softc *);
91 static void rt2560_rx_intr(struct rt2560_softc *);
92 static void rt2560_beacon_expire(struct rt2560_softc *);
93 static void rt2560_wakeup_expire(struct rt2560_softc *);
94 static uint8_t rt2560_rxrate(struct rt2560_rx_desc *);
95 static uint8_t rt2560_plcp_signal(int);
96 static void rt2560_setup_tx_desc(struct rt2560_softc *,
97 struct rt2560_tx_desc *, uint32_t, int, int, int,
99 static int rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *,
100 struct ieee80211_node *);
101 static int rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *,
102 struct ieee80211_node *);
103 static struct mbuf *rt2560_get_rts(struct rt2560_softc *,
104 struct ieee80211_frame *, uint16_t);
105 static int rt2560_tx_data(struct rt2560_softc *, struct mbuf *,
106 struct ieee80211_node *);
107 static void rt2560_start(struct ifnet *);
108 static void rt2560_watchdog(struct ifnet *);
109 static int rt2560_reset(struct ifnet *);
110 static int rt2560_ioctl(struct ifnet *, u_long, caddr_t,
112 static void rt2560_bbp_write(struct rt2560_softc *, uint8_t,
114 static uint8_t rt2560_bbp_read(struct rt2560_softc *, uint8_t);
115 static void rt2560_rf_write(struct rt2560_softc *, uint8_t,
117 static void rt2560_set_chan(struct rt2560_softc *,
118 struct ieee80211_channel *);
120 static void rt2560_disable_rf_tune(struct rt2560_softc *);
122 static void rt2560_enable_tsf_sync(struct rt2560_softc *);
123 static void rt2560_update_plcp(struct rt2560_softc *);
124 static void rt2560_update_slot(struct ifnet *);
125 static void rt2560_set_basicrates(struct rt2560_softc *);
126 static void rt2560_update_led(struct rt2560_softc *, int, int);
127 static void rt2560_set_bssid(struct rt2560_softc *, uint8_t *);
128 static void rt2560_set_macaddr(struct rt2560_softc *, uint8_t *);
129 static void rt2560_get_macaddr(struct rt2560_softc *, uint8_t *);
130 static void rt2560_update_promisc(struct rt2560_softc *);
131 static const char *rt2560_get_rf(int);
132 static void rt2560_read_eeprom(struct rt2560_softc *);
133 static int rt2560_bbp_init(struct rt2560_softc *);
134 static void rt2560_set_txantenna(struct rt2560_softc *, int);
135 static void rt2560_set_rxantenna(struct rt2560_softc *, int);
136 static void rt2560_init(void *);
137 static void rt2560_stop(void *);
138 static void rt2560_intr(void *);
141 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
143 static const struct ieee80211_rateset rt2560_rateset_11a =
144 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
146 static const struct ieee80211_rateset rt2560_rateset_11b =
147 { 4, { 2, 4, 11, 22 } };
149 static const struct ieee80211_rateset rt2560_rateset_11g =
150 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
152 static const struct {
155 } rt2560_def_mac[] = {
159 static const struct {
162 } rt2560_def_bbp[] = {
166 static const uint32_t rt2560_rf2522_r2[] = RT2560_RF2522_R2;
167 static const uint32_t rt2560_rf2523_r2[] = RT2560_RF2523_R2;
168 static const uint32_t rt2560_rf2524_r2[] = RT2560_RF2524_R2;
169 static const uint32_t rt2560_rf2525_r2[] = RT2560_RF2525_R2;
170 static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2;
171 static const uint32_t rt2560_rf2525e_r2[] = RT2560_RF2525E_R2;
172 static const uint32_t rt2560_rf2526_r2[] = RT2560_RF2526_R2;
173 static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2;
175 static const struct {
178 } rt2560_rf5222[] = {
183 rt2560_attach(device_t dev, int id)
185 struct rt2560_softc *sc = device_get_softc(dev);
186 struct ieee80211com *ic = &sc->sc_ic;
187 struct ifnet *ifp = &ic->ic_if;
190 callout_init(&sc->scan_ch);
193 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irq_rid,
194 RF_ACTIVE | RF_SHAREABLE);
195 if (sc->sc_irq == NULL) {
196 device_printf(dev, "could not allocate interrupt resource\n");
200 /* retrieve RT2560 rev. no */
201 sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
203 /* retrieve MAC address */
204 rt2560_get_macaddr(sc, ic->ic_myaddr);
206 /* retrieve RF rev. no and various other things from EEPROM */
207 rt2560_read_eeprom(sc);
209 device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n",
210 sc->asic_rev, rt2560_get_rf(sc->rf_rev));
213 * Allocate Tx and Rx rings.
215 error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT);
217 device_printf(sc->sc_dev, "could not allocate Tx ring\n");
221 error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT);
223 device_printf(sc->sc_dev, "could not allocate ATIM ring\n");
227 error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT);
229 device_printf(sc->sc_dev, "could not allocate Prio ring\n");
233 error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT);
235 device_printf(sc->sc_dev, "could not allocate Beacon ring\n");
239 error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT);
241 device_printf(sc->sc_dev, "could not allocate Rx ring\n");
245 sysctl_ctx_init(&sc->sysctl_ctx);
246 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
247 SYSCTL_STATIC_CHILDREN(_hw),
249 device_get_nameunit(dev),
251 if (sc->sysctl_tree == NULL) {
252 device_printf(dev, "could not add sysctl node\n");
258 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
259 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
260 ifp->if_init = rt2560_init;
261 ifp->if_ioctl = rt2560_ioctl;
262 ifp->if_start = rt2560_start;
263 ifp->if_watchdog = rt2560_watchdog;
264 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
265 ifq_set_ready(&ifp->if_snd);
267 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
268 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
269 ic->ic_state = IEEE80211_S_INIT;
271 ic->ic_ratectl.rc_st_ratectl_cap = IEEE80211_RATECTL_CAP_ONOE |
272 IEEE80211_RATECTL_CAP_SAMPLE;
273 ic->ic_ratectl.rc_st_ratectl = IEEE80211_RATECTL_SAMPLE;
275 /* set device capabilities */
277 IEEE80211_C_IBSS | /* IBSS mode supported */
278 IEEE80211_C_MONITOR | /* monitor mode supported */
279 IEEE80211_C_HOSTAP | /* HostAp mode supported */
280 IEEE80211_C_TXPMGT | /* tx power management */
281 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
282 IEEE80211_C_SHSLOT | /* short slot time supported */
283 IEEE80211_C_WEP | /* WEP */
284 IEEE80211_C_WPA; /* 802.11i */
286 if (sc->rf_rev == RT2560_RF_5222) {
287 /* set supported .11a rates */
288 ic->ic_sup_rates[IEEE80211_MODE_11A] = rt2560_rateset_11a;
290 /* set supported .11a channels */
291 for (i = 36; i <= 64; i += 4) {
292 ic->ic_channels[i].ic_freq =
293 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
294 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
296 for (i = 100; i <= 140; i += 4) {
297 ic->ic_channels[i].ic_freq =
298 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
299 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
301 for (i = 149; i <= 161; i += 4) {
302 ic->ic_channels[i].ic_freq =
303 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
304 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
308 /* set supported .11b and .11g rates */
309 ic->ic_sup_rates[IEEE80211_MODE_11B] = rt2560_rateset_11b;
310 ic->ic_sup_rates[IEEE80211_MODE_11G] = rt2560_rateset_11g;
312 /* set supported .11b and .11g channels (1 through 14) */
313 for (i = 1; i <= 14; i++) {
314 ic->ic_channels[i].ic_freq =
315 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
316 ic->ic_channels[i].ic_flags =
317 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
318 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
321 sc->sc_sifs = IEEE80211_DUR_SIFS; /* Default SIFS */
323 ieee80211_ifattach(ic);
324 ic->ic_updateslot = rt2560_update_slot;
325 ic->ic_reset = rt2560_reset;
326 /* enable s/w bmiss handling in sta mode */
327 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
329 /* override state transition machine */
330 sc->sc_newstate = ic->ic_newstate;
331 ic->ic_newstate = rt2560_newstate;
332 ieee80211_media_init(ic, rt2560_media_change, ieee80211_media_status);
334 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
335 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
337 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
338 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
339 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2560_RX_RADIOTAP_PRESENT);
341 sc->sc_txtap_len = sizeof sc->sc_txtapu;
342 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
343 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2560_TX_RADIOTAP_PRESENT);
346 * Add a few sysctl knobs.
350 SYSCTL_ADD_INT(&sc->sysctl_ctx,
351 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
352 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)");
354 SYSCTL_ADD_INT(&sc->sysctl_ctx,
355 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
356 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)");
358 SYSCTL_ADD_INT(&sc->sysctl_ctx,
359 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "dwell",
360 CTLFLAG_RW, &sc->dwelltime, 0,
361 "channel dwell time (ms) for AP/station scanning");
363 error = bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE, rt2560_intr,
364 sc, &sc->sc_ih, ifp->if_serializer);
366 device_printf(dev, "could not set up interrupt\n");
368 ieee80211_ifdetach(ic);
373 ieee80211_announce(ic);
381 rt2560_detach(void *xsc)
383 struct rt2560_softc *sc = xsc;
384 struct ieee80211com *ic = &sc->sc_ic;
385 struct ifnet *ifp = ic->ic_ifp;
387 if (device_is_attached(sc->sc_dev)) {
388 lwkt_serialize_enter(ifp->if_serializer);
390 callout_stop(&sc->scan_ch);
393 bus_teardown_intr(sc->sc_dev, sc->sc_irq, sc->sc_ih);
395 lwkt_serialize_exit(ifp->if_serializer);
398 ieee80211_ifdetach(ic);
401 rt2560_free_tx_ring(sc, &sc->txq);
402 rt2560_free_tx_ring(sc, &sc->atimq);
403 rt2560_free_tx_ring(sc, &sc->prioq);
404 rt2560_free_tx_ring(sc, &sc->bcnq);
405 rt2560_free_rx_ring(sc, &sc->rxq);
407 if (sc->sc_irq != NULL) {
408 bus_release_resource(sc->sc_dev, SYS_RES_IRQ, sc->sc_irq_rid,
412 if (sc->sysctl_tree != NULL)
413 sysctl_ctx_free(&sc->sysctl_ctx);
419 rt2560_shutdown(void *xsc)
421 struct rt2560_softc *sc = xsc;
422 struct ifnet *ifp = &sc->sc_ic.ic_if;
424 lwkt_serialize_enter(ifp->if_serializer);
426 lwkt_serialize_exit(ifp->if_serializer);
430 rt2560_suspend(void *xsc)
432 struct rt2560_softc *sc = xsc;
433 struct ifnet *ifp = &sc->sc_ic.ic_if;
435 lwkt_serialize_enter(ifp->if_serializer);
437 lwkt_serialize_exit(ifp->if_serializer);
441 rt2560_resume(void *xsc)
443 struct rt2560_softc *sc = xsc;
444 struct ifnet *ifp = sc->sc_ic.ic_ifp;
446 lwkt_serialize_enter(ifp->if_serializer);
447 if (ifp->if_flags & IFF_UP) {
448 ifp->if_init(ifp->if_softc);
449 if (ifp->if_flags & IFF_RUNNING)
452 lwkt_serialize_exit(ifp->if_serializer);
456 rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
461 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
463 *(bus_addr_t *)arg = segs[0].ds_addr;
467 rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring,
474 ring->cur = ring->next = 0;
475 ring->cur_encrypt = ring->next_encrypt = 0;
477 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
478 BUS_SPACE_MAXADDR, NULL, NULL, count * RT2560_TX_DESC_SIZE, 1,
479 count * RT2560_TX_DESC_SIZE, 0, &ring->desc_dmat);
481 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
485 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
486 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
488 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
492 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
493 count * RT2560_TX_DESC_SIZE,
494 rt2560_dma_map_addr, &ring->physaddr, 0);
496 device_printf(sc->sc_dev, "could not load desc DMA map\n");
498 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
503 ring->data = kmalloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF,
506 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
507 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, RT2560_MAX_SCATTER,
508 MCLBYTES, 0, &ring->data_dmat);
510 device_printf(sc->sc_dev, "could not create data DMA tag\n");
514 for (i = 0; i < count; i++) {
515 error = bus_dmamap_create(ring->data_dmat, 0,
518 device_printf(sc->sc_dev, "could not create DMA map\n");
524 fail: rt2560_free_tx_ring(sc, ring);
529 rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
531 struct rt2560_tx_desc *desc;
532 struct rt2560_tx_data *data;
535 for (i = 0; i < ring->count; i++) {
536 desc = &ring->desc[i];
537 data = &ring->data[i];
539 if (data->m != NULL) {
540 bus_dmamap_sync(ring->data_dmat, data->map,
541 BUS_DMASYNC_POSTWRITE);
542 bus_dmamap_unload(ring->data_dmat, data->map);
547 if (data->ni != NULL) {
548 ieee80211_free_node(data->ni);
555 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
558 ring->cur = ring->next = 0;
559 ring->cur_encrypt = ring->next_encrypt = 0;
563 rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
565 struct rt2560_tx_data *data;
568 if (ring->desc != NULL) {
569 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
570 BUS_DMASYNC_POSTWRITE);
571 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
572 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
576 if (ring->desc_dmat != NULL) {
577 bus_dma_tag_destroy(ring->desc_dmat);
578 ring->desc_dmat = NULL;
581 if (ring->data != NULL) {
582 for (i = 0; i < ring->count; i++) {
583 data = &ring->data[i];
585 if (data->m != NULL) {
586 bus_dmamap_sync(ring->data_dmat, data->map,
587 BUS_DMASYNC_POSTWRITE);
588 bus_dmamap_unload(ring->data_dmat, data->map);
593 if (data->ni != NULL) {
594 ieee80211_free_node(data->ni);
598 if (data->map != NULL) {
599 bus_dmamap_destroy(ring->data_dmat, data->map);
604 kfree(ring->data, M_DEVBUF);
608 if (ring->data_dmat != NULL) {
609 bus_dma_tag_destroy(ring->data_dmat);
610 ring->data_dmat = NULL;
615 rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring,
618 struct rt2560_rx_desc *desc;
619 struct rt2560_rx_data *data;
624 ring->cur = ring->next = 0;
625 ring->cur_decrypt = 0;
627 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
628 BUS_SPACE_MAXADDR, NULL, NULL, count * RT2560_RX_DESC_SIZE, 1,
629 count * RT2560_RX_DESC_SIZE, 0, &ring->desc_dmat);
631 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
635 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
636 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
638 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
642 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
643 count * RT2560_RX_DESC_SIZE,
644 rt2560_dma_map_addr, &ring->physaddr, 0);
646 device_printf(sc->sc_dev, "could not load desc DMA map\n");
648 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
653 ring->data = kmalloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF,
657 * Pre-allocate Rx buffers and populate Rx ring.
659 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
660 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0,
663 device_printf(sc->sc_dev, "could not create data DMA tag\n");
667 for (i = 0; i < count; i++) {
668 desc = &sc->rxq.desc[i];
669 data = &sc->rxq.data[i];
671 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
673 device_printf(sc->sc_dev, "could not create DMA map\n");
677 data->m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
678 if (data->m == NULL) {
679 device_printf(sc->sc_dev,
680 "could not allocate rx mbuf\n");
685 error = bus_dmamap_load(ring->data_dmat, data->map,
686 mtod(data->m, void *), MCLBYTES, rt2560_dma_map_addr,
689 device_printf(sc->sc_dev,
690 "could not load rx buf DMA map");
697 desc->flags = htole32(RT2560_RX_BUSY);
698 desc->physaddr = htole32(physaddr);
701 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
705 fail: rt2560_free_rx_ring(sc, ring);
710 rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
714 for (i = 0; i < ring->count; i++) {
715 ring->desc[i].flags = htole32(RT2560_RX_BUSY);
716 ring->data[i].drop = 0;
719 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
721 ring->cur = ring->next = 0;
722 ring->cur_decrypt = 0;
726 rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
728 struct rt2560_rx_data *data;
730 if (ring->desc != NULL) {
731 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
732 BUS_DMASYNC_POSTWRITE);
733 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
734 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
738 if (ring->desc_dmat != NULL) {
739 bus_dma_tag_destroy(ring->desc_dmat);
740 ring->desc_dmat = NULL;
743 if (ring->data != NULL) {
746 for (i = 0; i < ring->count; i++) {
747 data = &ring->data[i];
749 if (data->m != NULL) {
750 bus_dmamap_sync(ring->data_dmat, data->map,
751 BUS_DMASYNC_POSTREAD);
752 bus_dmamap_unload(ring->data_dmat, data->map);
757 if (data->map != NULL) {
758 bus_dmamap_destroy(ring->data_dmat, data->map);
763 kfree(ring->data, M_DEVBUF);
767 if (ring->data_dmat != NULL) {
768 bus_dma_tag_destroy(ring->data_dmat);
769 ring->data_dmat = NULL;
774 rt2560_media_change(struct ifnet *ifp)
776 struct rt2560_softc *sc = ifp->if_softc;
779 error = ieee80211_media_change(ifp);
780 if (error != ENETRESET)
783 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
789 * This function is called periodically (every 200ms) during scanning to
790 * switch from one channel to another.
793 rt2560_next_scan(void *arg)
795 struct rt2560_softc *sc = arg;
796 struct ieee80211com *ic = &sc->sc_ic;
797 struct ifnet *ifp = ic->ic_ifp;
799 lwkt_serialize_enter(ifp->if_serializer);
800 if (ic->ic_state == IEEE80211_S_SCAN)
801 ieee80211_next_scan(ic);
802 lwkt_serialize_exit(ifp->if_serializer);
806 rt2560_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
808 struct rt2560_softc *sc = ic->ic_ifp->if_softc;
809 enum ieee80211_state ostate;
810 struct ieee80211_node *ni;
814 ostate = ic->ic_state;
815 callout_stop(&sc->scan_ch);
816 ieee80211_ratectl_newstate(ic, nstate);
819 case IEEE80211_S_INIT:
820 if (ostate == IEEE80211_S_RUN) {
821 /* abort TSF synchronization */
822 RAL_WRITE(sc, RT2560_CSR14, 0);
824 /* turn association led off */
825 rt2560_update_led(sc, 0, 0);
829 case IEEE80211_S_SCAN:
830 rt2560_set_chan(sc, ic->ic_curchan);
831 callout_reset(&sc->scan_ch, (sc->dwelltime * hz) / 1000,
832 rt2560_next_scan, sc);
835 case IEEE80211_S_AUTH:
836 rt2560_set_chan(sc, ic->ic_curchan);
839 case IEEE80211_S_ASSOC:
840 rt2560_set_chan(sc, ic->ic_curchan);
843 case IEEE80211_S_RUN:
844 rt2560_set_chan(sc, ic->ic_curchan);
848 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
849 rt2560_update_plcp(sc);
850 rt2560_set_basicrates(sc);
851 rt2560_set_bssid(sc, ni->ni_bssid);
854 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
855 ic->ic_opmode == IEEE80211_M_IBSS) {
856 m = ieee80211_beacon_alloc(ic, ni, &sc->sc_bo);
858 device_printf(sc->sc_dev,
859 "could not allocate beacon\n");
864 ieee80211_ref_node(ni);
865 error = rt2560_tx_bcn(sc, m, ni);
870 /* turn assocation led on */
871 rt2560_update_led(sc, 1, 0);
873 if (ic->ic_opmode != IEEE80211_M_MONITOR)
874 rt2560_enable_tsf_sync(sc);
878 return (error != 0) ? error : sc->sc_newstate(ic, nstate, arg);
882 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
886 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
892 /* clock C once before the first command */
893 RT2560_EEPROM_CTL(sc, 0);
895 RT2560_EEPROM_CTL(sc, RT2560_S);
896 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
897 RT2560_EEPROM_CTL(sc, RT2560_S);
899 /* write start bit (1) */
900 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
901 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
903 /* write READ opcode (10) */
904 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
905 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
906 RT2560_EEPROM_CTL(sc, RT2560_S);
907 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
909 /* write address (A5-A0 or A7-A0) */
910 n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
911 for (; n >= 0; n--) {
912 RT2560_EEPROM_CTL(sc, RT2560_S |
913 (((addr >> n) & 1) << RT2560_SHIFT_D));
914 RT2560_EEPROM_CTL(sc, RT2560_S |
915 (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
918 RT2560_EEPROM_CTL(sc, RT2560_S);
920 /* read data Q15-Q0 */
922 for (n = 15; n >= 0; n--) {
923 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
924 tmp = RAL_READ(sc, RT2560_CSR21);
925 val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
926 RT2560_EEPROM_CTL(sc, RT2560_S);
929 RT2560_EEPROM_CTL(sc, 0);
931 /* clear Chip Select and clock C */
932 RT2560_EEPROM_CTL(sc, RT2560_S);
933 RT2560_EEPROM_CTL(sc, 0);
934 RT2560_EEPROM_CTL(sc, RT2560_C);
940 * Some frames were processed by the hardware cipher engine and are ready for
944 rt2560_encryption_intr(struct rt2560_softc *sc)
946 struct rt2560_tx_desc *desc;
949 /* retrieve last descriptor index processed by cipher engine */
950 hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr;
951 hw /= RT2560_TX_DESC_SIZE;
953 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
954 BUS_DMASYNC_POSTREAD);
956 for (; sc->txq.next_encrypt != hw;) {
957 desc = &sc->txq.desc[sc->txq.next_encrypt];
959 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
960 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY))
963 /* for TKIP, swap eiv field to fix a bug in ASIC */
964 if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) ==
965 RT2560_TX_CIPHER_TKIP)
966 desc->eiv = bswap32(desc->eiv);
968 /* mark the frame ready for transmission */
969 desc->flags |= htole32(RT2560_TX_VALID);
970 desc->flags |= htole32(RT2560_TX_BUSY);
972 DPRINTFN(15, ("encryption done idx=%u\n",
973 sc->txq.next_encrypt));
975 sc->txq.next_encrypt =
976 (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
979 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
980 BUS_DMASYNC_PREWRITE);
983 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
987 rt2560_tx_intr(struct rt2560_softc *sc)
989 struct ieee80211com *ic = &sc->sc_ic;
990 struct ifnet *ifp = ic->ic_ifp;
992 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
993 BUS_DMASYNC_POSTREAD);
996 struct rt2560_tx_desc *desc;
997 struct rt2560_tx_data *data;
998 struct ieee80211_node *ni;
999 int rateidx, data_retries, failed;
1003 desc = &sc->txq.desc[sc->txq.next];
1004 data = &sc->txq.data[sc->txq.next];
1006 flags = le32toh(desc->flags);
1008 if ((flags & RT2560_TX_BUSY) ||
1009 (flags & RT2560_TX_CIPHER_BUSY) ||
1010 !(flags & RT2560_TX_VALID))
1013 rateidx = data->rateidx;
1021 switch (flags & RT2560_TX_RESULT_MASK) {
1022 case RT2560_TX_SUCCESS:
1023 DPRINTFN(10, ("data frame sent successfully\n"));
1028 case RT2560_TX_SUCCESS_RETRY:
1029 data_retries = (flags >> 5) & 0x7;
1030 DPRINTFN(9, ("data frame sent after %u retries\n",
1035 case RT2560_TX_FAIL_RETRY:
1036 DPRINTFN(9, ("sending data frame failed (too much "
1043 case RT2560_TX_FAIL_INVALID:
1044 case RT2560_TX_FAIL_OTHER:
1048 device_printf(sc->sc_dev, "sending data frame failed "
1054 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1055 BUS_DMASYNC_POSTWRITE);
1056 bus_dmamap_unload(sc->txq.data_dmat, data->map);
1059 struct ieee80211_ratectl_res res;
1061 res.rc_res_tries = data_retries + 1;
1062 res.rc_res_rateidx = rateidx;
1063 ieee80211_ratectl_tx_complete(ni, m->m_pkthdr.len,
1064 &res, 1, data_retries, 0, failed);
1068 ieee80211_free_node(ni);
1070 /* descriptor is no longer valid */
1071 desc->flags &= ~htole32(RT2560_TX_VALID);
1073 DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next));
1076 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
1079 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1080 BUS_DMASYNC_PREWRITE);
1082 sc->sc_tx_timer = 0;
1083 ifp->if_flags &= ~IFF_OACTIVE;
1088 rt2560_prio_intr(struct rt2560_softc *sc)
1090 struct ieee80211com *ic = &sc->sc_ic;
1091 struct ifnet *ifp = ic->ic_ifp;
1092 struct rt2560_tx_desc *desc;
1093 struct rt2560_tx_data *data;
1095 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1096 BUS_DMASYNC_POSTREAD);
1099 desc = &sc->prioq.desc[sc->prioq.next];
1100 data = &sc->prioq.data[sc->prioq.next];
1102 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
1103 !(le32toh(desc->flags) & RT2560_TX_VALID))
1106 switch (le32toh(desc->flags) & RT2560_TX_RESULT_MASK) {
1107 case RT2560_TX_SUCCESS:
1108 DPRINTFN(10, ("mgt frame sent successfully\n"));
1111 case RT2560_TX_SUCCESS_RETRY:
1112 DPRINTFN(9, ("mgt frame sent after %u retries\n",
1113 (le32toh(desc->flags) >> 5) & 0x7));
1116 case RT2560_TX_FAIL_RETRY:
1117 DPRINTFN(9, ("sending mgt frame failed (too much "
1121 case RT2560_TX_FAIL_INVALID:
1122 case RT2560_TX_FAIL_OTHER:
1124 device_printf(sc->sc_dev, "sending mgt frame failed "
1125 "0x%08x\n", le32toh(desc->flags));
1128 bus_dmamap_sync(sc->prioq.data_dmat, data->map,
1129 BUS_DMASYNC_POSTWRITE);
1130 bus_dmamap_unload(sc->prioq.data_dmat, data->map);
1134 KASSERT(data->ni == NULL, ("mgmt node is not empty\n"));
1136 /* descriptor is no longer valid */
1137 desc->flags &= ~htole32(RT2560_TX_VALID);
1139 DPRINTFN(15, ("prio done idx=%u\n", sc->prioq.next));
1142 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
1145 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1146 BUS_DMASYNC_PREWRITE);
1148 sc->sc_tx_timer = 0;
1149 ifp->if_flags &= ~IFF_OACTIVE;
1154 * Some frames were processed by the hardware cipher engine and are ready for
1155 * transmission to the IEEE802.11 layer.
1158 rt2560_decryption_intr(struct rt2560_softc *sc)
1160 struct ieee80211com *ic = &sc->sc_ic;
1161 struct ifnet *ifp = ic->ic_ifp;
1162 struct rt2560_rx_desc *desc;
1163 struct rt2560_rx_data *data;
1164 bus_addr_t physaddr;
1165 struct ieee80211_frame *wh;
1166 struct ieee80211_node *ni;
1167 struct mbuf *mnew, *m;
1170 /* retrieve last decriptor index processed by cipher engine */
1171 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr;
1172 hw /= RT2560_RX_DESC_SIZE;
1174 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1175 BUS_DMASYNC_POSTREAD);
1177 for (; sc->rxq.cur_decrypt != hw;) {
1178 desc = &sc->rxq.desc[sc->rxq.cur_decrypt];
1179 data = &sc->rxq.data[sc->rxq.cur_decrypt];
1181 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1182 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1190 if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
1191 (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) {
1197 * Try to allocate a new mbuf for this ring element and load it
1198 * before processing the current mbuf. If the ring element
1199 * cannot be loaded, drop the received packet and reuse the old
1200 * mbuf. In the unlikely case that the old mbuf can't be
1201 * reloaded either, explicitly panic.
1203 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1209 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1210 BUS_DMASYNC_POSTREAD);
1211 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1213 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1214 mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr,
1219 /* try to reload the old mbuf */
1220 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1221 mtod(data->m, void *), MCLBYTES,
1222 rt2560_dma_map_addr, &physaddr, 0);
1224 /* very unlikely that it will fail... */
1225 panic("%s: could not load old rx mbuf",
1226 device_get_name(sc->sc_dev));
1233 * New mbuf successfully loaded, update Rx ring and continue
1238 desc->physaddr = htole32(physaddr);
1241 m->m_pkthdr.rcvif = ifp;
1242 m->m_pkthdr.len = m->m_len =
1243 (le32toh(desc->flags) >> 16) & 0xfff;
1245 if (sc->sc_drvbpf != NULL) {
1246 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
1247 uint32_t tsf_lo, tsf_hi;
1249 /* get timestamp (low and high 32 bits) */
1250 tsf_hi = RAL_READ(sc, RT2560_CSR17);
1251 tsf_lo = RAL_READ(sc, RT2560_CSR16);
1254 htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1256 tap->wr_rate = rt2560_rxrate(desc);
1257 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1258 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1259 tap->wr_antenna = sc->rx_ant;
1260 tap->wr_antsignal = RT2560_RSSI(sc, desc->rssi);
1262 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1265 wh = mtod(m, struct ieee80211_frame *);
1266 ni = ieee80211_find_rxnode(ic,
1267 (struct ieee80211_frame_min *)wh);
1269 /* send the frame to the 802.11 layer */
1270 ieee80211_input(ic, m, ni, RT2560_RSSI(sc, desc->rssi), 0);
1272 /* node is no longer needed */
1273 ieee80211_free_node(ni);
1275 skip: desc->flags = htole32(RT2560_RX_BUSY);
1277 DPRINTFN(15, ("decryption done idx=%u\n", sc->rxq.cur_decrypt));
1279 sc->rxq.cur_decrypt =
1280 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1283 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1284 BUS_DMASYNC_PREWRITE);
1288 * Some frames were received. Pass them to the hardware cipher engine before
1289 * sending them to the 802.11 layer.
1292 rt2560_rx_intr(struct rt2560_softc *sc)
1294 struct rt2560_rx_desc *desc;
1295 struct rt2560_rx_data *data;
1297 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1298 BUS_DMASYNC_POSTREAD);
1301 desc = &sc->rxq.desc[sc->rxq.cur];
1302 data = &sc->rxq.data[sc->rxq.cur];
1304 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1305 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1310 if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) ||
1311 (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) {
1313 * This should not happen since we did not request
1314 * to receive those frames when we filled RXCSR0.
1316 DPRINTFN(5, ("PHY or CRC error flags 0x%08x\n",
1317 le32toh(desc->flags)));
1321 if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
1322 DPRINTFN(5, ("bad length\n"));
1326 /* mark the frame for decryption */
1327 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
1329 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1331 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1334 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1335 BUS_DMASYNC_PREWRITE);
1338 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1342 * This function is called periodically in IBSS mode when a new beacon must be
1346 rt2560_beacon_expire(struct rt2560_softc *sc)
1348 struct ieee80211com *ic = &sc->sc_ic;
1349 struct rt2560_tx_data *data;
1351 if (ic->ic_opmode != IEEE80211_M_IBSS &&
1352 ic->ic_opmode != IEEE80211_M_HOSTAP)
1355 data = &sc->bcnq.data[sc->bcnq.next];
1357 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1358 bus_dmamap_unload(sc->bcnq.data_dmat, data->map);
1360 ieee80211_beacon_update(ic, data->ni, &sc->sc_bo, data->m, 1);
1362 if (ic->ic_rawbpf != NULL)
1363 bpf_mtap(ic->ic_rawbpf, data->m);
1365 rt2560_tx_bcn(sc, data->m, data->ni);
1367 DPRINTFN(15, ("beacon expired\n"));
1369 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1374 rt2560_wakeup_expire(struct rt2560_softc *sc)
1376 DPRINTFN(2, ("wakeup expired\n"));
1380 rt2560_intr(void *arg)
1382 struct rt2560_softc *sc = arg;
1383 struct ifnet *ifp = &sc->sc_ic.ic_if;
1386 /* disable interrupts */
1387 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1389 /* don't re-enable interrupts if we're shutting down */
1390 if (!(ifp->if_flags & IFF_RUNNING))
1393 r = RAL_READ(sc, RT2560_CSR7);
1394 RAL_WRITE(sc, RT2560_CSR7, r);
1396 if (r & RT2560_BEACON_EXPIRE)
1397 rt2560_beacon_expire(sc);
1399 if (r & RT2560_WAKEUP_EXPIRE)
1400 rt2560_wakeup_expire(sc);
1402 if (r & RT2560_PRIO_DONE)
1403 rt2560_prio_intr(sc);
1405 if (r & (RT2560_TX_DONE | RT2560_ENCRYPTION_DONE)) {
1408 for (i = 0; i < 2; ++i) {
1410 rt2560_encryption_intr(sc);
1414 if (r & (RT2560_DECRYPTION_DONE | RT2560_RX_DONE)) {
1417 for (i = 0; i < 2; ++i) {
1418 rt2560_decryption_intr(sc);
1423 /* re-enable interrupts */
1424 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1427 /* quickly determine if a given rate is CCK or OFDM */
1428 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1430 #define RAL_ACK_SIZE (sizeof(struct ieee80211_frame_ack) + IEEE80211_FCS_LEN)
1431 #define RAL_CTS_SIZE (sizeof(struct ieee80211_frame_cts) + IEEE80211_FCS_LEN)
1433 #define RT2560_TXRX_TURNAROUND 10 /* us */
1436 * This function is only used by the Rx radiotap code.
1439 rt2560_rxrate(struct rt2560_rx_desc *desc)
1441 if (le32toh(desc->flags) & RT2560_RX_OFDM) {
1442 /* reverse function of rt2560_plcp_signal */
1443 switch (desc->rate) {
1444 case 0xb: return 12;
1445 case 0xf: return 18;
1446 case 0xa: return 24;
1447 case 0xe: return 36;
1448 case 0x9: return 48;
1449 case 0xd: return 72;
1450 case 0x8: return 96;
1451 case 0xc: return 108;
1454 if (desc->rate == 10)
1456 if (desc->rate == 20)
1458 if (desc->rate == 55)
1460 if (desc->rate == 110)
1463 return 2; /* should not get there */
1467 rt2560_plcp_signal(int rate)
1470 /* CCK rates (returned values are device-dependent) */
1473 case 11: return 0x2;
1474 case 22: return 0x3;
1476 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1477 case 12: return 0xb;
1478 case 18: return 0xf;
1479 case 24: return 0xa;
1480 case 36: return 0xe;
1481 case 48: return 0x9;
1482 case 72: return 0xd;
1483 case 96: return 0x8;
1484 case 108: return 0xc;
1486 /* unsupported rates (should not get there) */
1487 default: return 0xff;
1492 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1493 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1495 struct ieee80211com *ic = &sc->sc_ic;
1496 uint16_t plcp_length;
1499 desc->flags = htole32(flags);
1500 desc->flags |= htole32(len << 16);
1502 desc->flags |= htole32(RT2560_TX_VALID);
1504 desc->physaddr = htole32(physaddr);
1505 desc->wme = htole16(
1507 RT2560_LOGCWMIN(3) |
1508 RT2560_LOGCWMAX(8));
1510 /* setup PLCP fields */
1511 desc->plcp_signal = rt2560_plcp_signal(rate);
1512 desc->plcp_service = 4;
1514 len += IEEE80211_CRC_LEN;
1515 if (RAL_RATE_IS_OFDM(rate)) {
1516 desc->flags |= htole32(RT2560_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 |= RT2560_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 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY)
1536 : htole32(RT2560_TX_BUSY);
1540 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1541 struct ieee80211_node *ni)
1543 struct ieee80211com *ic = &sc->sc_ic;
1544 struct rt2560_tx_desc *desc;
1545 struct rt2560_tx_data *data;
1549 desc = &sc->bcnq.desc[sc->bcnq.cur];
1550 data = &sc->bcnq.data[sc->bcnq.cur];
1552 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1554 error = bus_dmamap_load_mbuf(sc->bcnq.data_dmat, data->map, m0,
1555 rt2560_dma_map_mbuf, &paddr,
1558 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1564 if (sc->sc_drvbpf != NULL) {
1565 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1568 tap->wt_rate = rate;
1569 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1570 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1571 tap->wt_antenna = sc->tx_ant;
1573 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1579 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1580 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, paddr);
1582 DPRINTFN(10, ("sending beacon frame len=%u idx=%u rate=%u\n",
1583 m0->m_pkthdr.len, sc->bcnq.cur, rate));
1585 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1586 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map,
1587 BUS_DMASYNC_PREWRITE);
1589 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT;
1595 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1596 struct ieee80211_node *ni)
1598 struct ieee80211com *ic = &sc->sc_ic;
1599 struct rt2560_tx_desc *desc;
1600 struct rt2560_tx_data *data;
1601 struct ieee80211_frame *wh;
1607 desc = &sc->prioq.desc[sc->prioq.cur];
1608 data = &sc->prioq.data[sc->prioq.cur];
1610 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1612 error = bus_dmamap_load_mbuf(sc->prioq.data_dmat, data->map, m0,
1613 rt2560_dma_map_mbuf, &paddr, 0);
1615 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1617 ieee80211_free_node(ni);
1622 if (sc->sc_drvbpf != NULL) {
1623 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1626 tap->wt_rate = rate;
1627 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1628 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1629 tap->wt_antenna = sc->tx_ant;
1631 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1637 wh = mtod(m0, struct ieee80211_frame *);
1639 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1640 flags |= RT2560_TX_ACK;
1642 dur = ieee80211_txtime(ni, RAL_ACK_SIZE, rate, ic->ic_flags) +
1644 *(uint16_t *)wh->i_dur = htole16(dur);
1646 /* tell hardware to add timestamp for probe responses */
1647 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1648 IEEE80211_FC0_TYPE_MGT &&
1649 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1650 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1651 flags |= RT2560_TX_TIMESTAMP;
1654 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0, paddr);
1656 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1657 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1658 BUS_DMASYNC_PREWRITE);
1660 DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n",
1661 m0->m_pkthdr.len, sc->prioq.cur, rate));
1665 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1666 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1668 ieee80211_free_node(ni);
1674 * Build a RTS control frame.
1676 static struct mbuf *
1677 rt2560_get_rts(struct rt2560_softc *sc, struct ieee80211_frame *wh,
1680 struct ieee80211_frame_rts *rts;
1683 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1685 sc->sc_ic.ic_stats.is_tx_nobuf++;
1686 device_printf(sc->sc_dev, "could not allocate RTS frame\n");
1690 rts = mtod(m, struct ieee80211_frame_rts *);
1692 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1693 IEEE80211_FC0_SUBTYPE_RTS;
1694 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1695 *(uint16_t *)rts->i_dur = htole16(dur);
1696 IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1);
1697 IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2);
1699 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
1705 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1706 struct ieee80211_node *ni)
1708 struct ieee80211com *ic = &sc->sc_ic;
1709 struct rt2560_tx_desc *desc;
1710 struct rt2560_tx_data *data;
1711 struct ieee80211_frame *wh;
1712 struct ieee80211_key *k;
1717 int rate, error, ackrate, rateidx;
1719 wh = mtod(m0, struct ieee80211_frame *);
1720 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1721 k = ieee80211_crypto_encap(ic, ni, m0);
1727 /* packet header may have moved, reset our local pointer */
1728 wh = mtod(m0, struct ieee80211_frame *);
1731 ieee80211_ratectl_findrate(ni, m0->m_pkthdr.len, &rateidx, 1);
1732 rate = IEEE80211_RS_RATE(&ni->ni_rates, rateidx);
1734 ackrate = ieee80211_ack_rate(ni, rate);
1737 * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange
1738 * for directed frames only when the length of the MPDU is greater
1739 * than the length threshold indicated by [...]" ic_rtsthreshold.
1741 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1742 m0->m_pkthdr.len > ic->ic_rtsthreshold) {
1747 rtsrate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1748 dur = ieee80211_txtime(ni, m0->m_pkthdr.len + IEEE80211_FCS_LEN,
1749 rate, ic->ic_flags) +
1750 ieee80211_txtime(ni, RAL_CTS_SIZE, rtsrate, ic->ic_flags)+
1751 ieee80211_txtime(ni, RAL_ACK_SIZE, ackrate, ic->ic_flags)+
1754 m = rt2560_get_rts(sc, wh, dur);
1756 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1757 data = &sc->txq.data[sc->txq.cur_encrypt];
1759 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map,
1760 m, rt2560_dma_map_mbuf, &paddr, 0);
1762 device_printf(sc->sc_dev,
1763 "could not map mbuf (error %d)\n", error);
1769 /* avoid multiple free() of the same node for each fragment */
1770 ieee80211_ref_node(ni);
1774 data->rateidx = -1; /* don't count RTS */
1776 rt2560_setup_tx_desc(sc, desc, RT2560_TX_ACK |
1777 RT2560_TX_MORE_FRAG, m->m_pkthdr.len, rtsrate, 1, paddr);
1779 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1780 BUS_DMASYNC_PREWRITE);
1783 sc->txq.cur_encrypt =
1784 (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1787 * IEEE Std 802.11-1999: when an RTS/CTS exchange is used, the
1788 * asynchronous data frame shall be transmitted after the CTS
1789 * frame and a SIFS period.
1791 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1794 data = &sc->txq.data[sc->txq.cur_encrypt];
1795 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1797 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map, m0,
1798 rt2560_dma_map_mbuf, &paddr, 0);
1799 if (error != 0 && error != EFBIG) {
1800 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1806 mnew = m_defrag(m0, MB_DONTWAIT);
1808 device_printf(sc->sc_dev,
1809 "could not defragment mbuf\n");
1815 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map,
1816 m0, rt2560_dma_map_mbuf, &paddr,
1819 device_printf(sc->sc_dev,
1820 "could not map mbuf (error %d)\n", error);
1825 /* packet header may have moved, reset our local pointer */
1826 wh = mtod(m0, struct ieee80211_frame *);
1829 if (sc->sc_drvbpf != NULL) {
1830 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1833 tap->wt_rate = rate;
1834 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1835 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1836 tap->wt_antenna = sc->tx_ant;
1838 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1843 data->rateidx = rateidx;
1845 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1846 flags |= RT2560_TX_ACK;
1847 dur = ieee80211_txtime(ni, RAL_ACK_SIZE, ackrate, ic->ic_flags)+
1849 *(uint16_t *)wh->i_dur = htole16(dur);
1852 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1, paddr);
1854 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1855 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1856 BUS_DMASYNC_PREWRITE);
1858 DPRINTFN(10, ("sending data frame len=%u idx=%u rate=%u\n",
1859 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate));
1863 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1864 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1870 rt2560_start(struct ifnet *ifp)
1872 struct rt2560_softc *sc = ifp->if_softc;
1873 struct ieee80211com *ic = &sc->sc_ic;
1875 struct ether_header *eh;
1876 struct ieee80211_node *ni;
1878 /* prevent management frames from being sent if we're not ready */
1879 if (!(ifp->if_flags & IFF_RUNNING))
1883 IF_POLL(&ic->ic_mgtq, m0);
1885 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
1886 ifp->if_flags |= IFF_OACTIVE;
1889 IF_DEQUEUE(&ic->ic_mgtq, m0);
1891 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1892 m0->m_pkthdr.rcvif = NULL;
1894 if (ic->ic_rawbpf != NULL)
1895 bpf_mtap(ic->ic_rawbpf, m0);
1897 if (rt2560_tx_mgt(sc, m0, ni) != 0)
1901 if (ic->ic_state != IEEE80211_S_RUN)
1903 m0 = ifq_poll(&ifp->if_snd);
1906 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
1907 ifp->if_flags |= IFF_OACTIVE;
1910 m0 = ifq_dequeue(&ifp->if_snd, m0);
1912 if (m0->m_len < sizeof (struct ether_header) &&
1913 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1916 eh = mtod(m0, struct ether_header *);
1917 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1924 m0 = ieee80211_encap(ic, m0, ni);
1926 ieee80211_free_node(ni);
1930 if (ic->ic_rawbpf != NULL)
1931 bpf_mtap(ic->ic_rawbpf, m0);
1933 if (rt2560_tx_data(sc, m0, ni) != 0) {
1934 ieee80211_free_node(ni);
1940 sc->sc_tx_timer = 5;
1946 rt2560_watchdog(struct ifnet *ifp)
1948 struct rt2560_softc *sc = ifp->if_softc;
1949 struct ieee80211com *ic = &sc->sc_ic;
1953 if (sc->sc_tx_timer > 0) {
1954 if (--sc->sc_tx_timer == 0) {
1955 device_printf(sc->sc_dev, "device timeout\n");
1963 ieee80211_watchdog(ic);
1967 * This function allows for fast channel switching in monitor mode (used by
1968 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
1969 * generate a new beacon frame.
1972 rt2560_reset(struct ifnet *ifp)
1974 struct rt2560_softc *sc = ifp->if_softc;
1975 struct ieee80211com *ic = &sc->sc_ic;
1977 if (ic->ic_opmode != IEEE80211_M_MONITOR)
1980 rt2560_set_chan(sc, ic->ic_curchan);
1986 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
1988 struct rt2560_softc *sc = ifp->if_softc;
1989 struct ieee80211com *ic = &sc->sc_ic;
1994 if (ifp->if_flags & IFF_UP) {
1995 if (ifp->if_flags & IFF_RUNNING)
1996 rt2560_update_promisc(sc);
2000 if (ifp->if_flags & IFF_RUNNING)
2006 error = ieee80211_ioctl(ic, cmd, data, cr);
2009 if (error == ENETRESET) {
2010 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
2011 (IFF_UP | IFF_RUNNING) &&
2012 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
2021 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2026 for (ntries = 0; ntries < 100; ntries++) {
2027 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2031 if (ntries == 100) {
2032 device_printf(sc->sc_dev, "could not write to BBP\n");
2036 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2037 RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2039 DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg, val));
2043 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2048 val = RT2560_BBP_BUSY | reg << 8;
2049 RAL_WRITE(sc, RT2560_BBPCSR, val);
2051 for (ntries = 0; ntries < 100; ntries++) {
2052 val = RAL_READ(sc, RT2560_BBPCSR);
2053 if (!(val & RT2560_BBP_BUSY))
2058 device_printf(sc->sc_dev, "could not read from BBP\n");
2063 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2068 for (ntries = 0; ntries < 100; ntries++) {
2069 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2073 if (ntries == 100) {
2074 device_printf(sc->sc_dev, "could not write to RF\n");
2078 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2080 RAL_WRITE(sc, RT2560_RFCSR, tmp);
2082 /* remember last written value in sc */
2083 sc->rf_regs[reg] = val;
2085 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
2089 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2091 struct ieee80211com *ic = &sc->sc_ic;
2095 chan = ieee80211_chan2ieee(ic, c);
2096 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2099 if (IEEE80211_IS_CHAN_2GHZ(c))
2100 power = min(sc->txpow[chan - 1], 31);
2104 /* adjust txpower using ifconfig settings */
2105 power -= (100 - ic->ic_txpowlimit) / 8;
2107 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
2109 switch (sc->rf_rev) {
2110 case RT2560_RF_2522:
2111 rt2560_rf_write(sc, RAL_RF1, 0x00814);
2112 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2113 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2116 case RT2560_RF_2523:
2117 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2118 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2119 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2120 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2123 case RT2560_RF_2524:
2124 rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2125 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2126 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2127 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2130 case RT2560_RF_2525:
2131 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2132 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2133 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2134 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2136 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2137 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2138 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2139 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2142 case RT2560_RF_2525E:
2143 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2144 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2145 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2146 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2149 case RT2560_RF_2526:
2150 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2151 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2152 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2154 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2155 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2156 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2160 case RT2560_RF_5222:
2161 for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2163 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2164 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2165 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2166 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2170 if (ic->ic_state != IEEE80211_S_SCAN) {
2171 /* set Japan filter bit for channel 14 */
2172 tmp = rt2560_bbp_read(sc, 70);
2174 tmp &= ~RT2560_JAPAN_FILTER;
2176 tmp |= RT2560_JAPAN_FILTER;
2178 rt2560_bbp_write(sc, 70, tmp);
2180 /* clear CRC errors */
2181 RAL_READ(sc, RT2560_CNT0);
2184 sc->sc_sifs = IEEE80211_IS_CHAN_5GHZ(c) ? IEEE80211_DUR_OFDM_SIFS
2185 : IEEE80211_DUR_SIFS;
2190 * Disable RF auto-tuning.
2193 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2197 if (sc->rf_rev != RT2560_RF_2523) {
2198 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
2199 rt2560_rf_write(sc, RAL_RF1, tmp);
2202 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
2203 rt2560_rf_write(sc, RAL_RF3, tmp);
2205 DPRINTFN(2, ("disabling RF autotune\n"));
2210 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2214 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2216 struct ieee80211com *ic = &sc->sc_ic;
2217 uint16_t logcwmin, preload;
2220 /* first, disable TSF synchronization */
2221 RAL_WRITE(sc, RT2560_CSR14, 0);
2223 tmp = 16 * ic->ic_bss->ni_intval;
2224 RAL_WRITE(sc, RT2560_CSR12, tmp);
2226 RAL_WRITE(sc, RT2560_CSR13, 0);
2229 preload = (ic->ic_opmode == IEEE80211_M_STA) ? 384 : 1024;
2230 tmp = logcwmin << 16 | preload;
2231 RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2233 /* finally, enable TSF synchronization */
2234 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2235 if (ic->ic_opmode == IEEE80211_M_STA)
2236 tmp |= RT2560_ENABLE_TSF_SYNC(1);
2238 tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2239 RT2560_ENABLE_BEACON_GENERATOR;
2240 RAL_WRITE(sc, RT2560_CSR14, tmp);
2242 DPRINTF(("enabling TSF synchronization\n"));
2246 rt2560_update_plcp(struct rt2560_softc *sc)
2248 struct ieee80211com *ic = &sc->sc_ic;
2250 /* no short preamble for 1Mbps */
2251 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2253 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2254 /* values taken from the reference driver */
2255 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401);
2256 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2257 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403);
2259 /* same values as above or'ed 0x8 */
2260 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409);
2261 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2262 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b);
2265 DPRINTF(("updating PLCP for %s preamble\n",
2266 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"));
2270 * This function can be called by ieee80211_set_shortslottime(). Refer to
2271 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
2274 rt2560_update_slot(struct ifnet *ifp)
2276 struct rt2560_softc *sc = ifp->if_softc;
2277 struct ieee80211com *ic = &sc->sc_ic;
2279 uint16_t tx_sifs, tx_pifs, tx_difs, eifs;
2282 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2284 /* update the MAC slot boundaries */
2285 tx_sifs = sc->sc_sifs - RT2560_TXRX_TURNAROUND;
2286 tx_pifs = tx_sifs + slottime;
2287 tx_difs = tx_sifs + 2 * slottime;
2288 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2290 tmp = RAL_READ(sc, RT2560_CSR11);
2291 tmp = (tmp & ~0x1f00) | slottime << 8;
2292 RAL_WRITE(sc, RT2560_CSR11, tmp);
2294 tmp = tx_pifs << 16 | tx_sifs;
2295 RAL_WRITE(sc, RT2560_CSR18, tmp);
2297 tmp = eifs << 16 | tx_difs;
2298 RAL_WRITE(sc, RT2560_CSR19, tmp);
2300 DPRINTF(("setting slottime to %uus\n", slottime));
2304 rt2560_set_basicrates(struct rt2560_softc *sc)
2306 struct ieee80211com *ic = &sc->sc_ic;
2308 /* update basic rate set */
2309 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2310 /* 11b basic rates: 1, 2Mbps */
2311 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2312 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
2313 /* 11a basic rates: 6, 12, 24Mbps */
2314 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2316 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2317 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2322 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2326 /* set ON period to 70ms and OFF period to 30ms */
2327 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2328 RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2332 rt2560_set_bssid(struct rt2560_softc *sc, uint8_t *bssid)
2336 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2337 RAL_WRITE(sc, RT2560_CSR5, tmp);
2339 tmp = bssid[4] | bssid[5] << 8;
2340 RAL_WRITE(sc, RT2560_CSR6, tmp);
2342 DPRINTF(("setting BSSID to %6D\n", bssid, ":"));
2346 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2350 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2351 RAL_WRITE(sc, RT2560_CSR3, tmp);
2353 tmp = addr[4] | addr[5] << 8;
2354 RAL_WRITE(sc, RT2560_CSR4, tmp);
2356 DPRINTF(("setting MAC address to %6D\n", addr, ":"));
2360 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2364 tmp = RAL_READ(sc, RT2560_CSR3);
2365 addr[0] = tmp & 0xff;
2366 addr[1] = (tmp >> 8) & 0xff;
2367 addr[2] = (tmp >> 16) & 0xff;
2368 addr[3] = (tmp >> 24);
2370 tmp = RAL_READ(sc, RT2560_CSR4);
2371 addr[4] = tmp & 0xff;
2372 addr[5] = (tmp >> 8) & 0xff;
2376 rt2560_update_promisc(struct rt2560_softc *sc)
2378 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2381 tmp = RAL_READ(sc, RT2560_RXCSR0);
2383 tmp &= ~RT2560_DROP_NOT_TO_ME;
2384 if (!(ifp->if_flags & IFF_PROMISC))
2385 tmp |= RT2560_DROP_NOT_TO_ME;
2387 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2389 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2390 "entering" : "leaving"));
2394 rt2560_get_rf(int rev)
2397 case RT2560_RF_2522: return "RT2522";
2398 case RT2560_RF_2523: return "RT2523";
2399 case RT2560_RF_2524: return "RT2524";
2400 case RT2560_RF_2525: return "RT2525";
2401 case RT2560_RF_2525E: return "RT2525e";
2402 case RT2560_RF_2526: return "RT2526";
2403 case RT2560_RF_5222: return "RT5222";
2404 default: return "unknown";
2409 rt2560_read_eeprom(struct rt2560_softc *sc)
2414 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2415 sc->rf_rev = (val >> 11) & 0x7;
2416 sc->hw_radio = (val >> 10) & 0x1;
2417 sc->led_mode = (val >> 6) & 0x7;
2418 sc->rx_ant = (val >> 4) & 0x3;
2419 sc->tx_ant = (val >> 2) & 0x3;
2420 sc->nb_ant = val & 0x3;
2422 /* read default values for BBP registers */
2423 for (i = 0; i < 16; i++) {
2424 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2425 sc->bbp_prom[i].reg = val >> 8;
2426 sc->bbp_prom[i].val = val & 0xff;
2429 /* read Tx power for all b/g channels */
2430 for (i = 0; i < 14 / 2; i++) {
2431 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2432 sc->txpow[i * 2] = val >> 8;
2433 sc->txpow[i * 2 + 1] = val & 0xff;
2436 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE);
2437 if ((val & 0xff00) == 0xff00)
2438 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR;
2440 sc->rssi_corr = val >> 8;
2441 DPRINTF(("rssi correction %d, calibrate 0x%02x\n",
2442 sc->rssi_corr, val));
2446 rt2560_bbp_init(struct rt2560_softc *sc)
2448 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2451 /* wait for BBP to be ready */
2452 for (ntries = 0; ntries < 100; ntries++) {
2453 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2457 if (ntries == 100) {
2458 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2462 rt2560_set_txantenna(sc, sc->tx_ant);
2463 rt2560_set_rxantenna(sc, sc->rx_ant);
2465 /* initialize BBP registers to default values */
2466 for (i = 0; i < N(rt2560_def_bbp); i++) {
2467 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2468 rt2560_def_bbp[i].val);
2471 /* initialize BBP registers to values stored in EEPROM */
2472 for (i = 0; i < 16; i++) {
2473 if (sc->bbp_prom[i].reg == 0xff)
2475 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2484 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2489 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2491 tx |= RT2560_BBP_ANTA;
2492 else if (antenna == 2)
2493 tx |= RT2560_BBP_ANTB;
2495 tx |= RT2560_BBP_DIVERSITY;
2497 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2498 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2499 sc->rf_rev == RT2560_RF_5222)
2500 tx |= RT2560_BBP_FLIPIQ;
2502 rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2504 /* update values for CCK and OFDM in BBPCSR1 */
2505 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2506 tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2507 RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2511 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2515 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2517 rx |= RT2560_BBP_ANTA;
2518 else if (antenna == 2)
2519 rx |= RT2560_BBP_ANTB;
2521 rx |= RT2560_BBP_DIVERSITY;
2523 /* need to force no I/Q flip for RF 2525e and 2526 */
2524 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2525 rx &= ~RT2560_BBP_FLIPIQ;
2527 rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2531 rt2560_init(void *priv)
2533 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2534 struct rt2560_softc *sc = priv;
2535 struct ieee80211com *ic = &sc->sc_ic;
2536 struct ifnet *ifp = ic->ic_ifp;
2542 /* setup tx rings */
2543 tmp = RT2560_PRIO_RING_COUNT << 24 |
2544 RT2560_ATIM_RING_COUNT << 16 |
2545 RT2560_TX_RING_COUNT << 8 |
2546 RT2560_TX_DESC_SIZE;
2548 /* rings must be initialized in this exact order */
2549 RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2550 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2551 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2552 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2553 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2556 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2558 RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2559 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2561 /* initialize MAC registers to default values */
2562 for (i = 0; i < N(rt2560_def_mac); i++)
2563 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2565 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2566 rt2560_set_macaddr(sc, ic->ic_myaddr);
2568 /* set basic rate set (will be updated later) */
2569 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2571 rt2560_update_slot(ifp);
2572 rt2560_update_plcp(sc);
2573 rt2560_update_led(sc, 0, 0);
2575 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2576 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2578 if (rt2560_bbp_init(sc) != 0) {
2583 /* set default BSS channel */
2584 rt2560_set_chan(sc, ic->ic_curchan);
2587 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2588 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2589 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2590 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2591 tmp |= RT2560_DROP_TODS;
2592 if (!(ifp->if_flags & IFF_PROMISC))
2593 tmp |= RT2560_DROP_NOT_TO_ME;
2595 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2597 /* clear old FCS and Rx FIFO errors */
2598 RAL_READ(sc, RT2560_CNT0);
2599 RAL_READ(sc, RT2560_CNT4);
2601 /* clear any pending interrupts */
2602 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2604 /* enable interrupts */
2605 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2607 ifp->if_flags &= ~IFF_OACTIVE;
2608 ifp->if_flags |= IFF_RUNNING;
2611 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2614 ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
2615 for (i = 0; i < IEEE80211_WEP_NKID; ++i) {
2616 struct ieee80211_key *wk = &ic->ic_nw_keys[i];
2618 if (wk->wk_keylen == 0)
2620 if (wk->wk_flags & IEEE80211_KEY_XMIT)
2621 wk->wk_flags |= IEEE80211_KEY_SWCRYPT;
2625 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2626 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2627 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2629 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2634 rt2560_stop(void *priv)
2636 struct rt2560_softc *sc = priv;
2637 struct ieee80211com *ic = &sc->sc_ic;
2638 struct ifnet *ifp = ic->ic_ifp;
2640 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2642 sc->sc_tx_timer = 0;
2644 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2647 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2650 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2652 /* reset ASIC (imply reset BBP) */
2653 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2654 RAL_WRITE(sc, RT2560_CSR1, 0);
2656 /* disable interrupts */
2657 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2659 /* reset Tx and Rx rings */
2660 rt2560_reset_tx_ring(sc, &sc->txq);
2661 rt2560_reset_tx_ring(sc, &sc->atimq);
2662 rt2560_reset_tx_ring(sc, &sc->prioq);
2663 rt2560_reset_tx_ring(sc, &sc->bcnq);
2664 rt2560_reset_rx_ring(sc, &sc->rxq);
2668 rt2560_dma_map_mbuf(void *arg, bus_dma_segment_t *seg, int nseg,
2669 bus_size_t map_size __unused, int error)
2674 KASSERT(nseg == 1, ("too many dma segments\n"));
2675 *((bus_addr_t *)arg) = seg->ds_addr;