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.2 2006/09/05 00:55:40 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>
36 #include <sys/endian.h>
37 #include <sys/serialize.h>
39 #include <machine/bus.h>
40 #include <machine/resource.h>
41 #include <machine/clock.h>
46 #include <net/if_arp.h>
47 #include <net/ethernet.h>
48 #include <net/if_dl.h>
49 #include <net/if_media.h>
50 #include <net/if_types.h>
51 #include <net/ifq_var.h>
53 #include <netproto/802_11/ieee80211_var.h>
54 #include <netproto/802_11/ieee80211_radiotap.h>
56 #include <netinet/in.h>
57 #include <netinet/in_systm.h>
58 #include <netinet/in_var.h>
59 #include <netinet/ip.h>
60 #include <netinet/if_ether.h>
62 #include <dev/netif/ral/if_ralrate.h>
63 #include <dev/netif/ral/rt2560reg.h>
64 #include <dev/netif/ral/rt2560var.h>
67 #define DPRINTF(x) do { if (ral_debug > 0) printf x; } while (0)
68 #define DPRINTFN(n, x) do { if (ral_debug >= (n)) printf x; } while (0)
72 #define DPRINTFN(n, x)
75 static void rt2560_dma_map_addr(void *, bus_dma_segment_t *, int,
77 static void rt2560_dma_map_mbuf(void *, bus_dma_segment_t *, int,
79 static int rt2560_alloc_tx_ring(struct rt2560_softc *,
80 struct rt2560_tx_ring *, int);
81 static void rt2560_reset_tx_ring(struct rt2560_softc *,
82 struct rt2560_tx_ring *);
83 static void rt2560_free_tx_ring(struct rt2560_softc *,
84 struct rt2560_tx_ring *);
85 static int rt2560_alloc_rx_ring(struct rt2560_softc *,
86 struct rt2560_rx_ring *, int);
87 static void rt2560_reset_rx_ring(struct rt2560_softc *,
88 struct rt2560_rx_ring *);
89 static void rt2560_free_rx_ring(struct rt2560_softc *,
90 struct rt2560_rx_ring *);
91 static struct ieee80211_node *rt2560_node_alloc(
92 struct ieee80211_node_table *);
93 static int rt2560_media_change(struct ifnet *);
94 static void rt2560_next_scan(void *);
95 static void rt2560_iter_func(void *, struct ieee80211_node *);
96 static void rt2560_update_rssadapt(void *);
97 static int rt2560_newstate(struct ieee80211com *,
98 enum ieee80211_state, int);
99 static uint16_t rt2560_eeprom_read(struct rt2560_softc *, uint8_t);
100 static void rt2560_encryption_intr(struct rt2560_softc *);
101 static void rt2560_tx_intr(struct rt2560_softc *);
102 static void rt2560_prio_intr(struct rt2560_softc *);
103 static void rt2560_decryption_intr(struct rt2560_softc *);
104 static void rt2560_rx_intr(struct rt2560_softc *);
105 static void rt2560_beacon_expire(struct rt2560_softc *);
106 static void rt2560_wakeup_expire(struct rt2560_softc *);
107 static uint8_t rt2560_rxrate(struct rt2560_rx_desc *);
108 static int rt2560_ack_rate(struct ieee80211com *, int);
109 static uint16_t rt2560_txtime(int, int, uint32_t);
110 static uint8_t rt2560_plcp_signal(int);
111 static void rt2560_setup_tx_desc(struct rt2560_softc *,
112 struct rt2560_tx_desc *, uint32_t, int, int, int,
114 static int rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *,
115 struct ieee80211_node *);
116 static int rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *,
117 struct ieee80211_node *);
118 static struct mbuf *rt2560_get_rts(struct rt2560_softc *,
119 struct ieee80211_frame *, uint16_t);
120 static int rt2560_tx_data(struct rt2560_softc *, struct mbuf *,
121 struct ieee80211_node *);
122 static void rt2560_start(struct ifnet *);
123 static void rt2560_watchdog(struct ifnet *);
124 static int rt2560_reset(struct ifnet *);
125 static int rt2560_ioctl(struct ifnet *, u_long, caddr_t,
127 static void rt2560_bbp_write(struct rt2560_softc *, uint8_t,
129 static uint8_t rt2560_bbp_read(struct rt2560_softc *, uint8_t);
130 static void rt2560_rf_write(struct rt2560_softc *, uint8_t,
132 static void rt2560_set_chan(struct rt2560_softc *,
133 struct ieee80211_channel *);
135 static void rt2560_disable_rf_tune(struct rt2560_softc *);
137 static void rt2560_enable_tsf_sync(struct rt2560_softc *);
138 static void rt2560_update_plcp(struct rt2560_softc *);
139 static void rt2560_update_slot(struct ifnet *);
140 static void rt2560_set_basicrates(struct rt2560_softc *);
141 static void rt2560_update_led(struct rt2560_softc *, int, int);
142 static void rt2560_set_bssid(struct rt2560_softc *, uint8_t *);
143 static void rt2560_set_macaddr(struct rt2560_softc *, uint8_t *);
144 static void rt2560_get_macaddr(struct rt2560_softc *, uint8_t *);
145 static void rt2560_update_promisc(struct rt2560_softc *);
146 static const char *rt2560_get_rf(int);
147 static void rt2560_read_eeprom(struct rt2560_softc *);
148 static int rt2560_bbp_init(struct rt2560_softc *);
149 static void rt2560_set_txantenna(struct rt2560_softc *, int);
150 static void rt2560_set_rxantenna(struct rt2560_softc *, int);
151 static void rt2560_init(void *);
152 static void rt2560_stop(void *);
153 static void rt2560_intr(void *);
156 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
158 static const struct ieee80211_rateset rt2560_rateset_11a =
159 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
161 static const struct ieee80211_rateset rt2560_rateset_11b =
162 { 4, { 2, 4, 11, 22 } };
164 static const struct ieee80211_rateset rt2560_rateset_11g =
165 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
167 static const struct {
170 } rt2560_def_mac[] = {
174 static const struct {
177 } rt2560_def_bbp[] = {
181 static const uint32_t rt2560_rf2522_r2[] = RT2560_RF2522_R2;
182 static const uint32_t rt2560_rf2523_r2[] = RT2560_RF2523_R2;
183 static const uint32_t rt2560_rf2524_r2[] = RT2560_RF2524_R2;
184 static const uint32_t rt2560_rf2525_r2[] = RT2560_RF2525_R2;
185 static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2;
186 static const uint32_t rt2560_rf2525e_r2[] = RT2560_RF2525E_R2;
187 static const uint32_t rt2560_rf2526_r2[] = RT2560_RF2526_R2;
188 static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2;
190 static const struct {
193 } rt2560_rf5222[] = {
198 rt2560_attach(device_t dev, int id)
200 struct rt2560_softc *sc = device_get_softc(dev);
201 struct ieee80211com *ic = &sc->sc_ic;
202 struct ifnet *ifp = &ic->ic_if;
205 callout_init(&sc->scan_ch);
206 callout_init(&sc->rssadapt_ch);
209 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irq_rid,
210 RF_ACTIVE | RF_SHAREABLE);
211 if (sc->sc_irq == NULL) {
212 device_printf(dev, "could not allocate interrupt resource\n");
216 /* retrieve RT2560 rev. no */
217 sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
219 /* retrieve MAC address */
220 rt2560_get_macaddr(sc, ic->ic_myaddr);
222 /* retrieve RF rev. no and various other things from EEPROM */
223 rt2560_read_eeprom(sc);
225 device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n",
226 sc->asic_rev, rt2560_get_rf(sc->rf_rev));
229 * Allocate Tx and Rx rings.
231 error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT);
233 device_printf(sc->sc_dev, "could not allocate Tx ring\n");
237 error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT);
239 device_printf(sc->sc_dev, "could not allocate ATIM ring\n");
243 error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT);
245 device_printf(sc->sc_dev, "could not allocate Prio ring\n");
249 error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT);
251 device_printf(sc->sc_dev, "could not allocate Beacon ring\n");
255 error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT);
257 device_printf(sc->sc_dev, "could not allocate Rx ring\n");
261 sysctl_ctx_init(&sc->sysctl_ctx);
262 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
263 SYSCTL_STATIC_CHILDREN(_hw),
265 device_get_nameunit(dev),
267 if (sc->sysctl_tree == NULL) {
268 device_printf(dev, "could not add sysctl node\n");
274 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
275 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
276 ifp->if_init = rt2560_init;
277 ifp->if_ioctl = rt2560_ioctl;
278 ifp->if_start = rt2560_start;
279 ifp->if_watchdog = rt2560_watchdog;
280 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
281 ifq_set_ready(&ifp->if_snd);
283 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
284 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
285 ic->ic_state = IEEE80211_S_INIT;
287 /* set device capabilities */
289 IEEE80211_C_IBSS | /* IBSS mode supported */
290 IEEE80211_C_MONITOR | /* monitor mode supported */
291 IEEE80211_C_HOSTAP | /* HostAp mode supported */
292 IEEE80211_C_TXPMGT | /* tx power management */
293 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
294 IEEE80211_C_SHSLOT | /* short slot time supported */
295 IEEE80211_C_WEP | /* WEP */
296 IEEE80211_C_WPA; /* 802.11i */
298 if (sc->rf_rev == RT2560_RF_5222) {
299 /* set supported .11a rates */
300 ic->ic_sup_rates[IEEE80211_MODE_11A] = rt2560_rateset_11a;
302 /* set supported .11a channels */
303 for (i = 36; i <= 64; i += 4) {
304 ic->ic_channels[i].ic_freq =
305 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
306 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
308 for (i = 100; i <= 140; i += 4) {
309 ic->ic_channels[i].ic_freq =
310 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
311 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
313 for (i = 149; i <= 161; i += 4) {
314 ic->ic_channels[i].ic_freq =
315 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
316 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
320 /* set supported .11b and .11g rates */
321 ic->ic_sup_rates[IEEE80211_MODE_11B] = rt2560_rateset_11b;
322 ic->ic_sup_rates[IEEE80211_MODE_11G] = rt2560_rateset_11g;
324 /* set supported .11b and .11g channels (1 through 14) */
325 for (i = 1; i <= 14; i++) {
326 ic->ic_channels[i].ic_freq =
327 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
328 ic->ic_channels[i].ic_flags =
329 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
330 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
333 ieee80211_ifattach(ic);
334 ic->ic_node_alloc = rt2560_node_alloc;
335 ic->ic_updateslot = rt2560_update_slot;
336 ic->ic_reset = rt2560_reset;
337 /* enable s/w bmiss handling in sta mode */
338 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
340 /* override state transition machine */
341 sc->sc_newstate = ic->ic_newstate;
342 ic->ic_newstate = rt2560_newstate;
343 ieee80211_media_init(ic, rt2560_media_change, ieee80211_media_status);
345 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
346 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
348 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
349 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
350 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2560_RX_RADIOTAP_PRESENT);
352 sc->sc_txtap_len = sizeof sc->sc_txtapu;
353 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
354 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2560_TX_RADIOTAP_PRESENT);
357 * Add a few sysctl knobs.
361 SYSCTL_ADD_INT(&sc->sysctl_ctx,
362 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
363 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)");
365 SYSCTL_ADD_INT(&sc->sysctl_ctx,
366 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
367 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)");
369 SYSCTL_ADD_INT(&sc->sysctl_ctx,
370 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "dwell",
371 CTLFLAG_RW, &sc->dwelltime, 0,
372 "channel dwell time (ms) for AP/station scanning");
374 error = bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE, rt2560_intr,
375 sc, &sc->sc_ih, ifp->if_serializer);
377 device_printf(dev, "could not set up interrupt\n");
379 ieee80211_ifdetach(ic);
384 ieee80211_announce(ic);
392 rt2560_detach(void *xsc)
394 struct rt2560_softc *sc = xsc;
395 struct ieee80211com *ic = &sc->sc_ic;
396 struct ifnet *ifp = ic->ic_ifp;
398 if (device_is_attached(sc->sc_dev)) {
399 lwkt_serialize_enter(ifp->if_serializer);
401 callout_stop(&sc->scan_ch);
402 callout_stop(&sc->rssadapt_ch);
405 bus_teardown_intr(sc->sc_dev, sc->sc_irq, sc->sc_ih);
407 lwkt_serialize_exit(ifp->if_serializer);
410 ieee80211_ifdetach(ic);
413 rt2560_free_tx_ring(sc, &sc->txq);
414 rt2560_free_tx_ring(sc, &sc->atimq);
415 rt2560_free_tx_ring(sc, &sc->prioq);
416 rt2560_free_tx_ring(sc, &sc->bcnq);
417 rt2560_free_rx_ring(sc, &sc->rxq);
419 if (sc->sc_irq != NULL) {
420 bus_release_resource(sc->sc_dev, SYS_RES_IRQ, sc->sc_irq_rid,
424 if (sc->sysctl_tree != NULL)
425 sysctl_ctx_free(&sc->sysctl_ctx);
431 rt2560_shutdown(void *xsc)
433 struct rt2560_softc *sc = xsc;
434 struct ifnet *ifp = &sc->sc_ic.ic_if;
436 lwkt_serialize_enter(ifp->if_serializer);
438 lwkt_serialize_exit(ifp->if_serializer);
442 rt2560_suspend(void *xsc)
444 struct rt2560_softc *sc = xsc;
445 struct ifnet *ifp = &sc->sc_ic.ic_if;
447 lwkt_serialize_enter(ifp->if_serializer);
449 lwkt_serialize_exit(ifp->if_serializer);
453 rt2560_resume(void *xsc)
455 struct rt2560_softc *sc = xsc;
456 struct ifnet *ifp = sc->sc_ic.ic_ifp;
458 lwkt_serialize_enter(ifp->if_serializer);
459 if (ifp->if_flags & IFF_UP) {
460 ifp->if_init(ifp->if_softc);
461 if (ifp->if_flags & IFF_RUNNING)
464 lwkt_serialize_exit(ifp->if_serializer);
468 rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
473 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
475 *(bus_addr_t *)arg = segs[0].ds_addr;
479 rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring,
486 ring->cur = ring->next = 0;
487 ring->cur_encrypt = ring->next_encrypt = 0;
489 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
490 BUS_SPACE_MAXADDR, NULL, NULL, count * RT2560_TX_DESC_SIZE, 1,
491 count * RT2560_TX_DESC_SIZE, 0, &ring->desc_dmat);
493 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
497 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
498 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
500 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
504 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
505 count * RT2560_TX_DESC_SIZE,
506 rt2560_dma_map_addr, &ring->physaddr, 0);
508 device_printf(sc->sc_dev, "could not load desc DMA map\n");
510 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
515 ring->data = kmalloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF,
518 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
519 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, RT2560_MAX_SCATTER,
520 MCLBYTES, 0, &ring->data_dmat);
522 device_printf(sc->sc_dev, "could not create data DMA tag\n");
526 for (i = 0; i < count; i++) {
527 error = bus_dmamap_create(ring->data_dmat, 0,
530 device_printf(sc->sc_dev, "could not create DMA map\n");
536 fail: rt2560_free_tx_ring(sc, ring);
541 rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
543 struct rt2560_tx_desc *desc;
544 struct rt2560_tx_data *data;
547 for (i = 0; i < ring->count; i++) {
548 desc = &ring->desc[i];
549 data = &ring->data[i];
551 if (data->m != NULL) {
552 bus_dmamap_sync(ring->data_dmat, data->map,
553 BUS_DMASYNC_POSTWRITE);
554 bus_dmamap_unload(ring->data_dmat, data->map);
559 if (data->ni != NULL) {
560 ieee80211_free_node(data->ni);
567 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
570 ring->cur = ring->next = 0;
571 ring->cur_encrypt = ring->next_encrypt = 0;
575 rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
577 struct rt2560_tx_data *data;
580 if (ring->desc != NULL) {
581 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
582 BUS_DMASYNC_POSTWRITE);
583 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
584 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
588 if (ring->desc_dmat != NULL) {
589 bus_dma_tag_destroy(ring->desc_dmat);
590 ring->desc_dmat = NULL;
593 if (ring->data != NULL) {
594 for (i = 0; i < ring->count; i++) {
595 data = &ring->data[i];
597 if (data->m != NULL) {
598 bus_dmamap_sync(ring->data_dmat, data->map,
599 BUS_DMASYNC_POSTWRITE);
600 bus_dmamap_unload(ring->data_dmat, data->map);
605 if (data->ni != NULL) {
606 ieee80211_free_node(data->ni);
610 if (data->map != NULL) {
611 bus_dmamap_destroy(ring->data_dmat, data->map);
616 kfree(ring->data, M_DEVBUF);
620 if (ring->data_dmat != NULL) {
621 bus_dma_tag_destroy(ring->data_dmat);
622 ring->data_dmat = NULL;
627 rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring,
630 struct rt2560_rx_desc *desc;
631 struct rt2560_rx_data *data;
636 ring->cur = ring->next = 0;
637 ring->cur_decrypt = 0;
639 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
640 BUS_SPACE_MAXADDR, NULL, NULL, count * RT2560_RX_DESC_SIZE, 1,
641 count * RT2560_RX_DESC_SIZE, 0, &ring->desc_dmat);
643 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
647 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
648 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
650 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
654 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
655 count * RT2560_RX_DESC_SIZE,
656 rt2560_dma_map_addr, &ring->physaddr, 0);
658 device_printf(sc->sc_dev, "could not load desc DMA map\n");
660 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
665 ring->data = kmalloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF,
669 * Pre-allocate Rx buffers and populate Rx ring.
671 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
672 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0,
675 device_printf(sc->sc_dev, "could not create data DMA tag\n");
679 for (i = 0; i < count; i++) {
680 desc = &sc->rxq.desc[i];
681 data = &sc->rxq.data[i];
683 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
685 device_printf(sc->sc_dev, "could not create DMA map\n");
689 data->m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
690 if (data->m == NULL) {
691 device_printf(sc->sc_dev,
692 "could not allocate rx mbuf\n");
697 error = bus_dmamap_load(ring->data_dmat, data->map,
698 mtod(data->m, void *), MCLBYTES, rt2560_dma_map_addr,
701 device_printf(sc->sc_dev,
702 "could not load rx buf DMA map");
709 desc->flags = htole32(RT2560_RX_BUSY);
710 desc->physaddr = htole32(physaddr);
713 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
717 fail: rt2560_free_rx_ring(sc, ring);
722 rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
726 for (i = 0; i < ring->count; i++) {
727 ring->desc[i].flags = htole32(RT2560_RX_BUSY);
728 ring->data[i].drop = 0;
731 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
733 ring->cur = ring->next = 0;
734 ring->cur_decrypt = 0;
738 rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
740 struct rt2560_rx_data *data;
742 if (ring->desc != NULL) {
743 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
744 BUS_DMASYNC_POSTWRITE);
745 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
746 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
750 if (ring->desc_dmat != NULL) {
751 bus_dma_tag_destroy(ring->desc_dmat);
752 ring->desc_dmat = NULL;
755 if (ring->data != NULL) {
758 for (i = 0; i < ring->count; i++) {
759 data = &ring->data[i];
761 if (data->m != NULL) {
762 bus_dmamap_sync(ring->data_dmat, data->map,
763 BUS_DMASYNC_POSTREAD);
764 bus_dmamap_unload(ring->data_dmat, data->map);
769 if (data->map != NULL) {
770 bus_dmamap_destroy(ring->data_dmat, data->map);
775 kfree(ring->data, M_DEVBUF);
779 if (ring->data_dmat != NULL) {
780 bus_dma_tag_destroy(ring->data_dmat);
781 ring->data_dmat = NULL;
785 static struct ieee80211_node *
786 rt2560_node_alloc(struct ieee80211_node_table *nt)
788 struct rt2560_node *rn;
790 rn = kmalloc(sizeof(struct rt2560_node), M_80211_NODE,
793 return (rn != NULL) ? &rn->ni : NULL;
797 rt2560_media_change(struct ifnet *ifp)
799 struct rt2560_softc *sc = ifp->if_softc;
802 error = ieee80211_media_change(ifp);
803 if (error != ENETRESET)
806 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
812 * This function is called periodically (every 200ms) during scanning to
813 * switch from one channel to another.
816 rt2560_next_scan(void *arg)
818 struct rt2560_softc *sc = arg;
819 struct ieee80211com *ic = &sc->sc_ic;
820 struct ifnet *ifp = ic->ic_ifp;
822 lwkt_serialize_enter(ifp->if_serializer);
823 if (ic->ic_state == IEEE80211_S_SCAN)
824 ieee80211_next_scan(ic);
825 lwkt_serialize_exit(ifp->if_serializer);
829 * This function is called for each node present in the node station table.
832 rt2560_iter_func(void *arg, struct ieee80211_node *ni)
834 struct rt2560_node *rn = (struct rt2560_node *)ni;
836 ral_rssadapt_updatestats(&rn->rssadapt);
840 * This function is called periodically (every 100ms) in RUN state to update
841 * the rate adaptation statistics.
844 rt2560_update_rssadapt(void *arg)
846 struct rt2560_softc *sc = arg;
847 struct ieee80211com *ic = &sc->sc_ic;
848 struct ifnet *ifp = ic->ic_ifp;
850 lwkt_serialize_enter(ifp->if_serializer);
852 ieee80211_iterate_nodes(&ic->ic_sta, rt2560_iter_func, arg);
853 callout_reset(&sc->rssadapt_ch, hz / 10, rt2560_update_rssadapt, sc);
855 lwkt_serialize_exit(ifp->if_serializer);
859 rt2560_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
861 struct rt2560_softc *sc = ic->ic_ifp->if_softc;
862 enum ieee80211_state ostate;
863 struct ieee80211_node *ni;
867 ostate = ic->ic_state;
868 callout_stop(&sc->scan_ch);
871 case IEEE80211_S_INIT:
872 callout_stop(&sc->rssadapt_ch);
874 if (ostate == IEEE80211_S_RUN) {
875 /* abort TSF synchronization */
876 RAL_WRITE(sc, RT2560_CSR14, 0);
878 /* turn association led off */
879 rt2560_update_led(sc, 0, 0);
883 case IEEE80211_S_SCAN:
884 rt2560_set_chan(sc, ic->ic_curchan);
885 callout_reset(&sc->scan_ch, (sc->dwelltime * hz) / 1000,
886 rt2560_next_scan, sc);
889 case IEEE80211_S_AUTH:
890 rt2560_set_chan(sc, ic->ic_curchan);
893 case IEEE80211_S_ASSOC:
894 rt2560_set_chan(sc, ic->ic_curchan);
897 case IEEE80211_S_RUN:
898 rt2560_set_chan(sc, ic->ic_curchan);
902 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
903 rt2560_update_plcp(sc);
904 rt2560_set_basicrates(sc);
905 rt2560_set_bssid(sc, ni->ni_bssid);
908 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
909 ic->ic_opmode == IEEE80211_M_IBSS) {
910 m = ieee80211_beacon_alloc(ic, ni, &sc->sc_bo);
912 device_printf(sc->sc_dev,
913 "could not allocate beacon\n");
918 ieee80211_ref_node(ni);
919 error = rt2560_tx_bcn(sc, m, ni);
924 /* turn assocation led on */
925 rt2560_update_led(sc, 1, 0);
927 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
928 callout_reset(&sc->rssadapt_ch, hz / 10,
929 rt2560_update_rssadapt, sc);
931 rt2560_enable_tsf_sync(sc);
936 return (error != 0) ? error : sc->sc_newstate(ic, nstate, arg);
940 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
944 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
950 /* clock C once before the first command */
951 RT2560_EEPROM_CTL(sc, 0);
953 RT2560_EEPROM_CTL(sc, RT2560_S);
954 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
955 RT2560_EEPROM_CTL(sc, RT2560_S);
957 /* write start bit (1) */
958 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
959 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
961 /* write READ opcode (10) */
962 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
963 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
964 RT2560_EEPROM_CTL(sc, RT2560_S);
965 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
967 /* write address (A5-A0 or A7-A0) */
968 n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
969 for (; n >= 0; n--) {
970 RT2560_EEPROM_CTL(sc, RT2560_S |
971 (((addr >> n) & 1) << RT2560_SHIFT_D));
972 RT2560_EEPROM_CTL(sc, RT2560_S |
973 (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
976 RT2560_EEPROM_CTL(sc, RT2560_S);
978 /* read data Q15-Q0 */
980 for (n = 15; n >= 0; n--) {
981 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
982 tmp = RAL_READ(sc, RT2560_CSR21);
983 val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
984 RT2560_EEPROM_CTL(sc, RT2560_S);
987 RT2560_EEPROM_CTL(sc, 0);
989 /* clear Chip Select and clock C */
990 RT2560_EEPROM_CTL(sc, RT2560_S);
991 RT2560_EEPROM_CTL(sc, 0);
992 RT2560_EEPROM_CTL(sc, RT2560_C);
998 * Some frames were processed by the hardware cipher engine and are ready for
1002 rt2560_encryption_intr(struct rt2560_softc *sc)
1004 struct rt2560_tx_desc *desc;
1007 /* retrieve last descriptor index processed by cipher engine */
1008 hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr;
1009 hw /= RT2560_TX_DESC_SIZE;
1011 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1012 BUS_DMASYNC_POSTREAD);
1014 for (; sc->txq.next_encrypt != hw;) {
1015 desc = &sc->txq.desc[sc->txq.next_encrypt];
1017 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
1018 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY))
1021 /* for TKIP, swap eiv field to fix a bug in ASIC */
1022 if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) ==
1023 RT2560_TX_CIPHER_TKIP)
1024 desc->eiv = bswap32(desc->eiv);
1026 /* mark the frame ready for transmission */
1027 desc->flags |= htole32(RT2560_TX_BUSY | RT2560_TX_VALID);
1029 DPRINTFN(15, ("encryption done idx=%u\n",
1030 sc->txq.next_encrypt));
1032 sc->txq.next_encrypt =
1033 (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
1036 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1037 BUS_DMASYNC_PREWRITE);
1040 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
1044 rt2560_tx_intr(struct rt2560_softc *sc)
1046 struct ieee80211com *ic = &sc->sc_ic;
1047 struct ifnet *ifp = ic->ic_ifp;
1048 struct rt2560_tx_desc *desc;
1049 struct rt2560_tx_data *data;
1050 struct rt2560_node *rn;
1052 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1053 BUS_DMASYNC_POSTREAD);
1056 desc = &sc->txq.desc[sc->txq.next];
1057 data = &sc->txq.data[sc->txq.next];
1059 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
1060 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY) ||
1061 !(le32toh(desc->flags) & RT2560_TX_VALID))
1064 rn = (struct rt2560_node *)data->ni;
1066 switch (le32toh(desc->flags) & RT2560_TX_RESULT_MASK) {
1067 case RT2560_TX_SUCCESS:
1068 DPRINTFN(10, ("data frame sent successfully\n"));
1069 if (data->id.id_node != NULL) {
1070 ral_rssadapt_raise_rate(ic, &rn->rssadapt,
1076 case RT2560_TX_SUCCESS_RETRY:
1077 DPRINTFN(9, ("data frame sent after %u retries\n",
1078 (le32toh(desc->flags) >> 5) & 0x7));
1082 case RT2560_TX_FAIL_RETRY:
1083 DPRINTFN(9, ("sending data frame failed (too much "
1085 if (data->id.id_node != NULL) {
1086 ral_rssadapt_lower_rate(ic, data->ni,
1087 &rn->rssadapt, &data->id);
1092 case RT2560_TX_FAIL_INVALID:
1093 case RT2560_TX_FAIL_OTHER:
1095 device_printf(sc->sc_dev, "sending data frame failed "
1096 "0x%08x\n", le32toh(desc->flags));
1100 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1101 BUS_DMASYNC_POSTWRITE);
1102 bus_dmamap_unload(sc->txq.data_dmat, data->map);
1105 ieee80211_free_node(data->ni);
1108 /* descriptor is no longer valid */
1109 desc->flags &= ~htole32(RT2560_TX_VALID);
1111 DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next));
1114 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
1117 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1118 BUS_DMASYNC_PREWRITE);
1120 sc->sc_tx_timer = 0;
1121 ifp->if_flags &= ~IFF_OACTIVE;
1126 rt2560_prio_intr(struct rt2560_softc *sc)
1128 struct ieee80211com *ic = &sc->sc_ic;
1129 struct ifnet *ifp = ic->ic_ifp;
1130 struct rt2560_tx_desc *desc;
1131 struct rt2560_tx_data *data;
1133 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1134 BUS_DMASYNC_POSTREAD);
1137 desc = &sc->prioq.desc[sc->prioq.next];
1138 data = &sc->prioq.data[sc->prioq.next];
1140 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
1141 !(le32toh(desc->flags) & RT2560_TX_VALID))
1144 switch (le32toh(desc->flags) & RT2560_TX_RESULT_MASK) {
1145 case RT2560_TX_SUCCESS:
1146 DPRINTFN(10, ("mgt frame sent successfully\n"));
1149 case RT2560_TX_SUCCESS_RETRY:
1150 DPRINTFN(9, ("mgt frame sent after %u retries\n",
1151 (le32toh(desc->flags) >> 5) & 0x7));
1154 case RT2560_TX_FAIL_RETRY:
1155 DPRINTFN(9, ("sending mgt frame failed (too much "
1159 case RT2560_TX_FAIL_INVALID:
1160 case RT2560_TX_FAIL_OTHER:
1162 device_printf(sc->sc_dev, "sending mgt frame failed "
1163 "0x%08x\n", le32toh(desc->flags));
1166 bus_dmamap_sync(sc->prioq.data_dmat, data->map,
1167 BUS_DMASYNC_POSTWRITE);
1168 bus_dmamap_unload(sc->prioq.data_dmat, data->map);
1171 ieee80211_free_node(data->ni);
1174 /* descriptor is no longer valid */
1175 desc->flags &= ~htole32(RT2560_TX_VALID);
1177 DPRINTFN(15, ("prio done idx=%u\n", sc->prioq.next));
1180 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
1183 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1184 BUS_DMASYNC_PREWRITE);
1186 sc->sc_tx_timer = 0;
1187 ifp->if_flags &= ~IFF_OACTIVE;
1192 * Some frames were processed by the hardware cipher engine and are ready for
1193 * transmission to the IEEE802.11 layer.
1196 rt2560_decryption_intr(struct rt2560_softc *sc)
1198 struct ieee80211com *ic = &sc->sc_ic;
1199 struct ifnet *ifp = ic->ic_ifp;
1200 struct rt2560_rx_desc *desc;
1201 struct rt2560_rx_data *data;
1202 bus_addr_t physaddr;
1203 struct ieee80211_frame *wh;
1204 struct ieee80211_node *ni;
1205 struct rt2560_node *rn;
1206 struct mbuf *mnew, *m;
1209 /* retrieve last decriptor index processed by cipher engine */
1210 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr;
1211 hw /= RT2560_RX_DESC_SIZE;
1213 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1214 BUS_DMASYNC_POSTREAD);
1216 for (; sc->rxq.cur_decrypt != hw;) {
1217 desc = &sc->rxq.desc[sc->rxq.cur_decrypt];
1218 data = &sc->rxq.data[sc->rxq.cur_decrypt];
1220 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1221 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1229 if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
1230 (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) {
1236 * Try to allocate a new mbuf for this ring element and load it
1237 * before processing the current mbuf. If the ring element
1238 * cannot be loaded, drop the received packet and reuse the old
1239 * mbuf. In the unlikely case that the old mbuf can't be
1240 * reloaded either, explicitly panic.
1242 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1248 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1249 BUS_DMASYNC_POSTREAD);
1250 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1252 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1253 mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr,
1258 /* try to reload the old mbuf */
1259 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1260 mtod(data->m, void *), MCLBYTES,
1261 rt2560_dma_map_addr, &physaddr, 0);
1263 /* very unlikely that it will fail... */
1264 panic("%s: could not load old rx mbuf",
1265 device_get_name(sc->sc_dev));
1272 * New mbuf successfully loaded, update Rx ring and continue
1277 desc->physaddr = htole32(physaddr);
1280 m->m_pkthdr.rcvif = ifp;
1281 m->m_pkthdr.len = m->m_len =
1282 (le32toh(desc->flags) >> 16) & 0xfff;
1284 if (sc->sc_drvbpf != NULL) {
1285 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
1286 uint32_t tsf_lo, tsf_hi;
1288 /* get timestamp (low and high 32 bits) */
1289 tsf_hi = RAL_READ(sc, RT2560_CSR17);
1290 tsf_lo = RAL_READ(sc, RT2560_CSR16);
1293 htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1295 tap->wr_rate = rt2560_rxrate(desc);
1296 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1297 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1298 tap->wr_antenna = sc->rx_ant;
1299 tap->wr_antsignal = desc->rssi;
1301 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1304 wh = mtod(m, struct ieee80211_frame *);
1305 ni = ieee80211_find_rxnode(ic,
1306 (struct ieee80211_frame_min *)wh);
1308 /* send the frame to the 802.11 layer */
1309 ieee80211_input(ic, m, ni, desc->rssi, 0);
1311 /* give rssi to the rate adatation algorithm */
1312 rn = (struct rt2560_node *)ni;
1313 ral_rssadapt_input(ic, ni, &rn->rssadapt, desc->rssi);
1315 /* node is no longer needed */
1316 ieee80211_free_node(ni);
1318 skip: desc->flags = htole32(RT2560_RX_BUSY);
1320 DPRINTFN(15, ("decryption done idx=%u\n", sc->rxq.cur_decrypt));
1322 sc->rxq.cur_decrypt =
1323 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1326 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1327 BUS_DMASYNC_PREWRITE);
1331 * Some frames were received. Pass them to the hardware cipher engine before
1332 * sending them to the 802.11 layer.
1335 rt2560_rx_intr(struct rt2560_softc *sc)
1337 struct rt2560_rx_desc *desc;
1338 struct rt2560_rx_data *data;
1340 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1341 BUS_DMASYNC_POSTREAD);
1344 desc = &sc->rxq.desc[sc->rxq.cur];
1345 data = &sc->rxq.data[sc->rxq.cur];
1347 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1348 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1353 if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) ||
1354 (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) {
1356 * This should not happen since we did not request
1357 * to receive those frames when we filled RXCSR0.
1359 DPRINTFN(5, ("PHY or CRC error flags 0x%08x\n",
1360 le32toh(desc->flags)));
1364 if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
1365 DPRINTFN(5, ("bad length\n"));
1369 /* mark the frame for decryption */
1370 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
1372 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1374 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1377 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1378 BUS_DMASYNC_PREWRITE);
1381 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1385 * This function is called periodically in IBSS mode when a new beacon must be
1389 rt2560_beacon_expire(struct rt2560_softc *sc)
1391 struct ieee80211com *ic = &sc->sc_ic;
1392 struct rt2560_tx_data *data;
1394 if (ic->ic_opmode != IEEE80211_M_IBSS &&
1395 ic->ic_opmode != IEEE80211_M_HOSTAP)
1398 data = &sc->bcnq.data[sc->bcnq.next];
1400 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1401 bus_dmamap_unload(sc->bcnq.data_dmat, data->map);
1403 ieee80211_beacon_update(ic, data->ni, &sc->sc_bo, data->m, 1);
1405 if (ic->ic_rawbpf != NULL)
1406 bpf_mtap(ic->ic_rawbpf, data->m);
1408 rt2560_tx_bcn(sc, data->m, data->ni);
1410 DPRINTFN(15, ("beacon expired\n"));
1412 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1417 rt2560_wakeup_expire(struct rt2560_softc *sc)
1419 DPRINTFN(2, ("wakeup expired\n"));
1423 rt2560_intr(void *arg)
1425 struct rt2560_softc *sc = arg;
1426 struct ifnet *ifp = &sc->sc_ic.ic_if;
1429 /* disable interrupts */
1430 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1432 /* don't re-enable interrupts if we're shutting down */
1433 if (!(ifp->if_flags & IFF_RUNNING))
1436 r = RAL_READ(sc, RT2560_CSR7);
1437 RAL_WRITE(sc, RT2560_CSR7, r);
1439 if (r & RT2560_BEACON_EXPIRE)
1440 rt2560_beacon_expire(sc);
1442 if (r & RT2560_WAKEUP_EXPIRE)
1443 rt2560_wakeup_expire(sc);
1445 if (r & RT2560_PRIO_DONE)
1446 rt2560_prio_intr(sc);
1448 if (r & (RT2560_TX_DONE | RT2560_ENCRYPTION_DONE)) {
1451 for (i = 0; i < 2; ++i) {
1453 rt2560_encryption_intr(sc);
1457 if (r & (RT2560_DECRYPTION_DONE | RT2560_RX_DONE)) {
1460 for (i = 0; i < 2; ++i) {
1461 rt2560_decryption_intr(sc);
1466 /* re-enable interrupts */
1467 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1470 /* quickly determine if a given rate is CCK or OFDM */
1471 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1473 #define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */
1474 #define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */
1476 #define RAL_SIFS 10 /* us */
1478 #define RT2560_TXRX_TURNAROUND 10 /* us */
1481 * This function is only used by the Rx radiotap code.
1484 rt2560_rxrate(struct rt2560_rx_desc *desc)
1486 if (le32toh(desc->flags) & RT2560_RX_OFDM) {
1487 /* reverse function of rt2560_plcp_signal */
1488 switch (desc->rate) {
1489 case 0xb: return 12;
1490 case 0xf: return 18;
1491 case 0xa: return 24;
1492 case 0xe: return 36;
1493 case 0x9: return 48;
1494 case 0xd: return 72;
1495 case 0x8: return 96;
1496 case 0xc: return 108;
1499 if (desc->rate == 10)
1501 if (desc->rate == 20)
1503 if (desc->rate == 55)
1505 if (desc->rate == 110)
1508 return 2; /* should not get there */
1512 * Return the expected ack rate for a frame transmitted at rate `rate'.
1513 * XXX: this should depend on the destination node basic rate set.
1516 rt2560_ack_rate(struct ieee80211com *ic, int rate)
1525 return (ic->ic_curmode == IEEE80211_MODE_11B) ? 4 : rate;
1541 /* default to 1Mbps */
1546 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1547 * The function automatically determines the operating mode depending on the
1548 * given rate. `flags' indicates whether short preamble is in use or not.
1551 rt2560_txtime(int len, int rate, uint32_t flags)
1555 if (RAL_RATE_IS_OFDM(rate)) {
1556 /* IEEE Std 802.11a-1999, pp. 37 */
1557 txtime = (8 + 4 * len + 3 + rate - 1) / rate;
1558 txtime = 16 + 4 + 4 * txtime + 6;
1560 /* IEEE Std 802.11b-1999, pp. 28 */
1561 txtime = (16 * len + rate - 1) / rate;
1562 if (rate != 2 && (flags & IEEE80211_F_SHPREAMBLE))
1572 rt2560_plcp_signal(int rate)
1575 /* CCK rates (returned values are device-dependent) */
1578 case 11: return 0x2;
1579 case 22: return 0x3;
1581 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1582 case 12: return 0xb;
1583 case 18: return 0xf;
1584 case 24: return 0xa;
1585 case 36: return 0xe;
1586 case 48: return 0x9;
1587 case 72: return 0xd;
1588 case 96: return 0x8;
1589 case 108: return 0xc;
1591 /* unsupported rates (should not get there) */
1592 default: return 0xff;
1597 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1598 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1600 struct ieee80211com *ic = &sc->sc_ic;
1601 uint16_t plcp_length;
1604 desc->flags = htole32(flags);
1605 desc->flags |= htole32(len << 16);
1606 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY) :
1607 htole32(RT2560_TX_BUSY | RT2560_TX_VALID);
1609 desc->physaddr = htole32(physaddr);
1610 desc->wme = htole16(
1612 RT2560_LOGCWMIN(3) |
1613 RT2560_LOGCWMAX(8));
1615 /* setup PLCP fields */
1616 desc->plcp_signal = rt2560_plcp_signal(rate);
1617 desc->plcp_service = 4;
1619 len += IEEE80211_CRC_LEN;
1620 if (RAL_RATE_IS_OFDM(rate)) {
1621 desc->flags |= htole32(RT2560_TX_OFDM);
1623 plcp_length = len & 0xfff;
1624 desc->plcp_length_hi = plcp_length >> 6;
1625 desc->plcp_length_lo = plcp_length & 0x3f;
1627 plcp_length = (16 * len + rate - 1) / rate;
1629 remainder = (16 * len) % 22;
1630 if (remainder != 0 && remainder < 7)
1631 desc->plcp_service |= RT2560_PLCP_LENGEXT;
1633 desc->plcp_length_hi = plcp_length >> 8;
1634 desc->plcp_length_lo = plcp_length & 0xff;
1636 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1637 desc->plcp_signal |= 0x08;
1642 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1643 struct ieee80211_node *ni)
1645 struct ieee80211com *ic = &sc->sc_ic;
1646 struct rt2560_tx_desc *desc;
1647 struct rt2560_tx_data *data;
1651 desc = &sc->bcnq.desc[sc->bcnq.cur];
1652 data = &sc->bcnq.data[sc->bcnq.cur];
1654 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1656 error = bus_dmamap_load_mbuf(sc->bcnq.data_dmat, data->map, m0,
1657 rt2560_dma_map_mbuf, &paddr,
1660 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1666 if (sc->sc_drvbpf != NULL) {
1667 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1670 tap->wt_rate = rate;
1671 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1672 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1673 tap->wt_antenna = sc->tx_ant;
1675 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1681 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1682 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, paddr);
1684 DPRINTFN(10, ("sending beacon frame len=%u idx=%u rate=%u\n",
1685 m0->m_pkthdr.len, sc->bcnq.cur, rate));
1687 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1688 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map,
1689 BUS_DMASYNC_PREWRITE);
1691 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT;
1697 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1698 struct ieee80211_node *ni)
1700 struct ieee80211com *ic = &sc->sc_ic;
1701 struct rt2560_tx_desc *desc;
1702 struct rt2560_tx_data *data;
1703 struct ieee80211_frame *wh;
1709 desc = &sc->prioq.desc[sc->prioq.cur];
1710 data = &sc->prioq.data[sc->prioq.cur];
1712 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1714 error = bus_dmamap_load_mbuf(sc->prioq.data_dmat, data->map, m0,
1715 rt2560_dma_map_mbuf, &paddr, 0);
1717 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1723 if (sc->sc_drvbpf != NULL) {
1724 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1727 tap->wt_rate = rate;
1728 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1729 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1730 tap->wt_antenna = sc->tx_ant;
1732 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1738 wh = mtod(m0, struct ieee80211_frame *);
1740 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1741 flags |= RT2560_TX_ACK;
1743 dur = rt2560_txtime(RAL_ACK_SIZE, rate, ic->ic_flags) +
1745 *(uint16_t *)wh->i_dur = htole16(dur);
1747 /* tell hardware to add timestamp for probe responses */
1748 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1749 IEEE80211_FC0_TYPE_MGT &&
1750 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1751 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1752 flags |= RT2560_TX_TIMESTAMP;
1755 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0, paddr);
1757 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1758 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1759 BUS_DMASYNC_PREWRITE);
1761 DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n",
1762 m0->m_pkthdr.len, sc->prioq.cur, rate));
1766 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1767 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1773 * Build a RTS control frame.
1775 static struct mbuf *
1776 rt2560_get_rts(struct rt2560_softc *sc, struct ieee80211_frame *wh,
1779 struct ieee80211_frame_rts *rts;
1782 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1784 sc->sc_ic.ic_stats.is_tx_nobuf++;
1785 device_printf(sc->sc_dev, "could not allocate RTS frame\n");
1789 rts = mtod(m, struct ieee80211_frame_rts *);
1791 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1792 IEEE80211_FC0_SUBTYPE_RTS;
1793 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1794 *(uint16_t *)rts->i_dur = htole16(dur);
1795 IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1);
1796 IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2);
1798 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
1804 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1805 struct ieee80211_node *ni)
1807 struct ieee80211com *ic = &sc->sc_ic;
1808 struct rt2560_tx_desc *desc;
1809 struct rt2560_tx_data *data;
1810 struct rt2560_node *rn;
1811 struct ieee80211_rateset *rs;
1812 struct ieee80211_frame *wh;
1813 struct ieee80211_key *k;
1820 wh = mtod(m0, struct ieee80211_frame *);
1822 if (ic->ic_fixed_rate != IEEE80211_FIXED_RATE_NONE) {
1823 rs = &ic->ic_sup_rates[ic->ic_curmode];
1824 rate = rs->rs_rates[ic->ic_fixed_rate];
1827 rn = (struct rt2560_node *)ni;
1828 ni->ni_txrate = ral_rssadapt_choose(&rn->rssadapt, rs, wh,
1829 m0->m_pkthdr.len, NULL, 0);
1830 rate = rs->rs_rates[ni->ni_txrate];
1832 rate &= IEEE80211_RATE_VAL;
1834 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1835 k = ieee80211_crypto_encap(ic, ni, m0);
1841 /* packet header may have moved, reset our local pointer */
1842 wh = mtod(m0, struct ieee80211_frame *);
1846 * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange
1847 * for directed frames only when the length of the MPDU is greater
1848 * than the length threshold indicated by [...]" ic_rtsthreshold.
1850 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1851 m0->m_pkthdr.len > ic->ic_rtsthreshold) {
1854 int rtsrate, ackrate;
1856 rtsrate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1857 ackrate = rt2560_ack_rate(ic, rate);
1859 dur = rt2560_txtime(m0->m_pkthdr.len + 4, rate, ic->ic_flags) +
1860 rt2560_txtime(RAL_CTS_SIZE, rtsrate, ic->ic_flags) +
1861 rt2560_txtime(RAL_ACK_SIZE, ackrate, ic->ic_flags) +
1864 m = rt2560_get_rts(sc, wh, dur);
1866 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1867 data = &sc->txq.data[sc->txq.cur_encrypt];
1869 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map,
1870 m, rt2560_dma_map_mbuf, &paddr, 0);
1872 device_printf(sc->sc_dev,
1873 "could not map mbuf (error %d)\n", error);
1879 /* avoid multiple free() of the same node for each fragment */
1880 ieee80211_ref_node(ni);
1885 /* RTS frames are not taken into account for rssadapt */
1886 data->id.id_node = NULL;
1888 rt2560_setup_tx_desc(sc, desc, RT2560_TX_ACK |
1889 RT2560_TX_MORE_FRAG, m->m_pkthdr.len, rtsrate, 1, paddr);
1891 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1892 BUS_DMASYNC_PREWRITE);
1895 sc->txq.cur_encrypt =
1896 (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1899 * IEEE Std 802.11-1999: when an RTS/CTS exchange is used, the
1900 * asynchronous data frame shall be transmitted after the CTS
1901 * frame and a SIFS period.
1903 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1906 data = &sc->txq.data[sc->txq.cur_encrypt];
1907 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1909 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map, m0,
1910 rt2560_dma_map_mbuf, &paddr, 0);
1911 if (error != 0 && error != EFBIG) {
1912 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1918 mnew = m_defrag(m0, MB_DONTWAIT);
1920 device_printf(sc->sc_dev,
1921 "could not defragment mbuf\n");
1927 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map,
1928 m0, rt2560_dma_map_mbuf, &paddr,
1931 device_printf(sc->sc_dev,
1932 "could not map mbuf (error %d)\n", error);
1937 /* packet header may have moved, reset our local pointer */
1938 wh = mtod(m0, struct ieee80211_frame *);
1941 if (sc->sc_drvbpf != NULL) {
1942 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1945 tap->wt_rate = rate;
1946 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1947 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1948 tap->wt_antenna = sc->tx_ant;
1950 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1956 /* remember link conditions for rate adaptation algorithm */
1957 if (ic->ic_fixed_rate == IEEE80211_FIXED_RATE_NONE) {
1958 data->id.id_len = m0->m_pkthdr.len;
1959 data->id.id_rateidx = ni->ni_txrate;
1960 data->id.id_node = ni;
1961 data->id.id_rssi = ni->ni_rssi;
1963 data->id.id_node = NULL;
1965 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1966 flags |= RT2560_TX_ACK;
1968 dur = rt2560_txtime(RAL_ACK_SIZE, rt2560_ack_rate(ic, rate),
1969 ic->ic_flags) + RAL_SIFS;
1970 *(uint16_t *)wh->i_dur = htole16(dur);
1973 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1, paddr);
1975 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1976 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1977 BUS_DMASYNC_PREWRITE);
1979 DPRINTFN(10, ("sending data frame len=%u idx=%u rate=%u\n",
1980 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate));
1984 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1985 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1991 rt2560_start(struct ifnet *ifp)
1993 struct rt2560_softc *sc = ifp->if_softc;
1994 struct ieee80211com *ic = &sc->sc_ic;
1996 struct ether_header *eh;
1997 struct ieee80211_node *ni;
1999 /* prevent management frames from being sent if we're not ready */
2000 if (!(ifp->if_flags & IFF_RUNNING))
2004 IF_POLL(&ic->ic_mgtq, m0);
2006 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
2007 ifp->if_flags |= IFF_OACTIVE;
2010 IF_DEQUEUE(&ic->ic_mgtq, m0);
2012 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
2013 m0->m_pkthdr.rcvif = NULL;
2015 if (ic->ic_rawbpf != NULL)
2016 bpf_mtap(ic->ic_rawbpf, m0);
2018 if (rt2560_tx_mgt(sc, m0, ni) != 0)
2022 if (ic->ic_state != IEEE80211_S_RUN)
2024 m0 = ifq_poll(&ifp->if_snd);
2027 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
2028 ifp->if_flags |= IFF_OACTIVE;
2031 m0 = ifq_dequeue(&ifp->if_snd, m0);
2033 if (m0->m_len < sizeof (struct ether_header) &&
2034 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
2037 eh = mtod(m0, struct ether_header *);
2038 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2045 m0 = ieee80211_encap(ic, m0, ni);
2047 ieee80211_free_node(ni);
2051 if (ic->ic_rawbpf != NULL)
2052 bpf_mtap(ic->ic_rawbpf, m0);
2054 if (rt2560_tx_data(sc, m0, ni) != 0) {
2055 ieee80211_free_node(ni);
2061 sc->sc_tx_timer = 5;
2067 rt2560_watchdog(struct ifnet *ifp)
2069 struct rt2560_softc *sc = ifp->if_softc;
2070 struct ieee80211com *ic = &sc->sc_ic;
2074 if (sc->sc_tx_timer > 0) {
2075 if (--sc->sc_tx_timer == 0) {
2076 device_printf(sc->sc_dev, "device timeout\n");
2084 ieee80211_watchdog(ic);
2088 * This function allows for fast channel switching in monitor mode (used by
2089 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
2090 * generate a new beacon frame.
2093 rt2560_reset(struct ifnet *ifp)
2095 struct rt2560_softc *sc = ifp->if_softc;
2096 struct ieee80211com *ic = &sc->sc_ic;
2098 if (ic->ic_opmode != IEEE80211_M_MONITOR)
2101 rt2560_set_chan(sc, ic->ic_curchan);
2107 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
2109 struct rt2560_softc *sc = ifp->if_softc;
2110 struct ieee80211com *ic = &sc->sc_ic;
2115 if (ifp->if_flags & IFF_UP) {
2116 if (ifp->if_flags & IFF_RUNNING)
2117 rt2560_update_promisc(sc);
2121 if (ifp->if_flags & IFF_RUNNING)
2127 error = ieee80211_ioctl(ic, cmd, data, cr);
2130 if (error == ENETRESET) {
2131 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
2132 (IFF_UP | IFF_RUNNING) &&
2133 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
2142 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2147 for (ntries = 0; ntries < 100; ntries++) {
2148 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2152 if (ntries == 100) {
2153 device_printf(sc->sc_dev, "could not write to BBP\n");
2157 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2158 RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2160 DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg, val));
2164 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2169 val = RT2560_BBP_BUSY | reg << 8;
2170 RAL_WRITE(sc, RT2560_BBPCSR, val);
2172 for (ntries = 0; ntries < 100; ntries++) {
2173 val = RAL_READ(sc, RT2560_BBPCSR);
2174 if (!(val & RT2560_BBP_BUSY))
2179 device_printf(sc->sc_dev, "could not read from BBP\n");
2184 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2189 for (ntries = 0; ntries < 100; ntries++) {
2190 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2194 if (ntries == 100) {
2195 device_printf(sc->sc_dev, "could not write to RF\n");
2199 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2201 RAL_WRITE(sc, RT2560_RFCSR, tmp);
2203 /* remember last written value in sc */
2204 sc->rf_regs[reg] = val;
2206 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
2210 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2212 struct ieee80211com *ic = &sc->sc_ic;
2216 chan = ieee80211_chan2ieee(ic, c);
2217 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2220 if (IEEE80211_IS_CHAN_2GHZ(c))
2221 power = min(sc->txpow[chan - 1], 31);
2225 /* adjust txpower using ifconfig settings */
2226 power -= (100 - ic->ic_txpowlimit) / 8;
2228 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
2230 switch (sc->rf_rev) {
2231 case RT2560_RF_2522:
2232 rt2560_rf_write(sc, RAL_RF1, 0x00814);
2233 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2234 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2237 case RT2560_RF_2523:
2238 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2239 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2240 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2241 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2244 case RT2560_RF_2524:
2245 rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2246 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2247 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2248 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2251 case RT2560_RF_2525:
2252 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2253 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2254 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2255 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2257 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2258 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2259 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2260 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2263 case RT2560_RF_2525E:
2264 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2265 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2266 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2267 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2270 case RT2560_RF_2526:
2271 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2272 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2273 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2275 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2276 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2277 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2281 case RT2560_RF_5222:
2282 for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2284 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2285 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2286 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2287 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2291 if (ic->ic_state != IEEE80211_S_SCAN) {
2292 /* set Japan filter bit for channel 14 */
2293 tmp = rt2560_bbp_read(sc, 70);
2295 tmp &= ~RT2560_JAPAN_FILTER;
2297 tmp |= RT2560_JAPAN_FILTER;
2299 rt2560_bbp_write(sc, 70, tmp);
2301 /* clear CRC errors */
2302 RAL_READ(sc, RT2560_CNT0);
2308 * Disable RF auto-tuning.
2311 rt2560_disable_rf_tune(struct rt2560_softc *sc)
2315 if (sc->rf_rev != RT2560_RF_2523) {
2316 tmp = sc->rf_regs[RAL_RF1] & ~RAL_RF1_AUTOTUNE;
2317 rt2560_rf_write(sc, RAL_RF1, tmp);
2320 tmp = sc->rf_regs[RAL_RF3] & ~RAL_RF3_AUTOTUNE;
2321 rt2560_rf_write(sc, RAL_RF3, tmp);
2323 DPRINTFN(2, ("disabling RF autotune\n"));
2328 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2332 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2334 struct ieee80211com *ic = &sc->sc_ic;
2335 uint16_t logcwmin, preload;
2338 /* first, disable TSF synchronization */
2339 RAL_WRITE(sc, RT2560_CSR14, 0);
2341 tmp = 16 * ic->ic_bss->ni_intval;
2342 RAL_WRITE(sc, RT2560_CSR12, tmp);
2344 RAL_WRITE(sc, RT2560_CSR13, 0);
2347 preload = (ic->ic_opmode == IEEE80211_M_STA) ? 384 : 1024;
2348 tmp = logcwmin << 16 | preload;
2349 RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2351 /* finally, enable TSF synchronization */
2352 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2353 if (ic->ic_opmode == IEEE80211_M_STA)
2354 tmp |= RT2560_ENABLE_TSF_SYNC(1);
2356 tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2357 RT2560_ENABLE_BEACON_GENERATOR;
2358 RAL_WRITE(sc, RT2560_CSR14, tmp);
2360 DPRINTF(("enabling TSF synchronization\n"));
2364 rt2560_update_plcp(struct rt2560_softc *sc)
2366 struct ieee80211com *ic = &sc->sc_ic;
2368 /* no short preamble for 1Mbps */
2369 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2371 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2372 /* values taken from the reference driver */
2373 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401);
2374 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2375 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403);
2377 /* same values as above or'ed 0x8 */
2378 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409);
2379 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2380 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b);
2383 DPRINTF(("updating PLCP for %s preamble\n",
2384 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"));
2388 * This function can be called by ieee80211_set_shortslottime(). Refer to
2389 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
2392 rt2560_update_slot(struct ifnet *ifp)
2394 struct rt2560_softc *sc = ifp->if_softc;
2395 struct ieee80211com *ic = &sc->sc_ic;
2397 uint16_t tx_sifs, tx_pifs, tx_difs, eifs;
2400 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2402 /* update the MAC slot boundaries */
2403 tx_sifs = RAL_SIFS - RT2560_TXRX_TURNAROUND;
2404 tx_pifs = tx_sifs + slottime;
2405 tx_difs = tx_sifs + 2 * slottime;
2406 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2408 tmp = RAL_READ(sc, RT2560_CSR11);
2409 tmp = (tmp & ~0x1f00) | slottime << 8;
2410 RAL_WRITE(sc, RT2560_CSR11, tmp);
2412 tmp = tx_pifs << 16 | tx_sifs;
2413 RAL_WRITE(sc, RT2560_CSR18, tmp);
2415 tmp = eifs << 16 | tx_difs;
2416 RAL_WRITE(sc, RT2560_CSR19, tmp);
2418 DPRINTF(("setting slottime to %uus\n", slottime));
2422 rt2560_set_basicrates(struct rt2560_softc *sc)
2424 struct ieee80211com *ic = &sc->sc_ic;
2426 /* update basic rate set */
2427 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2428 /* 11b basic rates: 1, 2Mbps */
2429 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2430 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
2431 /* 11a basic rates: 6, 12, 24Mbps */
2432 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2434 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2435 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2440 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2444 /* set ON period to 70ms and OFF period to 30ms */
2445 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2446 RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2450 rt2560_set_bssid(struct rt2560_softc *sc, uint8_t *bssid)
2454 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2455 RAL_WRITE(sc, RT2560_CSR5, tmp);
2457 tmp = bssid[4] | bssid[5] << 8;
2458 RAL_WRITE(sc, RT2560_CSR6, tmp);
2460 DPRINTF(("setting BSSID to %6D\n", bssid, ":"));
2464 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2468 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2469 RAL_WRITE(sc, RT2560_CSR3, tmp);
2471 tmp = addr[4] | addr[5] << 8;
2472 RAL_WRITE(sc, RT2560_CSR4, tmp);
2474 DPRINTF(("setting MAC address to %6D\n", addr, ":"));
2478 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2482 tmp = RAL_READ(sc, RT2560_CSR3);
2483 addr[0] = tmp & 0xff;
2484 addr[1] = (tmp >> 8) & 0xff;
2485 addr[2] = (tmp >> 16) & 0xff;
2486 addr[3] = (tmp >> 24);
2488 tmp = RAL_READ(sc, RT2560_CSR4);
2489 addr[4] = tmp & 0xff;
2490 addr[5] = (tmp >> 8) & 0xff;
2494 rt2560_update_promisc(struct rt2560_softc *sc)
2496 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2499 tmp = RAL_READ(sc, RT2560_RXCSR0);
2501 tmp &= ~RT2560_DROP_NOT_TO_ME;
2502 if (!(ifp->if_flags & IFF_PROMISC))
2503 tmp |= RT2560_DROP_NOT_TO_ME;
2505 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2507 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2508 "entering" : "leaving"));
2512 rt2560_get_rf(int rev)
2515 case RT2560_RF_2522: return "RT2522";
2516 case RT2560_RF_2523: return "RT2523";
2517 case RT2560_RF_2524: return "RT2524";
2518 case RT2560_RF_2525: return "RT2525";
2519 case RT2560_RF_2525E: return "RT2525e";
2520 case RT2560_RF_2526: return "RT2526";
2521 case RT2560_RF_5222: return "RT5222";
2522 default: return "unknown";
2527 rt2560_read_eeprom(struct rt2560_softc *sc)
2532 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2533 sc->rf_rev = (val >> 11) & 0x7;
2534 sc->hw_radio = (val >> 10) & 0x1;
2535 sc->led_mode = (val >> 6) & 0x7;
2536 sc->rx_ant = (val >> 4) & 0x3;
2537 sc->tx_ant = (val >> 2) & 0x3;
2538 sc->nb_ant = val & 0x3;
2540 /* read default values for BBP registers */
2541 for (i = 0; i < 16; i++) {
2542 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2543 sc->bbp_prom[i].reg = val >> 8;
2544 sc->bbp_prom[i].val = val & 0xff;
2547 /* read Tx power for all b/g channels */
2548 for (i = 0; i < 14 / 2; i++) {
2549 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2550 sc->txpow[i * 2] = val >> 8;
2551 sc->txpow[i * 2 + 1] = val & 0xff;
2556 rt2560_bbp_init(struct rt2560_softc *sc)
2558 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2561 /* wait for BBP to be ready */
2562 for (ntries = 0; ntries < 100; ntries++) {
2563 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2567 if (ntries == 100) {
2568 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2572 /* initialize BBP registers to default values */
2573 for (i = 0; i < N(rt2560_def_bbp); i++) {
2574 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2575 rt2560_def_bbp[i].val);
2578 /* initialize BBP registers to values stored in EEPROM */
2579 for (i = 0; i < 16; i++) {
2580 if (sc->bbp_prom[i].reg == 0xff)
2582 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2591 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2596 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2598 tx |= RT2560_BBP_ANTA;
2599 else if (antenna == 2)
2600 tx |= RT2560_BBP_ANTB;
2602 tx |= RT2560_BBP_DIVERSITY;
2604 /* need to force I/Q flip for RF 2525e, 2526 and 5222 */
2605 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526 ||
2606 sc->rf_rev == RT2560_RF_5222)
2607 tx |= RT2560_BBP_FLIPIQ;
2609 rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2611 /* update values for CCK and OFDM in BBPCSR1 */
2612 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2613 tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2614 RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2618 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2622 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2624 rx |= RT2560_BBP_ANTA;
2625 else if (antenna == 2)
2626 rx |= RT2560_BBP_ANTB;
2628 rx |= RT2560_BBP_DIVERSITY;
2630 /* need to force no I/Q flip for RF 2525e and 2526 */
2631 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_2526)
2632 rx &= ~RT2560_BBP_FLIPIQ;
2634 rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2638 rt2560_init(void *priv)
2640 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2641 struct rt2560_softc *sc = priv;
2642 struct ieee80211com *ic = &sc->sc_ic;
2643 struct ifnet *ifp = ic->ic_ifp;
2649 /* setup tx rings */
2650 tmp = RT2560_PRIO_RING_COUNT << 24 |
2651 RT2560_ATIM_RING_COUNT << 16 |
2652 RT2560_TX_RING_COUNT << 8 |
2653 RT2560_TX_DESC_SIZE;
2655 /* rings must be initialized in this exact order */
2656 RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2657 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2658 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2659 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2660 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2663 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2665 RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2666 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2668 /* initialize MAC registers to default values */
2669 for (i = 0; i < N(rt2560_def_mac); i++)
2670 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2672 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2673 rt2560_set_macaddr(sc, ic->ic_myaddr);
2675 /* set basic rate set (will be updated later) */
2676 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2678 rt2560_set_txantenna(sc, sc->tx_ant);
2679 rt2560_set_rxantenna(sc, sc->rx_ant);
2680 rt2560_update_slot(ifp);
2681 rt2560_update_plcp(sc);
2682 rt2560_update_led(sc, 0, 0);
2684 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2685 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2687 if (rt2560_bbp_init(sc) != 0) {
2692 /* set default BSS channel */
2693 rt2560_set_chan(sc, ic->ic_curchan);
2696 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2697 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2698 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2699 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2700 tmp |= RT2560_DROP_TODS;
2701 if (!(ifp->if_flags & IFF_PROMISC))
2702 tmp |= RT2560_DROP_NOT_TO_ME;
2704 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2706 /* clear old FCS and Rx FIFO errors */
2707 RAL_READ(sc, RT2560_CNT0);
2708 RAL_READ(sc, RT2560_CNT4);
2710 /* clear any pending interrupts */
2711 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2713 /* enable interrupts */
2714 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2716 ifp->if_flags &= ~IFF_OACTIVE;
2717 ifp->if_flags |= IFF_RUNNING;
2720 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2723 ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
2724 for (i = 0; i < IEEE80211_WEP_NKID; ++i) {
2725 struct ieee80211_key *wk = &ic->ic_nw_keys[i];
2727 if (wk->wk_keylen == 0)
2729 if (wk->wk_flags & IEEE80211_KEY_XMIT)
2730 wk->wk_flags |= IEEE80211_KEY_SWCRYPT;
2734 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2735 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2736 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2738 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2743 rt2560_stop(void *priv)
2745 struct rt2560_softc *sc = priv;
2746 struct ieee80211com *ic = &sc->sc_ic;
2747 struct ifnet *ifp = ic->ic_ifp;
2749 sc->sc_tx_timer = 0;
2751 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2753 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2756 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2759 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2761 /* reset ASIC (imply reset BBP) */
2762 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2763 RAL_WRITE(sc, RT2560_CSR1, 0);
2765 /* disable interrupts */
2766 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2768 /* reset Tx and Rx rings */
2769 rt2560_reset_tx_ring(sc, &sc->txq);
2770 rt2560_reset_tx_ring(sc, &sc->atimq);
2771 rt2560_reset_tx_ring(sc, &sc->prioq);
2772 rt2560_reset_tx_ring(sc, &sc->bcnq);
2773 rt2560_reset_rx_ring(sc, &sc->rxq);
2777 rt2560_dma_map_mbuf(void *arg, bus_dma_segment_t *seg, int nseg,
2778 bus_size_t map_size __unused, int error)
2783 KASSERT(nseg == 1, ("too many dma segments\n"));
2784 *((bus_addr_t *)arg) = seg->ds_addr;