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.3 2006/10/25 20:55:58 dillon Exp $
22 * Ralink Technology RT2560 chipset driver
23 * http://www.ralinktech.com/
26 #include <sys/param.h>
27 #include <sys/sysctl.h>
28 #include <sys/sockio.h>
30 #include <sys/kernel.h>
31 #include <sys/socket.h>
32 #include <sys/systm.h>
33 #include <sys/malloc.h>
34 #include <sys/module.h>
37 #include <sys/endian.h>
38 #include <sys/serialize.h>
40 #include <machine/clock.h>
44 #include <net/if_arp.h>
45 #include <net/ethernet.h>
46 #include <net/if_dl.h>
47 #include <net/if_media.h>
48 #include <net/if_types.h>
49 #include <net/ifq_var.h>
51 #include <netproto/802_11/ieee80211_var.h>
52 #include <netproto/802_11/ieee80211_radiotap.h>
54 #include <netinet/in.h>
55 #include <netinet/in_systm.h>
56 #include <netinet/in_var.h>
57 #include <netinet/ip.h>
58 #include <netinet/if_ether.h>
60 #include <dev/netif/ral/if_ralrate.h>
61 #include <dev/netif/ral/rt2560reg.h>
62 #include <dev/netif/ral/rt2560var.h>
65 #define DPRINTF(x) do { if (ral_debug > 0) printf x; } while (0)
66 #define DPRINTFN(n, x) do { if (ral_debug >= (n)) printf x; } while (0)
70 #define DPRINTFN(n, x)
73 static void rt2560_dma_map_addr(void *, bus_dma_segment_t *, int,
75 static void rt2560_dma_map_mbuf(void *, bus_dma_segment_t *, int,
77 static int rt2560_alloc_tx_ring(struct rt2560_softc *,
78 struct rt2560_tx_ring *, int);
79 static void rt2560_reset_tx_ring(struct rt2560_softc *,
80 struct rt2560_tx_ring *);
81 static void rt2560_free_tx_ring(struct rt2560_softc *,
82 struct rt2560_tx_ring *);
83 static int rt2560_alloc_rx_ring(struct rt2560_softc *,
84 struct rt2560_rx_ring *, int);
85 static void rt2560_reset_rx_ring(struct rt2560_softc *,
86 struct rt2560_rx_ring *);
87 static void rt2560_free_rx_ring(struct rt2560_softc *,
88 struct rt2560_rx_ring *);
89 static struct ieee80211_node *rt2560_node_alloc(
90 struct ieee80211_node_table *);
91 static int rt2560_media_change(struct ifnet *);
92 static void rt2560_next_scan(void *);
93 static void rt2560_iter_func(void *, struct ieee80211_node *);
94 static void rt2560_update_rssadapt(void *);
95 static int rt2560_newstate(struct ieee80211com *,
96 enum ieee80211_state, int);
97 static uint16_t rt2560_eeprom_read(struct rt2560_softc *, uint8_t);
98 static void rt2560_encryption_intr(struct rt2560_softc *);
99 static void rt2560_tx_intr(struct rt2560_softc *);
100 static void rt2560_prio_intr(struct rt2560_softc *);
101 static void rt2560_decryption_intr(struct rt2560_softc *);
102 static void rt2560_rx_intr(struct rt2560_softc *);
103 static void rt2560_beacon_expire(struct rt2560_softc *);
104 static void rt2560_wakeup_expire(struct rt2560_softc *);
105 static uint8_t rt2560_rxrate(struct rt2560_rx_desc *);
106 static int rt2560_ack_rate(struct ieee80211com *, int);
107 static uint16_t rt2560_txtime(int, int, uint32_t);
108 static uint8_t rt2560_plcp_signal(int);
109 static void rt2560_setup_tx_desc(struct rt2560_softc *,
110 struct rt2560_tx_desc *, uint32_t, int, int, int,
112 static int rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *,
113 struct ieee80211_node *);
114 static int rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *,
115 struct ieee80211_node *);
116 static struct mbuf *rt2560_get_rts(struct rt2560_softc *,
117 struct ieee80211_frame *, uint16_t);
118 static int rt2560_tx_data(struct rt2560_softc *, struct mbuf *,
119 struct ieee80211_node *);
120 static void rt2560_start(struct ifnet *);
121 static void rt2560_watchdog(struct ifnet *);
122 static int rt2560_reset(struct ifnet *);
123 static int rt2560_ioctl(struct ifnet *, u_long, caddr_t,
125 static void rt2560_bbp_write(struct rt2560_softc *, uint8_t,
127 static uint8_t rt2560_bbp_read(struct rt2560_softc *, uint8_t);
128 static void rt2560_rf_write(struct rt2560_softc *, uint8_t,
130 static void rt2560_set_chan(struct rt2560_softc *,
131 struct ieee80211_channel *);
133 static void rt2560_disable_rf_tune(struct rt2560_softc *);
135 static void rt2560_enable_tsf_sync(struct rt2560_softc *);
136 static void rt2560_update_plcp(struct rt2560_softc *);
137 static void rt2560_update_slot(struct ifnet *);
138 static void rt2560_set_basicrates(struct rt2560_softc *);
139 static void rt2560_update_led(struct rt2560_softc *, int, int);
140 static void rt2560_set_bssid(struct rt2560_softc *, uint8_t *);
141 static void rt2560_set_macaddr(struct rt2560_softc *, uint8_t *);
142 static void rt2560_get_macaddr(struct rt2560_softc *, uint8_t *);
143 static void rt2560_update_promisc(struct rt2560_softc *);
144 static const char *rt2560_get_rf(int);
145 static void rt2560_read_eeprom(struct rt2560_softc *);
146 static int rt2560_bbp_init(struct rt2560_softc *);
147 static void rt2560_set_txantenna(struct rt2560_softc *, int);
148 static void rt2560_set_rxantenna(struct rt2560_softc *, int);
149 static void rt2560_init(void *);
150 static void rt2560_stop(void *);
151 static void rt2560_intr(void *);
154 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
156 static const struct ieee80211_rateset rt2560_rateset_11a =
157 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
159 static const struct ieee80211_rateset rt2560_rateset_11b =
160 { 4, { 2, 4, 11, 22 } };
162 static const struct ieee80211_rateset rt2560_rateset_11g =
163 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
165 static const struct {
168 } rt2560_def_mac[] = {
172 static const struct {
175 } rt2560_def_bbp[] = {
179 static const uint32_t rt2560_rf2522_r2[] = RT2560_RF2522_R2;
180 static const uint32_t rt2560_rf2523_r2[] = RT2560_RF2523_R2;
181 static const uint32_t rt2560_rf2524_r2[] = RT2560_RF2524_R2;
182 static const uint32_t rt2560_rf2525_r2[] = RT2560_RF2525_R2;
183 static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2;
184 static const uint32_t rt2560_rf2525e_r2[] = RT2560_RF2525E_R2;
185 static const uint32_t rt2560_rf2526_r2[] = RT2560_RF2526_R2;
186 static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2;
188 static const struct {
191 } rt2560_rf5222[] = {
196 rt2560_attach(device_t dev, int id)
198 struct rt2560_softc *sc = device_get_softc(dev);
199 struct ieee80211com *ic = &sc->sc_ic;
200 struct ifnet *ifp = &ic->ic_if;
203 callout_init(&sc->scan_ch);
204 callout_init(&sc->rssadapt_ch);
207 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irq_rid,
208 RF_ACTIVE | RF_SHAREABLE);
209 if (sc->sc_irq == NULL) {
210 device_printf(dev, "could not allocate interrupt resource\n");
214 /* retrieve RT2560 rev. no */
215 sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
217 /* retrieve MAC address */
218 rt2560_get_macaddr(sc, ic->ic_myaddr);
220 /* retrieve RF rev. no and various other things from EEPROM */
221 rt2560_read_eeprom(sc);
223 device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n",
224 sc->asic_rev, rt2560_get_rf(sc->rf_rev));
227 * Allocate Tx and Rx rings.
229 error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT);
231 device_printf(sc->sc_dev, "could not allocate Tx ring\n");
235 error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT);
237 device_printf(sc->sc_dev, "could not allocate ATIM ring\n");
241 error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT);
243 device_printf(sc->sc_dev, "could not allocate Prio ring\n");
247 error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT);
249 device_printf(sc->sc_dev, "could not allocate Beacon ring\n");
253 error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT);
255 device_printf(sc->sc_dev, "could not allocate Rx ring\n");
259 sysctl_ctx_init(&sc->sysctl_ctx);
260 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
261 SYSCTL_STATIC_CHILDREN(_hw),
263 device_get_nameunit(dev),
265 if (sc->sysctl_tree == NULL) {
266 device_printf(dev, "could not add sysctl node\n");
272 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
273 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
274 ifp->if_init = rt2560_init;
275 ifp->if_ioctl = rt2560_ioctl;
276 ifp->if_start = rt2560_start;
277 ifp->if_watchdog = rt2560_watchdog;
278 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
279 ifq_set_ready(&ifp->if_snd);
281 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
282 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
283 ic->ic_state = IEEE80211_S_INIT;
285 /* set device capabilities */
287 IEEE80211_C_IBSS | /* IBSS mode supported */
288 IEEE80211_C_MONITOR | /* monitor mode supported */
289 IEEE80211_C_HOSTAP | /* HostAp mode supported */
290 IEEE80211_C_TXPMGT | /* tx power management */
291 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
292 IEEE80211_C_SHSLOT | /* short slot time supported */
293 IEEE80211_C_WEP | /* WEP */
294 IEEE80211_C_WPA; /* 802.11i */
296 if (sc->rf_rev == RT2560_RF_5222) {
297 /* set supported .11a rates */
298 ic->ic_sup_rates[IEEE80211_MODE_11A] = rt2560_rateset_11a;
300 /* set supported .11a channels */
301 for (i = 36; i <= 64; 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;
306 for (i = 100; i <= 140; i += 4) {
307 ic->ic_channels[i].ic_freq =
308 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
309 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
311 for (i = 149; i <= 161; i += 4) {
312 ic->ic_channels[i].ic_freq =
313 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
314 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
318 /* set supported .11b and .11g rates */
319 ic->ic_sup_rates[IEEE80211_MODE_11B] = rt2560_rateset_11b;
320 ic->ic_sup_rates[IEEE80211_MODE_11G] = rt2560_rateset_11g;
322 /* set supported .11b and .11g channels (1 through 14) */
323 for (i = 1; i <= 14; i++) {
324 ic->ic_channels[i].ic_freq =
325 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
326 ic->ic_channels[i].ic_flags =
327 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
328 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
331 ieee80211_ifattach(ic);
332 ic->ic_node_alloc = rt2560_node_alloc;
333 ic->ic_updateslot = rt2560_update_slot;
334 ic->ic_reset = rt2560_reset;
335 /* enable s/w bmiss handling in sta mode */
336 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
338 /* override state transition machine */
339 sc->sc_newstate = ic->ic_newstate;
340 ic->ic_newstate = rt2560_newstate;
341 ieee80211_media_init(ic, rt2560_media_change, ieee80211_media_status);
343 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
344 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
346 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
347 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
348 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2560_RX_RADIOTAP_PRESENT);
350 sc->sc_txtap_len = sizeof sc->sc_txtapu;
351 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
352 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2560_TX_RADIOTAP_PRESENT);
355 * Add a few sysctl knobs.
359 SYSCTL_ADD_INT(&sc->sysctl_ctx,
360 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
361 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)");
363 SYSCTL_ADD_INT(&sc->sysctl_ctx,
364 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
365 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)");
367 SYSCTL_ADD_INT(&sc->sysctl_ctx,
368 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "dwell",
369 CTLFLAG_RW, &sc->dwelltime, 0,
370 "channel dwell time (ms) for AP/station scanning");
372 error = bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE, rt2560_intr,
373 sc, &sc->sc_ih, ifp->if_serializer);
375 device_printf(dev, "could not set up interrupt\n");
377 ieee80211_ifdetach(ic);
382 ieee80211_announce(ic);
390 rt2560_detach(void *xsc)
392 struct rt2560_softc *sc = xsc;
393 struct ieee80211com *ic = &sc->sc_ic;
394 struct ifnet *ifp = ic->ic_ifp;
396 if (device_is_attached(sc->sc_dev)) {
397 lwkt_serialize_enter(ifp->if_serializer);
399 callout_stop(&sc->scan_ch);
400 callout_stop(&sc->rssadapt_ch);
403 bus_teardown_intr(sc->sc_dev, sc->sc_irq, sc->sc_ih);
405 lwkt_serialize_exit(ifp->if_serializer);
408 ieee80211_ifdetach(ic);
411 rt2560_free_tx_ring(sc, &sc->txq);
412 rt2560_free_tx_ring(sc, &sc->atimq);
413 rt2560_free_tx_ring(sc, &sc->prioq);
414 rt2560_free_tx_ring(sc, &sc->bcnq);
415 rt2560_free_rx_ring(sc, &sc->rxq);
417 if (sc->sc_irq != NULL) {
418 bus_release_resource(sc->sc_dev, SYS_RES_IRQ, sc->sc_irq_rid,
422 if (sc->sysctl_tree != NULL)
423 sysctl_ctx_free(&sc->sysctl_ctx);
429 rt2560_shutdown(void *xsc)
431 struct rt2560_softc *sc = xsc;
432 struct ifnet *ifp = &sc->sc_ic.ic_if;
434 lwkt_serialize_enter(ifp->if_serializer);
436 lwkt_serialize_exit(ifp->if_serializer);
440 rt2560_suspend(void *xsc)
442 struct rt2560_softc *sc = xsc;
443 struct ifnet *ifp = &sc->sc_ic.ic_if;
445 lwkt_serialize_enter(ifp->if_serializer);
447 lwkt_serialize_exit(ifp->if_serializer);
451 rt2560_resume(void *xsc)
453 struct rt2560_softc *sc = xsc;
454 struct ifnet *ifp = sc->sc_ic.ic_ifp;
456 lwkt_serialize_enter(ifp->if_serializer);
457 if (ifp->if_flags & IFF_UP) {
458 ifp->if_init(ifp->if_softc);
459 if (ifp->if_flags & IFF_RUNNING)
462 lwkt_serialize_exit(ifp->if_serializer);
466 rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
471 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
473 *(bus_addr_t *)arg = segs[0].ds_addr;
477 rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring,
484 ring->cur = ring->next = 0;
485 ring->cur_encrypt = ring->next_encrypt = 0;
487 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
488 BUS_SPACE_MAXADDR, NULL, NULL, count * RT2560_TX_DESC_SIZE, 1,
489 count * RT2560_TX_DESC_SIZE, 0, &ring->desc_dmat);
491 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
495 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
496 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
498 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
502 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
503 count * RT2560_TX_DESC_SIZE,
504 rt2560_dma_map_addr, &ring->physaddr, 0);
506 device_printf(sc->sc_dev, "could not load desc DMA map\n");
508 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
513 ring->data = kmalloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF,
516 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
517 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, RT2560_MAX_SCATTER,
518 MCLBYTES, 0, &ring->data_dmat);
520 device_printf(sc->sc_dev, "could not create data DMA tag\n");
524 for (i = 0; i < count; i++) {
525 error = bus_dmamap_create(ring->data_dmat, 0,
528 device_printf(sc->sc_dev, "could not create DMA map\n");
534 fail: rt2560_free_tx_ring(sc, ring);
539 rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
541 struct rt2560_tx_desc *desc;
542 struct rt2560_tx_data *data;
545 for (i = 0; i < ring->count; i++) {
546 desc = &ring->desc[i];
547 data = &ring->data[i];
549 if (data->m != NULL) {
550 bus_dmamap_sync(ring->data_dmat, data->map,
551 BUS_DMASYNC_POSTWRITE);
552 bus_dmamap_unload(ring->data_dmat, data->map);
557 if (data->ni != NULL) {
558 ieee80211_free_node(data->ni);
565 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
568 ring->cur = ring->next = 0;
569 ring->cur_encrypt = ring->next_encrypt = 0;
573 rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
575 struct rt2560_tx_data *data;
578 if (ring->desc != NULL) {
579 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
580 BUS_DMASYNC_POSTWRITE);
581 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
582 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
586 if (ring->desc_dmat != NULL) {
587 bus_dma_tag_destroy(ring->desc_dmat);
588 ring->desc_dmat = NULL;
591 if (ring->data != NULL) {
592 for (i = 0; i < ring->count; i++) {
593 data = &ring->data[i];
595 if (data->m != NULL) {
596 bus_dmamap_sync(ring->data_dmat, data->map,
597 BUS_DMASYNC_POSTWRITE);
598 bus_dmamap_unload(ring->data_dmat, data->map);
603 if (data->ni != NULL) {
604 ieee80211_free_node(data->ni);
608 if (data->map != NULL) {
609 bus_dmamap_destroy(ring->data_dmat, data->map);
614 kfree(ring->data, M_DEVBUF);
618 if (ring->data_dmat != NULL) {
619 bus_dma_tag_destroy(ring->data_dmat);
620 ring->data_dmat = NULL;
625 rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring,
628 struct rt2560_rx_desc *desc;
629 struct rt2560_rx_data *data;
634 ring->cur = ring->next = 0;
635 ring->cur_decrypt = 0;
637 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
638 BUS_SPACE_MAXADDR, NULL, NULL, count * RT2560_RX_DESC_SIZE, 1,
639 count * RT2560_RX_DESC_SIZE, 0, &ring->desc_dmat);
641 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
645 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
646 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
648 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
652 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
653 count * RT2560_RX_DESC_SIZE,
654 rt2560_dma_map_addr, &ring->physaddr, 0);
656 device_printf(sc->sc_dev, "could not load desc DMA map\n");
658 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
663 ring->data = kmalloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF,
667 * Pre-allocate Rx buffers and populate Rx ring.
669 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
670 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0,
673 device_printf(sc->sc_dev, "could not create data DMA tag\n");
677 for (i = 0; i < count; i++) {
678 desc = &sc->rxq.desc[i];
679 data = &sc->rxq.data[i];
681 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
683 device_printf(sc->sc_dev, "could not create DMA map\n");
687 data->m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
688 if (data->m == NULL) {
689 device_printf(sc->sc_dev,
690 "could not allocate rx mbuf\n");
695 error = bus_dmamap_load(ring->data_dmat, data->map,
696 mtod(data->m, void *), MCLBYTES, rt2560_dma_map_addr,
699 device_printf(sc->sc_dev,
700 "could not load rx buf DMA map");
707 desc->flags = htole32(RT2560_RX_BUSY);
708 desc->physaddr = htole32(physaddr);
711 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
715 fail: rt2560_free_rx_ring(sc, ring);
720 rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
724 for (i = 0; i < ring->count; i++) {
725 ring->desc[i].flags = htole32(RT2560_RX_BUSY);
726 ring->data[i].drop = 0;
729 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
731 ring->cur = ring->next = 0;
732 ring->cur_decrypt = 0;
736 rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
738 struct rt2560_rx_data *data;
740 if (ring->desc != NULL) {
741 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
742 BUS_DMASYNC_POSTWRITE);
743 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
744 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
748 if (ring->desc_dmat != NULL) {
749 bus_dma_tag_destroy(ring->desc_dmat);
750 ring->desc_dmat = NULL;
753 if (ring->data != NULL) {
756 for (i = 0; i < ring->count; i++) {
757 data = &ring->data[i];
759 if (data->m != NULL) {
760 bus_dmamap_sync(ring->data_dmat, data->map,
761 BUS_DMASYNC_POSTREAD);
762 bus_dmamap_unload(ring->data_dmat, data->map);
767 if (data->map != NULL) {
768 bus_dmamap_destroy(ring->data_dmat, data->map);
773 kfree(ring->data, M_DEVBUF);
777 if (ring->data_dmat != NULL) {
778 bus_dma_tag_destroy(ring->data_dmat);
779 ring->data_dmat = NULL;
783 static struct ieee80211_node *
784 rt2560_node_alloc(struct ieee80211_node_table *nt)
786 struct rt2560_node *rn;
788 rn = kmalloc(sizeof(struct rt2560_node), M_80211_NODE,
791 return (rn != NULL) ? &rn->ni : NULL;
795 rt2560_media_change(struct ifnet *ifp)
797 struct rt2560_softc *sc = ifp->if_softc;
800 error = ieee80211_media_change(ifp);
801 if (error != ENETRESET)
804 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
810 * This function is called periodically (every 200ms) during scanning to
811 * switch from one channel to another.
814 rt2560_next_scan(void *arg)
816 struct rt2560_softc *sc = arg;
817 struct ieee80211com *ic = &sc->sc_ic;
818 struct ifnet *ifp = ic->ic_ifp;
820 lwkt_serialize_enter(ifp->if_serializer);
821 if (ic->ic_state == IEEE80211_S_SCAN)
822 ieee80211_next_scan(ic);
823 lwkt_serialize_exit(ifp->if_serializer);
827 * This function is called for each node present in the node station table.
830 rt2560_iter_func(void *arg, struct ieee80211_node *ni)
832 struct rt2560_node *rn = (struct rt2560_node *)ni;
834 ral_rssadapt_updatestats(&rn->rssadapt);
838 * This function is called periodically (every 100ms) in RUN state to update
839 * the rate adaptation statistics.
842 rt2560_update_rssadapt(void *arg)
844 struct rt2560_softc *sc = arg;
845 struct ieee80211com *ic = &sc->sc_ic;
846 struct ifnet *ifp = ic->ic_ifp;
848 lwkt_serialize_enter(ifp->if_serializer);
850 ieee80211_iterate_nodes(&ic->ic_sta, rt2560_iter_func, arg);
851 callout_reset(&sc->rssadapt_ch, hz / 10, rt2560_update_rssadapt, sc);
853 lwkt_serialize_exit(ifp->if_serializer);
857 rt2560_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
859 struct rt2560_softc *sc = ic->ic_ifp->if_softc;
860 enum ieee80211_state ostate;
861 struct ieee80211_node *ni;
865 ostate = ic->ic_state;
866 callout_stop(&sc->scan_ch);
869 case IEEE80211_S_INIT:
870 callout_stop(&sc->rssadapt_ch);
872 if (ostate == IEEE80211_S_RUN) {
873 /* abort TSF synchronization */
874 RAL_WRITE(sc, RT2560_CSR14, 0);
876 /* turn association led off */
877 rt2560_update_led(sc, 0, 0);
881 case IEEE80211_S_SCAN:
882 rt2560_set_chan(sc, ic->ic_curchan);
883 callout_reset(&sc->scan_ch, (sc->dwelltime * hz) / 1000,
884 rt2560_next_scan, sc);
887 case IEEE80211_S_AUTH:
888 rt2560_set_chan(sc, ic->ic_curchan);
891 case IEEE80211_S_ASSOC:
892 rt2560_set_chan(sc, ic->ic_curchan);
895 case IEEE80211_S_RUN:
896 rt2560_set_chan(sc, ic->ic_curchan);
900 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
901 rt2560_update_plcp(sc);
902 rt2560_set_basicrates(sc);
903 rt2560_set_bssid(sc, ni->ni_bssid);
906 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
907 ic->ic_opmode == IEEE80211_M_IBSS) {
908 m = ieee80211_beacon_alloc(ic, ni, &sc->sc_bo);
910 device_printf(sc->sc_dev,
911 "could not allocate beacon\n");
916 ieee80211_ref_node(ni);
917 error = rt2560_tx_bcn(sc, m, ni);
922 /* turn assocation led on */
923 rt2560_update_led(sc, 1, 0);
925 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
926 callout_reset(&sc->rssadapt_ch, hz / 10,
927 rt2560_update_rssadapt, sc);
929 rt2560_enable_tsf_sync(sc);
934 return (error != 0) ? error : sc->sc_newstate(ic, nstate, arg);
938 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
942 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
948 /* clock C once before the first command */
949 RT2560_EEPROM_CTL(sc, 0);
951 RT2560_EEPROM_CTL(sc, RT2560_S);
952 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
953 RT2560_EEPROM_CTL(sc, RT2560_S);
955 /* write start bit (1) */
956 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
957 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
959 /* write READ opcode (10) */
960 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
961 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
962 RT2560_EEPROM_CTL(sc, RT2560_S);
963 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
965 /* write address (A5-A0 or A7-A0) */
966 n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
967 for (; n >= 0; n--) {
968 RT2560_EEPROM_CTL(sc, RT2560_S |
969 (((addr >> n) & 1) << RT2560_SHIFT_D));
970 RT2560_EEPROM_CTL(sc, RT2560_S |
971 (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
974 RT2560_EEPROM_CTL(sc, RT2560_S);
976 /* read data Q15-Q0 */
978 for (n = 15; n >= 0; n--) {
979 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
980 tmp = RAL_READ(sc, RT2560_CSR21);
981 val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
982 RT2560_EEPROM_CTL(sc, RT2560_S);
985 RT2560_EEPROM_CTL(sc, 0);
987 /* clear Chip Select and clock C */
988 RT2560_EEPROM_CTL(sc, RT2560_S);
989 RT2560_EEPROM_CTL(sc, 0);
990 RT2560_EEPROM_CTL(sc, RT2560_C);
996 * Some frames were processed by the hardware cipher engine and are ready for
1000 rt2560_encryption_intr(struct rt2560_softc *sc)
1002 struct rt2560_tx_desc *desc;
1005 /* retrieve last descriptor index processed by cipher engine */
1006 hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr;
1007 hw /= RT2560_TX_DESC_SIZE;
1009 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1010 BUS_DMASYNC_POSTREAD);
1012 for (; sc->txq.next_encrypt != hw;) {
1013 desc = &sc->txq.desc[sc->txq.next_encrypt];
1015 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
1016 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY))
1019 /* for TKIP, swap eiv field to fix a bug in ASIC */
1020 if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) ==
1021 RT2560_TX_CIPHER_TKIP)
1022 desc->eiv = bswap32(desc->eiv);
1024 /* mark the frame ready for transmission */
1025 desc->flags |= htole32(RT2560_TX_BUSY | RT2560_TX_VALID);
1027 DPRINTFN(15, ("encryption done idx=%u\n",
1028 sc->txq.next_encrypt));
1030 sc->txq.next_encrypt =
1031 (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
1034 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1035 BUS_DMASYNC_PREWRITE);
1038 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
1042 rt2560_tx_intr(struct rt2560_softc *sc)
1044 struct ieee80211com *ic = &sc->sc_ic;
1045 struct ifnet *ifp = ic->ic_ifp;
1046 struct rt2560_tx_desc *desc;
1047 struct rt2560_tx_data *data;
1048 struct rt2560_node *rn;
1050 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1051 BUS_DMASYNC_POSTREAD);
1054 desc = &sc->txq.desc[sc->txq.next];
1055 data = &sc->txq.data[sc->txq.next];
1057 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
1058 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY) ||
1059 !(le32toh(desc->flags) & RT2560_TX_VALID))
1062 rn = (struct rt2560_node *)data->ni;
1064 switch (le32toh(desc->flags) & RT2560_TX_RESULT_MASK) {
1065 case RT2560_TX_SUCCESS:
1066 DPRINTFN(10, ("data frame sent successfully\n"));
1067 if (data->id.id_node != NULL) {
1068 ral_rssadapt_raise_rate(ic, &rn->rssadapt,
1074 case RT2560_TX_SUCCESS_RETRY:
1075 DPRINTFN(9, ("data frame sent after %u retries\n",
1076 (le32toh(desc->flags) >> 5) & 0x7));
1080 case RT2560_TX_FAIL_RETRY:
1081 DPRINTFN(9, ("sending data frame failed (too much "
1083 if (data->id.id_node != NULL) {
1084 ral_rssadapt_lower_rate(ic, data->ni,
1085 &rn->rssadapt, &data->id);
1090 case RT2560_TX_FAIL_INVALID:
1091 case RT2560_TX_FAIL_OTHER:
1093 device_printf(sc->sc_dev, "sending data frame failed "
1094 "0x%08x\n", le32toh(desc->flags));
1098 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1099 BUS_DMASYNC_POSTWRITE);
1100 bus_dmamap_unload(sc->txq.data_dmat, data->map);
1103 ieee80211_free_node(data->ni);
1106 /* descriptor is no longer valid */
1107 desc->flags &= ~htole32(RT2560_TX_VALID);
1109 DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next));
1112 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
1115 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1116 BUS_DMASYNC_PREWRITE);
1118 sc->sc_tx_timer = 0;
1119 ifp->if_flags &= ~IFF_OACTIVE;
1124 rt2560_prio_intr(struct rt2560_softc *sc)
1126 struct ieee80211com *ic = &sc->sc_ic;
1127 struct ifnet *ifp = ic->ic_ifp;
1128 struct rt2560_tx_desc *desc;
1129 struct rt2560_tx_data *data;
1131 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1132 BUS_DMASYNC_POSTREAD);
1135 desc = &sc->prioq.desc[sc->prioq.next];
1136 data = &sc->prioq.data[sc->prioq.next];
1138 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
1139 !(le32toh(desc->flags) & RT2560_TX_VALID))
1142 switch (le32toh(desc->flags) & RT2560_TX_RESULT_MASK) {
1143 case RT2560_TX_SUCCESS:
1144 DPRINTFN(10, ("mgt frame sent successfully\n"));
1147 case RT2560_TX_SUCCESS_RETRY:
1148 DPRINTFN(9, ("mgt frame sent after %u retries\n",
1149 (le32toh(desc->flags) >> 5) & 0x7));
1152 case RT2560_TX_FAIL_RETRY:
1153 DPRINTFN(9, ("sending mgt frame failed (too much "
1157 case RT2560_TX_FAIL_INVALID:
1158 case RT2560_TX_FAIL_OTHER:
1160 device_printf(sc->sc_dev, "sending mgt frame failed "
1161 "0x%08x\n", le32toh(desc->flags));
1164 bus_dmamap_sync(sc->prioq.data_dmat, data->map,
1165 BUS_DMASYNC_POSTWRITE);
1166 bus_dmamap_unload(sc->prioq.data_dmat, data->map);
1169 ieee80211_free_node(data->ni);
1172 /* descriptor is no longer valid */
1173 desc->flags &= ~htole32(RT2560_TX_VALID);
1175 DPRINTFN(15, ("prio done idx=%u\n", sc->prioq.next));
1178 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
1181 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1182 BUS_DMASYNC_PREWRITE);
1184 sc->sc_tx_timer = 0;
1185 ifp->if_flags &= ~IFF_OACTIVE;
1190 * Some frames were processed by the hardware cipher engine and are ready for
1191 * transmission to the IEEE802.11 layer.
1194 rt2560_decryption_intr(struct rt2560_softc *sc)
1196 struct ieee80211com *ic = &sc->sc_ic;
1197 struct ifnet *ifp = ic->ic_ifp;
1198 struct rt2560_rx_desc *desc;
1199 struct rt2560_rx_data *data;
1200 bus_addr_t physaddr;
1201 struct ieee80211_frame *wh;
1202 struct ieee80211_node *ni;
1203 struct rt2560_node *rn;
1204 struct mbuf *mnew, *m;
1207 /* retrieve last decriptor index processed by cipher engine */
1208 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr;
1209 hw /= RT2560_RX_DESC_SIZE;
1211 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1212 BUS_DMASYNC_POSTREAD);
1214 for (; sc->rxq.cur_decrypt != hw;) {
1215 desc = &sc->rxq.desc[sc->rxq.cur_decrypt];
1216 data = &sc->rxq.data[sc->rxq.cur_decrypt];
1218 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1219 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1227 if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
1228 (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) {
1234 * Try to allocate a new mbuf for this ring element and load it
1235 * before processing the current mbuf. If the ring element
1236 * cannot be loaded, drop the received packet and reuse the old
1237 * mbuf. In the unlikely case that the old mbuf can't be
1238 * reloaded either, explicitly panic.
1240 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1246 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1247 BUS_DMASYNC_POSTREAD);
1248 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1250 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1251 mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr,
1256 /* try to reload the old mbuf */
1257 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1258 mtod(data->m, void *), MCLBYTES,
1259 rt2560_dma_map_addr, &physaddr, 0);
1261 /* very unlikely that it will fail... */
1262 panic("%s: could not load old rx mbuf",
1263 device_get_name(sc->sc_dev));
1270 * New mbuf successfully loaded, update Rx ring and continue
1275 desc->physaddr = htole32(physaddr);
1278 m->m_pkthdr.rcvif = ifp;
1279 m->m_pkthdr.len = m->m_len =
1280 (le32toh(desc->flags) >> 16) & 0xfff;
1282 if (sc->sc_drvbpf != NULL) {
1283 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
1284 uint32_t tsf_lo, tsf_hi;
1286 /* get timestamp (low and high 32 bits) */
1287 tsf_hi = RAL_READ(sc, RT2560_CSR17);
1288 tsf_lo = RAL_READ(sc, RT2560_CSR16);
1291 htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1293 tap->wr_rate = rt2560_rxrate(desc);
1294 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1295 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1296 tap->wr_antenna = sc->rx_ant;
1297 tap->wr_antsignal = desc->rssi;
1299 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1302 wh = mtod(m, struct ieee80211_frame *);
1303 ni = ieee80211_find_rxnode(ic,
1304 (struct ieee80211_frame_min *)wh);
1306 /* send the frame to the 802.11 layer */
1307 ieee80211_input(ic, m, ni, desc->rssi, 0);
1309 /* give rssi to the rate adatation algorithm */
1310 rn = (struct rt2560_node *)ni;
1311 ral_rssadapt_input(ic, ni, &rn->rssadapt, desc->rssi);
1313 /* node is no longer needed */
1314 ieee80211_free_node(ni);
1316 skip: desc->flags = htole32(RT2560_RX_BUSY);
1318 DPRINTFN(15, ("decryption done idx=%u\n", sc->rxq.cur_decrypt));
1320 sc->rxq.cur_decrypt =
1321 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1324 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1325 BUS_DMASYNC_PREWRITE);
1329 * Some frames were received. Pass them to the hardware cipher engine before
1330 * sending them to the 802.11 layer.
1333 rt2560_rx_intr(struct rt2560_softc *sc)
1335 struct rt2560_rx_desc *desc;
1336 struct rt2560_rx_data *data;
1338 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1339 BUS_DMASYNC_POSTREAD);
1342 desc = &sc->rxq.desc[sc->rxq.cur];
1343 data = &sc->rxq.data[sc->rxq.cur];
1345 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1346 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1351 if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) ||
1352 (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) {
1354 * This should not happen since we did not request
1355 * to receive those frames when we filled RXCSR0.
1357 DPRINTFN(5, ("PHY or CRC error flags 0x%08x\n",
1358 le32toh(desc->flags)));
1362 if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
1363 DPRINTFN(5, ("bad length\n"));
1367 /* mark the frame for decryption */
1368 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
1370 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1372 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1375 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1376 BUS_DMASYNC_PREWRITE);
1379 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1383 * This function is called periodically in IBSS mode when a new beacon must be
1387 rt2560_beacon_expire(struct rt2560_softc *sc)
1389 struct ieee80211com *ic = &sc->sc_ic;
1390 struct rt2560_tx_data *data;
1392 if (ic->ic_opmode != IEEE80211_M_IBSS &&
1393 ic->ic_opmode != IEEE80211_M_HOSTAP)
1396 data = &sc->bcnq.data[sc->bcnq.next];
1398 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1399 bus_dmamap_unload(sc->bcnq.data_dmat, data->map);
1401 ieee80211_beacon_update(ic, data->ni, &sc->sc_bo, data->m, 1);
1403 if (ic->ic_rawbpf != NULL)
1404 bpf_mtap(ic->ic_rawbpf, data->m);
1406 rt2560_tx_bcn(sc, data->m, data->ni);
1408 DPRINTFN(15, ("beacon expired\n"));
1410 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1415 rt2560_wakeup_expire(struct rt2560_softc *sc)
1417 DPRINTFN(2, ("wakeup expired\n"));
1421 rt2560_intr(void *arg)
1423 struct rt2560_softc *sc = arg;
1424 struct ifnet *ifp = &sc->sc_ic.ic_if;
1427 /* disable interrupts */
1428 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1430 /* don't re-enable interrupts if we're shutting down */
1431 if (!(ifp->if_flags & IFF_RUNNING))
1434 r = RAL_READ(sc, RT2560_CSR7);
1435 RAL_WRITE(sc, RT2560_CSR7, r);
1437 if (r & RT2560_BEACON_EXPIRE)
1438 rt2560_beacon_expire(sc);
1440 if (r & RT2560_WAKEUP_EXPIRE)
1441 rt2560_wakeup_expire(sc);
1443 if (r & RT2560_PRIO_DONE)
1444 rt2560_prio_intr(sc);
1446 if (r & (RT2560_TX_DONE | RT2560_ENCRYPTION_DONE)) {
1449 for (i = 0; i < 2; ++i) {
1451 rt2560_encryption_intr(sc);
1455 if (r & (RT2560_DECRYPTION_DONE | RT2560_RX_DONE)) {
1458 for (i = 0; i < 2; ++i) {
1459 rt2560_decryption_intr(sc);
1464 /* re-enable interrupts */
1465 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1468 /* quickly determine if a given rate is CCK or OFDM */
1469 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1471 #define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */
1472 #define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */
1474 #define RAL_SIFS 10 /* us */
1476 #define RT2560_TXRX_TURNAROUND 10 /* us */
1479 * This function is only used by the Rx radiotap code.
1482 rt2560_rxrate(struct rt2560_rx_desc *desc)
1484 if (le32toh(desc->flags) & RT2560_RX_OFDM) {
1485 /* reverse function of rt2560_plcp_signal */
1486 switch (desc->rate) {
1487 case 0xb: return 12;
1488 case 0xf: return 18;
1489 case 0xa: return 24;
1490 case 0xe: return 36;
1491 case 0x9: return 48;
1492 case 0xd: return 72;
1493 case 0x8: return 96;
1494 case 0xc: return 108;
1497 if (desc->rate == 10)
1499 if (desc->rate == 20)
1501 if (desc->rate == 55)
1503 if (desc->rate == 110)
1506 return 2; /* should not get there */
1510 * Return the expected ack rate for a frame transmitted at rate `rate'.
1511 * XXX: this should depend on the destination node basic rate set.
1514 rt2560_ack_rate(struct ieee80211com *ic, int rate)
1523 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1539 /* default to 1Mbps */
1544 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1545 * The function automatically determines the operating mode depending on the
1546 * given rate. `flags' indicates whether short preamble is in use or not.
1549 rt2560_txtime(int len, int rate, uint32_t flags)
1553 if (RAL_RATE_IS_OFDM(rate)) {
1554 /* IEEE Std 802.11a-1999, pp. 37 */
1555 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1556 txtime = 16 + 4 + 4 * txtime + 6;
1558 /* IEEE Std 802.11b-1999, pp. 28 */
1559 txtime = (16 * len + rate - 1) / rate;
1560 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1570 rt2560_plcp_signal(int rate)
1573 /* CCK rates (returned values are device-dependent) */
1576 case 11: return 0x2;
1577 case 22: return 0x3;
1579 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1580 case 12: return 0xb;
1581 case 18: return 0xf;
1582 case 24: return 0xa;
1583 case 36: return 0xe;
1584 case 48: return 0x9;
1585 case 72: return 0xd;
1586 case 96: return 0x8;
1587 case 108: return 0xc;
1589 /* unsupported rates (should not get there) */
1590 default: return 0xff;
1595 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1596 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1598 struct ieee80211com *ic = &sc->sc_ic;
1599 uint16_t plcp_length;
1602 desc->flags = htole32(flags);
1603 desc->flags |= htole32(len << 16);
1604 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY) :
1605 htole32(RT2560_TX_BUSY | RT2560_TX_VALID);
1607 desc->physaddr = htole32(physaddr);
1608 desc->wme = htole16(
1610 RT2560_LOGCWMIN(3) |
1611 RT2560_LOGCWMAX(8));
1613 /* setup PLCP fields */
1614 desc->plcp_signal = rt2560_plcp_signal(rate);
1615 desc->plcp_service = 4;
1617 len += IEEE80211_CRC_LEN;
1618 if (RAL_RATE_IS_OFDM(rate)) {
1619 desc->flags |= htole32(RT2560_TX_OFDM);
1621 plcp_length = len & 0xfff;
1622 desc->plcp_length_hi = plcp_length >> 6;
1623 desc->plcp_length_lo = plcp_length & 0x3f;
1625 plcp_length = (16 * len + rate - 1) / rate;
1627 remainder = (16 * len) % 22;
1628 if (remainder != 0 && remainder < 7)
1629 desc->plcp_service |= RT2560_PLCP_LENGEXT;
1631 desc->plcp_length_hi = plcp_length >> 8;
1632 desc->plcp_length_lo = plcp_length & 0xff;
1634 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1635 desc->plcp_signal |= 0x08;
1640 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1641 struct ieee80211_node *ni)
1643 struct ieee80211com *ic = &sc->sc_ic;
1644 struct rt2560_tx_desc *desc;
1645 struct rt2560_tx_data *data;
1649 desc = &sc->bcnq.desc[sc->bcnq.cur];
1650 data = &sc->bcnq.data[sc->bcnq.cur];
1652 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1654 error = bus_dmamap_load_mbuf(sc->bcnq.data_dmat, data->map, m0,
1655 rt2560_dma_map_mbuf, &paddr,
1658 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1664 if (sc->sc_drvbpf != NULL) {
1665 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1668 tap->wt_rate = rate;
1669 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1670 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1671 tap->wt_antenna = sc->tx_ant;
1673 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1679 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1680 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, paddr);
1682 DPRINTFN(10, ("sending beacon frame len=%u idx=%u rate=%u\n",
1683 m0->m_pkthdr.len, sc->bcnq.cur, rate));
1685 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1686 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map,
1687 BUS_DMASYNC_PREWRITE);
1689 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT;
1695 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1696 struct ieee80211_node *ni)
1698 struct ieee80211com *ic = &sc->sc_ic;
1699 struct rt2560_tx_desc *desc;
1700 struct rt2560_tx_data *data;
1701 struct ieee80211_frame *wh;
1707 desc = &sc->prioq.desc[sc->prioq.cur];
1708 data = &sc->prioq.data[sc->prioq.cur];
1710 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1712 error = bus_dmamap_load_mbuf(sc->prioq.data_dmat, data->map, m0,
1713 rt2560_dma_map_mbuf, &paddr, 0);
1715 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1721 if (sc->sc_drvbpf != NULL) {
1722 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1725 tap->wt_rate = rate;
1726 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1727 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1728 tap->wt_antenna = sc->tx_ant;
1730 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1736 wh = mtod(m0, struct ieee80211_frame *);
1738 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1739 flags |= RT2560_TX_ACK;
1741 dur = rt2560_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) +
1743 *(uint16_t *)wh->i_dur = htole16(dur);
1745 /* tell hardware to add timestamp for probe responses */
1746 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1747 IEEE80211_FC0_TYPE_MGT &&
1748 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1749 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1750 flags |= RT2560_TX_TIMESTAMP;
1753 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0, paddr);
1755 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1756 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1757 BUS_DMASYNC_PREWRITE);
1759 DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n",
1760 m0->m_pkthdr.len, sc->prioq.cur, rate));
1764 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1765 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1771 * Build a RTS control frame.
1773 static struct mbuf *
1774 rt2560_get_rts(struct rt2560_softc *sc, struct ieee80211_frame *wh,
1777 struct ieee80211_frame_rts *rts;
1780 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1782 sc->sc_ic.ic_stats.is_tx_nobuf++;
1783 device_printf(sc->sc_dev, "could not allocate RTS frame\n");
1787 rts = mtod(m, struct ieee80211_frame_rts *);
1789 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1790 IEEE80211_FC0_SUBTYPE_RTS;
1791 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1792 *(uint16_t *)rts->i_dur = htole16(dur);
1793 IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1);
1794 IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2);
1796 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
1802 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1803 struct ieee80211_node *ni)
1805 struct ieee80211com *ic = &sc->sc_ic;
1806 struct rt2560_tx_desc *desc;
1807 struct rt2560_tx_data *data;
1808 struct rt2560_node *rn;
1809 struct ieee80211_rateset *rs;
1810 struct ieee80211_frame *wh;
1811 struct ieee80211_key *k;
1818 wh = mtod(m0, struct ieee80211_frame *);
1820 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
1821 rs = &ic->ic_sup_rates[ic->ic_curmode];
1822 rate = rs->rs_rates[ic->ic_fixed_rate];
1825 rn = (struct rt2560_node *)ni;
1826 ni->ni_txrate = ral_rssadapt_choose(&rn->rssadapt, rs, wh,
1827 m0->m_pkthdr.len, NULL, 0);
1828 rate = rs->rs_rates[ni->ni_txrate];
1830 rate &= IEEE80211_RATE_VAL;
1832 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1833 k = ieee80211_crypto_encap(ic, ni, m0);
1839 /* packet header may have moved, reset our local pointer */
1840 wh = mtod(m0, struct ieee80211_frame *);
1844 * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange
1845 * for directed frames only when the length of the MPDU is greater
1846 * than the length threshold indicated by [...]" ic_rtsthreshold.
1848 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1849 m0->m_pkthdr.len > ic->ic_rtsthreshold) {
1852 int rtsrate, ackrate;
1854 rtsrate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1855 ackrate = rt2560_ack_rate(ic, rate);
1857 dur = rt2560_txtime(m0->m_pkthdr.len + 4, rate, ic->ic_flags) +
1858 rt2560_txtime(RAL_CTS_SIZE, rtsrate, ic->ic_flags) +
1859 rt2560_txtime(RAL_ACK_SIZE, ackrate, ic->ic_flags) +
1862 m = rt2560_get_rts(sc, wh, dur);
1864 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1865 data = &sc->txq.data[sc->txq.cur_encrypt];
1867 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map,
1868 m, rt2560_dma_map_mbuf, &paddr, 0);
1870 device_printf(sc->sc_dev,
1871 "could not map mbuf (error %d)\n", error);
1877 /* avoid multiple free() of the same node for each fragment */
1878 ieee80211_ref_node(ni);
1883 /* RTS frames are not taken into account for rssadapt */
1884 data->id.id_node = NULL;
1886 rt2560_setup_tx_desc(sc, desc, RT2560_TX_ACK |
1887 RT2560_TX_MORE_FRAG, m->m_pkthdr.len, rtsrate, 1, paddr);
1889 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1890 BUS_DMASYNC_PREWRITE);
1893 sc->txq.cur_encrypt =
1894 (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1897 * IEEE Std 802.11-1999: when an RTS/CTS exchange is used, the
1898 * asynchronous data frame shall be transmitted after the CTS
1899 * frame and a SIFS period.
1901 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1904 data = &sc->txq.data[sc->txq.cur_encrypt];
1905 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1907 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map, m0,
1908 rt2560_dma_map_mbuf, &paddr, 0);
1909 if (error != 0 && error != EFBIG) {
1910 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1916 mnew = m_defrag(m0, MB_DONTWAIT);
1918 device_printf(sc->sc_dev,
1919 "could not defragment mbuf\n");
1925 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map,
1926 m0, rt2560_dma_map_mbuf, &paddr,
1929 device_printf(sc->sc_dev,
1930 "could not map mbuf (error %d)\n", error);
1935 /* packet header may have moved, reset our local pointer */
1936 wh = mtod(m0, struct ieee80211_frame *);
1939 if (sc->sc_drvbpf != NULL) {
1940 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1943 tap->wt_rate = rate;
1944 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1945 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1946 tap->wt_antenna = sc->tx_ant;
1948 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1954 /* remember link conditions for rate adaptation algorithm */
1955 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
1956 data->id.id_len = m0->m_pkthdr.len;
1957 data->id.id_rateidx = ni->ni_txrate;
1958 data->id.id_node = ni;
1959 data->id.id_rssi = ni->ni_rssi;
1961 data->id.id_node = NULL;
1963 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1964 flags |= RT2560_TX_ACK;
1966 dur = rt2560_txtime(RAL_ACK_SIZE, rt2560_ack_rate(ic, rate),
1967 ic->ic_flags) + RAL_SIFS;
1968 *(uint16_t *)wh->i_dur = htole16(dur);
1971 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1, paddr);
1973 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1974 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1975 BUS_DMASYNC_PREWRITE);
1977 DPRINTFN(10, ("sending data frame len=%u idx=%u rate=%u\n",
1978 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate));
1982 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1983 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1989 rt2560_start(struct ifnet *ifp)
1991 struct rt2560_softc *sc = ifp->if_softc;
1992 struct ieee80211com *ic = &sc->sc_ic;
1994 struct ether_header *eh;
1995 struct ieee80211_node *ni;
1997 /* prevent management frames from being sent if we're not ready */
1998 if (!(ifp->if_flags & IFF_RUNNING))
2002 IF_POLL(&ic->ic_mgtq, m0);
2004 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2005 ifp->if_flags |= IFF_OACTIVE;
2008 IF_DEQUEUE(&ic->ic_mgtq, m0);
2010 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
2011 m0->m_pkthdr.rcvif = NULL;
2013 if (ic->ic_rawbpf != NULL)
2014 bpf_mtap(ic->ic_rawbpf, m0);
2016 if (rt2560_tx_mgt(sc, m0, ni) != 0)
2020 if (ic->ic_state != IEEE80211_S_RUN)
2022 m0 = ifq_poll(&ifp->if_snd);
2025 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
2026 ifp->if_flags |= IFF_OACTIVE;
2029 m0 = ifq_dequeue(&ifp->if_snd, m0);
2031 if (m0->m_len < sizeof (struct ether_header) &&
2032 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
2035 eh = mtod(m0, struct ether_header *);
2036 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2043 m0 = ieee80211_encap(ic, m0, ni);
2045 ieee80211_free_node(ni);
2049 if (ic->ic_rawbpf != NULL)
2050 bpf_mtap(ic->ic_rawbpf, m0);
2052 if (rt2560_tx_data(sc, m0, ni) != 0) {
2053 ieee80211_free_node(ni);
2059 sc->sc_tx_timer = 5;
2065 rt2560_watchdog(struct ifnet *ifp)
2067 struct rt2560_softc *sc = ifp->if_softc;
2068 struct ieee80211com *ic = &sc->sc_ic;
2072 if (sc->sc_tx_timer > 0) {
2073 if (--sc->sc_tx_timer == 0) {
2074 device_printf(sc->sc_dev, "device timeout\n");
2082 ieee80211_watchdog(ic);
2086 * This function allows for fast channel switching in monitor mode (used by
2087 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
2088 * generate a new beacon frame.
2091 rt2560_reset(struct ifnet *ifp)
2093 struct rt2560_softc *sc = ifp->if_softc;
2094 struct ieee80211com *ic = &sc->sc_ic;
2096 if (ic->ic_opmode != IEEE80211_M_MONITOR)
2099 rt2560_set_chan(sc, ic->ic_curchan);
2105 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
2107 struct rt2560_softc *sc = ifp->if_softc;
2108 struct ieee80211com *ic = &sc->sc_ic;
2113 if (ifp->if_flags & IFF_UP) {
2114 if (ifp->if_flags & IFF_RUNNING)
2115 rt2560_update_promisc(sc);
2119 if (ifp->if_flags & IFF_RUNNING)
2125 error = ieee80211_ioctl(ic, cmd, data, cr);
2128 if (error == ENETRESET) {
2129 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
2130 (IFF_UP | IFF_RUNNING) &&
2131 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
2140 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2145 for (ntries = 0; ntries < 100; ntries++) {
2146 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2150 if (ntries == 100) {
2151 device_printf(sc->sc_dev, "could not write to BBP\n");
2155 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2156 RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2158 DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg, val));
2162 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2167 val = RT2560_BBP_BUSY | reg << 8;
2168 RAL_WRITE(sc, RT2560_BBPCSR, val);
2170 for (ntries = 0; ntries < 100; ntries++) {
2171 val = RAL_READ(sc, RT2560_BBPCSR);
2172 if (!(val & RT2560_BBP_BUSY))
2177 device_printf(sc->sc_dev, "could not read from BBP\n");
2182 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2187 for (ntries = 0; ntries < 100; ntries++) {
2188 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2192 if (ntries == 100) {
2193 device_printf(sc->sc_dev, "could not write to RF\n");
2197 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2199 RAL_WRITE(sc, RT2560_RFCSR, tmp);
2201 /* remember last written value in sc */
2202 sc->rf_regs[reg] = val;
2204 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
2208 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2210 struct ieee80211com *ic = &sc->sc_ic;
2214 chan = ieee80211_chan2ieee(ic, c);
2215 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2218 if (IEEE80211_IS_CHAN_2GHZ(c))
2219 power = min(sc->txpow[chan - 1], 31);
2223 /* adjust txpower using ifconfig settings */
2224 power -= (100 - ic->ic_txpowlimit) / 8;
2226 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
2228 switch (sc->rf_rev) {
2229 case RT2560_RF_2522:
2230 rt2560_rf_write(sc, RAL_RF1, 0x00814);
2231 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2232 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2235 case RT2560_RF_2523:
2236 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2237 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2238 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2239 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2242 case RT2560_RF_2524:
2243 rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2244 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2245 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2246 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2249 case RT2560_RF_2525:
2250 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2251 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2252 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2253 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2255 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2256 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2257 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2258 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2261 case RT2560_RF_2525E:
2262 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2263 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2264 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2265 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2268 case RT2560_RF_2526:
2269 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2270 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2271 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2273 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2274 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2275 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2279 case RT2560_RF_5222:
2280 for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2282 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2283 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2284 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2285 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2289 if (ic->ic_state != IEEE80211_S_SCAN) {
2290 /* set Japan filter bit for channel 14 */
2291 tmp = rt2560_bbp_read(sc, 70);
2293 tmp &= ~RT2560_JAPAN_FILTER;
2295 tmp |= RT2560_JAPAN_FILTER;
2297 rt2560_bbp_write(sc, 70, tmp);
2299 /* clear CRC errors */
2300 RAL_READ(sc, RT2560_CNT0);
2306 * Disable RF auto-tuning.
2309 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2313 if (sc->rf_rev != RT2560_RF_2523) {
2314 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
2315 rt2560_rf_write(sc, RAL_RF1, tmp);
2318 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
2319 rt2560_rf_write(sc, RAL_RF3, tmp);
2321 DPRINTFN(2, ("disabling RF autotune\n"));
2326 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2330 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2332 struct ieee80211com *ic = &sc->sc_ic;
2333 uint16_t logcwmin, preload;
2336 /* first, disable TSF synchronization */
2337 RAL_WRITE(sc, RT2560_CSR14, 0);
2339 tmp = 16 * ic->ic_bss->ni_intval;
2340 RAL_WRITE(sc, RT2560_CSR12, tmp);
2342 RAL_WRITE(sc, RT2560_CSR13, 0);
2345 preload = (ic->ic_opmode == IEEE80211_M_STA) ? 384 : 1024;
2346 tmp = logcwmin << 16 | preload;
2347 RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2349 /* finally, enable TSF synchronization */
2350 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2351 if (ic->ic_opmode == IEEE80211_M_STA)
2352 tmp |= RT2560_ENABLE_TSF_SYNC(1);
2354 tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2355 RT2560_ENABLE_BEACON_GENERATOR;
2356 RAL_WRITE(sc, RT2560_CSR14, tmp);
2358 DPRINTF(("enabling TSF synchronization\n"));
2362 rt2560_update_plcp(struct rt2560_softc *sc)
2364 struct ieee80211com *ic = &sc->sc_ic;
2366 /* no short preamble for 1Mbps */
2367 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2369 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2370 /* values taken from the reference driver */
2371 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401);
2372 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2373 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403);
2375 /* same values as above or'ed 0x8 */
2376 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409);
2377 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2378 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b);
2381 DPRINTF(("updating PLCP for %s preamble\n",
2382 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"));
2386 * This function can be called by ieee80211_set_shortslottime(). Refer to
2387 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
2390 rt2560_update_slot(struct ifnet *ifp)
2392 struct rt2560_softc *sc = ifp->if_softc;
2393 struct ieee80211com *ic = &sc->sc_ic;
2395 uint16_t tx_sifs, tx_pifs, tx_difs, eifs;
2398 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2400 /* update the MAC slot boundaries */
2401 tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND;
2402 tx_pifs = tx_sifs + slottime;
2403 tx_difs = tx_sifs + 2 * slottime;
2404 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2406 tmp = RAL_READ(sc, RT2560_CSR11);
2407 tmp = (tmp & ~0x1f00) | slottime << 8;
2408 RAL_WRITE(sc, RT2560_CSR11, tmp);
2410 tmp = tx_pifs << 16 | tx_sifs;
2411 RAL_WRITE(sc, RT2560_CSR18, tmp);
2413 tmp = eifs << 16 | tx_difs;
2414 RAL_WRITE(sc, RT2560_CSR19, tmp);
2416 DPRINTF(("setting slottime to %uus\n", slottime));
2420 rt2560_set_basicrates(struct rt2560_softc *sc)
2422 struct ieee80211com *ic = &sc->sc_ic;
2424 /* update basic rate set */
2425 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2426 /* 11b basic rates: 1, 2Mbps */
2427 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2428 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
2429 /* 11a basic rates: 6, 12, 24Mbps */
2430 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2432 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2433 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2438 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2442 /* set ON period to 70ms and OFF period to 30ms */
2443 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2444 RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2448 rt2560_set_bssid(struct rt2560_softc *sc, uint8_t *bssid)
2452 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2453 RAL_WRITE(sc, RT2560_CSR5, tmp);
2455 tmp = bssid[4] | bssid[5] << 8;
2456 RAL_WRITE(sc, RT2560_CSR6, tmp);
2458 DPRINTF(("setting BSSID to %6D\n", bssid, ":"));
2462 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2466 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2467 RAL_WRITE(sc, RT2560_CSR3, tmp);
2469 tmp = addr[4] | addr[5] << 8;
2470 RAL_WRITE(sc, RT2560_CSR4, tmp);
2472 DPRINTF(("setting MAC address to %6D\n", addr, ":"));
2476 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2480 tmp = RAL_READ(sc, RT2560_CSR3);
2481 addr[0] = tmp & 0xff;
2482 addr[1] = (tmp >> 8) & 0xff;
2483 addr[2] = (tmp >> 16) & 0xff;
2484 addr[3] = (tmp >> 24);
2486 tmp = RAL_READ(sc, RT2560_CSR4);
2487 addr[4] = tmp & 0xff;
2488 addr[5] = (tmp >> 8) & 0xff;
2492 rt2560_update_promisc(struct rt2560_softc *sc)
2494 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2497 tmp = RAL_READ(sc, RT2560_RXCSR0);
2499 tmp &= ~RT2560_DROP_NOT_TO_ME;
2500 if (!(ifp->if_flags & IFF_PROMISC))
2501 tmp |= RT2560_DROP_NOT_TO_ME;
2503 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2505 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2506 "entering" : "leaving"));
2510 rt2560_get_rf(int rev)
2513 case RT2560_RF_2522: return "RT2522";
2514 case RT2560_RF_2523: return "RT2523";
2515 case RT2560_RF_2524: return "RT2524";
2516 case RT2560_RF_2525: return "RT2525";
2517 case RT2560_RF_2525E: return "RT2525e";
2518 case RT2560_RF_2526: return "RT2526";
2519 case RT2560_RF_5222: return "RT5222";
2520 default: return "unknown";
2525 rt2560_read_eeprom(struct rt2560_softc *sc)
2530 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2531 sc->rf_rev = (val >> 11) & 0x7;
2532 sc->hw_radio = (val >> 10) & 0x1;
2533 sc->led_mode = (val >> 6) & 0x7;
2534 sc->rx_ant = (val >> 4) & 0x3;
2535 sc->tx_ant = (val >> 2) & 0x3;
2536 sc->nb_ant = val & 0x3;
2538 /* read default values for BBP registers */
2539 for (i = 0; i < 16; i++) {
2540 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2541 sc->bbp_prom[i].reg = val >> 8;
2542 sc->bbp_prom[i].val = val & 0xff;
2545 /* read Tx power for all b/g channels */
2546 for (i = 0; i < 14 / 2; i++) {
2547 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2548 sc->txpow[i * 2] = val >> 8;
2549 sc->txpow[i * 2 + 1] = val & 0xff;
2554 rt2560_bbp_init(struct rt2560_softc *sc)
2556 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2559 /* wait for BBP to be ready */
2560 for (ntries = 0; ntries < 100; ntries++) {
2561 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2565 if (ntries == 100) {
2566 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2570 /* initialize BBP registers to default values */
2571 for (i = 0; i < N(rt2560_def_bbp); i++) {
2572 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2573 rt2560_def_bbp[i].val);
2576 /* initialize BBP registers to values stored in EEPROM */
2577 for (i = 0; i < 16; i++) {
2578 if (sc->bbp_prom[i].reg == 0xff)
2580 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2589 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2594 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2596 tx |= RT2560_BBP_ANTA;
2597 else if (antenna == 2)
2598 tx |= RT2560_BBP_ANTB;
2600 tx |= RT2560_BBP_DIVERSITY;
2602 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2603 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2604 sc->rf_rev == RT2560_RF_5222)
2605 tx |= RT2560_BBP_FLIPIQ;
2607 rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2609 /* update values for CCK and OFDM in BBPCSR1 */
2610 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2611 tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2612 RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2616 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2620 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2622 rx |= RT2560_BBP_ANTA;
2623 else if (antenna == 2)
2624 rx |= RT2560_BBP_ANTB;
2626 rx |= RT2560_BBP_DIVERSITY;
2628 /* need to force no I/Q flip for RF 2525e and 2526 */
2629 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2630 rx &= ~RT2560_BBP_FLIPIQ;
2632 rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2636 rt2560_init(void *priv)
2638 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2639 struct rt2560_softc *sc = priv;
2640 struct ieee80211com *ic = &sc->sc_ic;
2641 struct ifnet *ifp = ic->ic_ifp;
2647 /* setup tx rings */
2648 tmp = RT2560_PRIO_RING_COUNT << 24 |
2649 RT2560_ATIM_RING_COUNT << 16 |
2650 RT2560_TX_RING_COUNT << 8 |
2651 RT2560_TX_DESC_SIZE;
2653 /* rings must be initialized in this exact order */
2654 RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2655 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2656 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2657 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2658 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2661 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2663 RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2664 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2666 /* initialize MAC registers to default values */
2667 for (i = 0; i < N(rt2560_def_mac); i++)
2668 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2670 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2671 rt2560_set_macaddr(sc, ic->ic_myaddr);
2673 /* set basic rate set (will be updated later) */
2674 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2676 rt2560_set_txantenna(sc, sc->tx_ant);
2677 rt2560_set_rxantenna(sc, sc->rx_ant);
2678 rt2560_update_slot(ifp);
2679 rt2560_update_plcp(sc);
2680 rt2560_update_led(sc, 0, 0);
2682 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2683 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2685 if (rt2560_bbp_init(sc) != 0) {
2690 /* set default BSS channel */
2691 rt2560_set_chan(sc, ic->ic_curchan);
2694 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2695 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2696 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2697 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2698 tmp |= RT2560_DROP_TODS;
2699 if (!(ifp->if_flags & IFF_PROMISC))
2700 tmp |= RT2560_DROP_NOT_TO_ME;
2702 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2704 /* clear old FCS and Rx FIFO errors */
2705 RAL_READ(sc, RT2560_CNT0);
2706 RAL_READ(sc, RT2560_CNT4);
2708 /* clear any pending interrupts */
2709 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2711 /* enable interrupts */
2712 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2714 ifp->if_flags &= ~IFF_OACTIVE;
2715 ifp->if_flags |= IFF_RUNNING;
2718 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2721 ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
2722 for (i = 0; i < IEEE80211_WEP_NKID; ++i) {
2723 struct ieee80211_key *wk = &ic->ic_nw_keys[i];
2725 if (wk->wk_keylen == 0)
2727 if (wk->wk_flags & IEEE80211_KEY_XMIT)
2728 wk->wk_flags |= IEEE80211_KEY_SWCRYPT;
2732 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2733 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2734 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2736 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2741 rt2560_stop(void *priv)
2743 struct rt2560_softc *sc = priv;
2744 struct ieee80211com *ic = &sc->sc_ic;
2745 struct ifnet *ifp = ic->ic_ifp;
2747 sc->sc_tx_timer = 0;
2749 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2751 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2754 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2757 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2759 /* reset ASIC (imply reset BBP) */
2760 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2761 RAL_WRITE(sc, RT2560_CSR1, 0);
2763 /* disable interrupts */
2764 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2766 /* reset Tx and Rx rings */
2767 rt2560_reset_tx_ring(sc, &sc->txq);
2768 rt2560_reset_tx_ring(sc, &sc->atimq);
2769 rt2560_reset_tx_ring(sc, &sc->prioq);
2770 rt2560_reset_tx_ring(sc, &sc->bcnq);
2771 rt2560_reset_rx_ring(sc, &sc->rxq);
2775 rt2560_dma_map_mbuf(void *arg, bus_dma_segment_t *seg, int nseg,
2776 bus_size_t map_size __unused, int error)
2781 KASSERT(nseg == 1, ("too many dma segments\n"));
2782 *((bus_addr_t *)arg) = seg->ds_addr;