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.36 2008/05/14 11:59:21 sephe Exp $
22 * Ralink Technology RT2560 chipset driver
23 * http://www.ralinktech.com/
26 #include <sys/param.h>
28 #include <sys/endian.h>
29 #include <sys/kernel.h>
30 #include <sys/interrupt.h>
31 #include <sys/malloc.h>
33 #include <sys/module.h>
35 #include <sys/socket.h>
36 #include <sys/sockio.h>
37 #include <sys/sysctl.h>
38 #include <sys/serialize.h>
42 #include <net/if_arp.h>
43 #include <net/ethernet.h>
44 #include <net/if_dl.h>
45 #include <net/if_media.h>
46 #include <net/ifq_var.h>
48 #include <netproto/802_11/ieee80211_var.h>
49 #include <netproto/802_11/ieee80211_radiotap.h>
50 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
51 #include <netproto/802_11/wlan_ratectl/sample/ieee80211_sample_param.h>
53 #include <dev/netif/ral/rt2560reg.h>
54 #include <dev/netif/ral/rt2560var.h>
56 #define RT2560_RSSI(sc, rssi) \
57 ((rssi) > (RT2560_NOISE_FLOOR + (sc)->rssi_corr) ? \
58 ((rssi) - RT2560_NOISE_FLOOR - (sc)->rssi_corr) : 0)
61 #define DPRINTF(sc, x) \
62 do { if ((sc)->sc_debug > 0) kprintf x; } while (0)
63 #define DPRINTFN(sc, n, x) \
64 do { if ((sc)->sc_debug >= (n)) kprintf x; } while (0)
66 #define DPRINTF(sc, x)
67 #define DPRINTFN(sc, n, x)
70 static void rt2560_dma_map_addr(void *, bus_dma_segment_t *, int,
72 static void rt2560_dma_map_mbuf(void *, bus_dma_segment_t *, int,
74 static int rt2560_alloc_tx_ring(struct rt2560_softc *,
75 struct rt2560_tx_ring *, int);
76 static void rt2560_reset_tx_ring(struct rt2560_softc *,
77 struct rt2560_tx_ring *);
78 static void rt2560_free_tx_ring(struct rt2560_softc *,
79 struct rt2560_tx_ring *);
80 static int rt2560_alloc_rx_ring(struct rt2560_softc *,
81 struct rt2560_rx_ring *, int);
82 static void rt2560_reset_rx_ring(struct rt2560_softc *,
83 struct rt2560_rx_ring *);
84 static void rt2560_free_rx_ring(struct rt2560_softc *,
85 struct rt2560_rx_ring *);
86 static int rt2560_media_change(struct ifnet *);
87 static void rt2560_next_scan(void *);
88 static int rt2560_newstate(struct ieee80211com *,
89 enum ieee80211_state, int);
90 static uint16_t rt2560_eeprom_read(struct rt2560_softc *, uint8_t);
91 static void rt2560_encryption_intr(struct rt2560_softc *);
92 static void rt2560_tx_intr(struct rt2560_softc *);
93 static void rt2560_prio_intr(struct rt2560_softc *);
94 static void rt2560_decryption_intr(struct rt2560_softc *);
95 static void rt2560_rx_intr(struct rt2560_softc *);
96 static void rt2560_beacon_expire(struct rt2560_softc *);
97 static void rt2560_wakeup_expire(struct rt2560_softc *);
98 static uint8_t rt2560_rxrate(struct rt2560_rx_desc *);
99 static uint8_t rt2560_plcp_signal(int);
100 static void rt2560_setup_tx_desc(struct rt2560_softc *,
101 struct rt2560_tx_desc *, uint32_t, int, int, int,
103 static int rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *,
104 struct ieee80211_node *);
105 static int rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *,
106 struct ieee80211_node *);
107 static struct mbuf *rt2560_get_rts(struct rt2560_softc *,
108 struct ieee80211_frame *, uint16_t);
109 static int rt2560_tx_data(struct rt2560_softc *, struct mbuf *,
110 struct ieee80211_node *);
111 static void rt2560_start(struct ifnet *);
112 static void rt2560_watchdog(struct ifnet *);
113 static int rt2560_reset(struct ifnet *);
114 static int rt2560_ioctl(struct ifnet *, u_long, caddr_t,
116 static void rt2560_bbp_write(struct rt2560_softc *, uint8_t,
118 static uint8_t rt2560_bbp_read(struct rt2560_softc *, uint8_t);
119 static void rt2560_rf_write(struct rt2560_softc *, uint8_t,
121 static void rt2560_set_chan(struct rt2560_softc *,
122 struct ieee80211_channel *);
123 static void rt2560_enable_tsf_sync(struct rt2560_softc *);
124 static void rt2560_update_plcp(struct rt2560_softc *);
125 static void rt2560_update_slot(struct ifnet *);
126 static void rt2560_set_basicrates(struct rt2560_softc *);
127 static void rt2560_update_led(struct rt2560_softc *, int, int);
128 static void rt2560_set_bssid(struct rt2560_softc *, uint8_t *);
129 static void rt2560_set_macaddr(struct rt2560_softc *, uint8_t *);
130 static void rt2560_get_macaddr(struct rt2560_softc *, uint8_t *);
131 static void rt2560_update_promisc(struct rt2560_softc *);
132 static const char *rt2560_get_rf(int);
133 static void rt2560_read_config(struct rt2560_softc *);
134 static int rt2560_bbp_init(struct rt2560_softc *);
135 static void rt2560_set_txantenna(struct rt2560_softc *, int);
136 static void rt2560_set_rxantenna(struct rt2560_softc *, int);
137 static void rt2560_init(void *);
138 static void rt2560_stop(void *);
139 static void rt2560_intr(void *);
140 static void *rt2560_ratectl_attach(struct ieee80211com *, u_int);
141 static void rt2560_calibrate(void *);
142 static void rt2560_calib_rxsensitivity(struct rt2560_softc *,
144 static int rt2560_sysctl_rxsns(SYSCTL_HANDLER_ARGS);
147 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
149 static const struct ieee80211_rateset rt2560_rateset_11a =
150 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
152 static const struct ieee80211_rateset rt2560_rateset_11b =
153 { 4, { 2, 4, 11, 22 } };
155 static const struct ieee80211_rateset rt2560_rateset_11g =
156 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
158 static const struct {
161 } rt2560_def_mac[] = {
165 static const struct {
168 } rt2560_def_bbp[] = {
172 static const uint32_t rt2560_rf2522_r2[] = RT2560_RF2522_R2;
173 static const uint32_t rt2560_rf2523_r2[] = RT2560_RF2523_R2;
174 static const uint32_t rt2560_rf2524_r2[] = RT2560_RF2524_R2;
175 static const uint32_t rt2560_rf2525_r2[] = RT2560_RF2525_R2;
176 static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2;
177 static const uint32_t rt2560_rf2525e_r2[] = RT2560_RF2525E_R2;
178 static const uint32_t rt2560_rf2526_r2[] = RT2560_RF2526_R2;
179 static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2;
181 static const struct {
184 } rt2560_rf5222[] = {
189 rt2560_attach(device_t dev, int id)
191 struct rt2560_softc *sc = device_get_softc(dev);
192 struct ieee80211com *ic = &sc->sc_ic;
193 struct ifnet *ifp = &ic->ic_if;
196 callout_init(&sc->scan_ch);
197 callout_init(&sc->calib_ch);
203 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irq_rid,
204 RF_ACTIVE | RF_SHAREABLE);
205 if (sc->sc_irq == NULL) {
206 device_printf(dev, "could not allocate interrupt resource\n");
210 /* retrieve RT2560 rev. no */
211 sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
213 /* retrieve MAC address */
214 rt2560_get_macaddr(sc, ic->ic_myaddr);
216 /* retrieve RF rev. no and various other things from EEPROM */
217 rt2560_read_config(sc);
219 device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n",
220 sc->asic_rev, rt2560_get_rf(sc->rf_rev));
223 * Allocate Tx and Rx rings.
225 error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT);
227 device_printf(sc->sc_dev, "could not allocate Tx ring\n");
231 error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT);
233 device_printf(sc->sc_dev, "could not allocate ATIM ring\n");
237 error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT);
239 device_printf(sc->sc_dev, "could not allocate Prio ring\n");
243 error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT);
245 device_printf(sc->sc_dev, "could not allocate Beacon ring\n");
249 error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT);
251 device_printf(sc->sc_dev, "could not allocate Rx ring\n");
255 sysctl_ctx_init(&sc->sysctl_ctx);
256 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
257 SYSCTL_STATIC_CHILDREN(_hw),
259 device_get_nameunit(dev),
261 if (sc->sysctl_tree == NULL) {
262 device_printf(dev, "could not add sysctl node\n");
268 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
269 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
270 ifp->if_init = rt2560_init;
271 ifp->if_ioctl = rt2560_ioctl;
272 ifp->if_start = rt2560_start;
273 ifp->if_watchdog = rt2560_watchdog;
274 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
275 ifq_set_ready(&ifp->if_snd);
277 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
278 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
279 ic->ic_state = IEEE80211_S_INIT;
281 IEEE80211_ONOE_PARAM_SETUP(&sc->sc_onoe_param);
282 IEEE80211_SAMPLE_PARAM_SETUP(&sc->sc_sample_param);
283 ic->ic_ratectl.rc_st_ratectl_cap = IEEE80211_RATECTL_CAP_ONOE |
284 IEEE80211_RATECTL_CAP_SAMPLE;
285 ic->ic_ratectl.rc_st_ratectl = IEEE80211_RATECTL_SAMPLE;
286 ic->ic_ratectl.rc_st_attach = rt2560_ratectl_attach;
288 /* set device capabilities */
290 IEEE80211_C_IBSS | /* IBSS mode supported */
291 IEEE80211_C_MONITOR | /* monitor mode supported */
292 IEEE80211_C_HOSTAP | /* HostAp mode supported */
293 IEEE80211_C_TXPMGT | /* tx power management */
294 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
295 IEEE80211_C_SHSLOT | /* short slot time supported */
296 IEEE80211_C_WPA; /* 802.11i */
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 sc->sc_sifs = IEEE80211_DUR_SIFS; /* Default SIFS */
335 ieee80211_ifattach(ic);
336 ic->ic_updateslot = rt2560_update_slot;
337 ic->ic_reset = rt2560_reset;
338 /* enable s/w bmiss handling in sta mode */
339 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
340 ic->ic_txpowlimit = RT2560_DEFAULT_TXPOWER;
342 /* override state transition machine */
343 sc->sc_newstate = ic->ic_newstate;
344 ic->ic_newstate = rt2560_newstate;
345 ieee80211_media_init(ic, rt2560_media_change, ieee80211_media_status);
347 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
348 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
350 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
351 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
352 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2560_RX_RADIOTAP_PRESENT);
354 sc->sc_txtap_len = sizeof sc->sc_txtapu;
355 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
356 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2560_TX_RADIOTAP_PRESENT);
359 * Add a few sysctl knobs.
361 sc->sc_dwelltime = 200; /* milliseconds */
362 sc->sc_calib_rxsns = 1; /* Enable */
363 sc->sc_rxsns = sc->sc_bbp17_dynmax;
365 SYSCTL_ADD_INT(&sc->sysctl_ctx,
366 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
367 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)");
369 SYSCTL_ADD_INT(&sc->sysctl_ctx,
370 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
371 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)");
373 SYSCTL_ADD_INT(&sc->sysctl_ctx,
374 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "dwell",
375 CTLFLAG_RW, &sc->sc_dwelltime, 0,
376 "channel dwell time (ms) for AP/station scanning");
379 SYSCTL_ADD_INT(&sc->sysctl_ctx,
380 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "debug",
381 CTLFLAG_RW, &sc->sc_debug, 0, "debug level");
384 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
385 SYSCTL_CHILDREN(sc->sysctl_tree),
386 OID_AUTO, "rx_sensitivity", CTLTYPE_INT | CTLFLAG_RW,
387 sc, 0, rt2560_sysctl_rxsns, "I",
388 "initial RX sensitivity");
390 if (sc->sc_flags & RT2560_FLAG_RXSNS) {
391 SYSCTL_ADD_INT(&sc->sysctl_ctx,
392 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "calib_rxsns",
393 CTLFLAG_RW, &sc->sc_calib_rxsns, 0,
394 "calibrate RX sensitivity (sta mode)");
397 error = bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE, rt2560_intr,
398 sc, &sc->sc_ih, ifp->if_serializer);
400 device_printf(dev, "could not set up interrupt\n");
402 ieee80211_ifdetach(ic);
406 ifp->if_cpuid = ithread_cpuid(rman_get_start(sc->sc_irq));
407 KKASSERT(ifp->if_cpuid >= 0 && ifp->if_cpuid < ncpus);
410 ieee80211_announce(ic);
418 rt2560_detach(void *xsc)
420 struct rt2560_softc *sc = xsc;
421 struct ieee80211com *ic = &sc->sc_ic;
422 struct ifnet *ifp = ic->ic_ifp;
424 if (device_is_attached(sc->sc_dev)) {
425 lwkt_serialize_enter(ifp->if_serializer);
427 callout_stop(&sc->scan_ch);
430 bus_teardown_intr(sc->sc_dev, sc->sc_irq, sc->sc_ih);
432 lwkt_serialize_exit(ifp->if_serializer);
435 ieee80211_ifdetach(ic);
438 rt2560_free_tx_ring(sc, &sc->txq);
439 rt2560_free_tx_ring(sc, &sc->atimq);
440 rt2560_free_tx_ring(sc, &sc->prioq);
441 rt2560_free_tx_ring(sc, &sc->bcnq);
442 rt2560_free_rx_ring(sc, &sc->rxq);
444 if (sc->sc_irq != NULL) {
445 bus_release_resource(sc->sc_dev, SYS_RES_IRQ, sc->sc_irq_rid,
449 if (sc->sysctl_tree != NULL)
450 sysctl_ctx_free(&sc->sysctl_ctx);
456 rt2560_shutdown(void *xsc)
458 struct rt2560_softc *sc = xsc;
459 struct ifnet *ifp = &sc->sc_ic.ic_if;
461 lwkt_serialize_enter(ifp->if_serializer);
463 lwkt_serialize_exit(ifp->if_serializer);
467 rt2560_suspend(void *xsc)
469 struct rt2560_softc *sc = xsc;
470 struct ifnet *ifp = &sc->sc_ic.ic_if;
472 lwkt_serialize_enter(ifp->if_serializer);
474 lwkt_serialize_exit(ifp->if_serializer);
478 rt2560_resume(void *xsc)
480 struct rt2560_softc *sc = xsc;
481 struct ifnet *ifp = sc->sc_ic.ic_ifp;
483 lwkt_serialize_enter(ifp->if_serializer);
484 if (ifp->if_flags & IFF_UP) {
485 ifp->if_init(ifp->if_softc);
486 if (ifp->if_flags & IFF_RUNNING)
489 lwkt_serialize_exit(ifp->if_serializer);
493 rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
498 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
500 *(bus_addr_t *)arg = segs[0].ds_addr;
504 rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring,
511 ring->cur = ring->next = 0;
512 ring->cur_encrypt = ring->next_encrypt = 0;
514 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
515 BUS_SPACE_MAXADDR, NULL, NULL, count * RT2560_TX_DESC_SIZE, 1,
516 count * RT2560_TX_DESC_SIZE, 0, &ring->desc_dmat);
518 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
522 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
523 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
525 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
529 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
530 count * RT2560_TX_DESC_SIZE,
531 rt2560_dma_map_addr, &ring->physaddr, 0);
533 device_printf(sc->sc_dev, "could not load desc DMA map\n");
535 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
540 ring->data = kmalloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF,
543 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
544 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, RT2560_MAX_SCATTER,
545 MCLBYTES, 0, &ring->data_dmat);
547 device_printf(sc->sc_dev, "could not create data DMA tag\n");
551 for (i = 0; i < count; i++) {
552 error = bus_dmamap_create(ring->data_dmat, 0,
555 device_printf(sc->sc_dev, "could not create DMA map\n");
561 fail: rt2560_free_tx_ring(sc, ring);
566 rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
568 struct rt2560_tx_desc *desc;
569 struct rt2560_tx_data *data;
572 for (i = 0; i < ring->count; i++) {
573 desc = &ring->desc[i];
574 data = &ring->data[i];
576 if (data->m != NULL) {
577 bus_dmamap_sync(ring->data_dmat, data->map,
578 BUS_DMASYNC_POSTWRITE);
579 bus_dmamap_unload(ring->data_dmat, data->map);
584 if (data->ni != NULL) {
585 ieee80211_free_node(data->ni);
592 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
595 ring->cur = ring->next = 0;
596 ring->cur_encrypt = ring->next_encrypt = 0;
600 rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
602 struct rt2560_tx_data *data;
605 if (ring->desc != NULL) {
606 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
607 BUS_DMASYNC_POSTWRITE);
608 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
609 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
613 if (ring->desc_dmat != NULL) {
614 bus_dma_tag_destroy(ring->desc_dmat);
615 ring->desc_dmat = NULL;
618 if (ring->data != NULL) {
619 for (i = 0; i < ring->count; i++) {
620 data = &ring->data[i];
622 if (data->m != NULL) {
623 bus_dmamap_sync(ring->data_dmat, data->map,
624 BUS_DMASYNC_POSTWRITE);
625 bus_dmamap_unload(ring->data_dmat, data->map);
630 if (data->ni != NULL) {
631 ieee80211_free_node(data->ni);
635 if (data->map != NULL) {
636 bus_dmamap_destroy(ring->data_dmat, data->map);
641 kfree(ring->data, M_DEVBUF);
645 if (ring->data_dmat != NULL) {
646 bus_dma_tag_destroy(ring->data_dmat);
647 ring->data_dmat = NULL;
652 rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring,
655 struct rt2560_rx_desc *desc;
656 struct rt2560_rx_data *data;
661 ring->cur = ring->next = 0;
662 ring->cur_decrypt = 0;
664 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
665 BUS_SPACE_MAXADDR, NULL, NULL, count * RT2560_RX_DESC_SIZE, 1,
666 count * RT2560_RX_DESC_SIZE, 0, &ring->desc_dmat);
668 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
672 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
673 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
675 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
679 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
680 count * RT2560_RX_DESC_SIZE,
681 rt2560_dma_map_addr, &ring->physaddr, 0);
683 device_printf(sc->sc_dev, "could not load desc DMA map\n");
685 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
690 ring->data = kmalloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF,
694 * Pre-allocate Rx buffers and populate Rx ring.
696 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
697 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0,
700 device_printf(sc->sc_dev, "could not create data DMA tag\n");
704 for (i = 0; i < count; i++) {
705 desc = &sc->rxq.desc[i];
706 data = &sc->rxq.data[i];
708 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
710 device_printf(sc->sc_dev, "could not create DMA map\n");
714 data->m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
715 if (data->m == NULL) {
716 device_printf(sc->sc_dev,
717 "could not allocate rx mbuf\n");
722 error = bus_dmamap_load(ring->data_dmat, data->map,
723 mtod(data->m, void *), MCLBYTES, rt2560_dma_map_addr,
726 device_printf(sc->sc_dev,
727 "could not load rx buf DMA map");
734 desc->flags = htole32(RT2560_RX_BUSY);
735 desc->physaddr = htole32(physaddr);
738 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
742 fail: rt2560_free_rx_ring(sc, ring);
747 rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
751 for (i = 0; i < ring->count; i++) {
752 ring->desc[i].flags = htole32(RT2560_RX_BUSY);
753 ring->data[i].drop = 0;
756 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
758 ring->cur = ring->next = 0;
759 ring->cur_decrypt = 0;
763 rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
765 struct rt2560_rx_data *data;
767 if (ring->desc != NULL) {
768 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
769 BUS_DMASYNC_POSTWRITE);
770 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
771 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
775 if (ring->desc_dmat != NULL) {
776 bus_dma_tag_destroy(ring->desc_dmat);
777 ring->desc_dmat = NULL;
780 if (ring->data != NULL) {
783 for (i = 0; i < ring->count; i++) {
784 data = &ring->data[i];
786 if (data->m != NULL) {
787 bus_dmamap_sync(ring->data_dmat, data->map,
788 BUS_DMASYNC_POSTREAD);
789 bus_dmamap_unload(ring->data_dmat, data->map);
794 if (data->map != NULL) {
795 bus_dmamap_destroy(ring->data_dmat, data->map);
800 kfree(ring->data, M_DEVBUF);
804 if (ring->data_dmat != NULL) {
805 bus_dma_tag_destroy(ring->data_dmat);
806 ring->data_dmat = NULL;
811 rt2560_media_change(struct ifnet *ifp)
813 struct rt2560_softc *sc = ifp->if_softc;
816 error = ieee80211_media_change(ifp);
817 if (error != ENETRESET)
820 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
826 * This function is called periodically (every 200ms) during scanning to
827 * switch from one channel to another.
830 rt2560_next_scan(void *arg)
832 struct rt2560_softc *sc = arg;
833 struct ieee80211com *ic = &sc->sc_ic;
834 struct ifnet *ifp = ic->ic_ifp;
836 lwkt_serialize_enter(ifp->if_serializer);
837 if (ic->ic_state == IEEE80211_S_SCAN)
838 ieee80211_next_scan(ic);
839 lwkt_serialize_exit(ifp->if_serializer);
843 rt2560_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
845 struct rt2560_softc *sc = ic->ic_ifp->if_softc;
846 enum ieee80211_state ostate;
847 struct ieee80211_node *ni;
851 ostate = ic->ic_state;
852 callout_stop(&sc->scan_ch);
853 callout_stop(&sc->calib_ch);
854 ieee80211_ratectl_newstate(ic, nstate);
857 case IEEE80211_S_INIT:
858 if (ostate == IEEE80211_S_RUN) {
859 /* abort TSF synchronization */
860 RAL_WRITE(sc, RT2560_CSR14, 0);
862 /* turn association led off */
863 rt2560_update_led(sc, 0, 0);
867 case IEEE80211_S_SCAN:
868 rt2560_set_chan(sc, ic->ic_curchan);
869 callout_reset(&sc->scan_ch, (sc->sc_dwelltime * hz) / 1000,
870 rt2560_next_scan, sc);
873 case IEEE80211_S_AUTH:
874 rt2560_set_chan(sc, ic->ic_curchan);
877 case IEEE80211_S_ASSOC:
878 rt2560_set_chan(sc, ic->ic_curchan);
881 case IEEE80211_S_RUN:
883 rt2560_set_chan(sc, ic->ic_curchan);
887 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
888 rt2560_update_plcp(sc);
889 rt2560_set_basicrates(sc);
890 rt2560_set_bssid(sc, ni->ni_bssid);
893 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
894 ic->ic_opmode == IEEE80211_M_IBSS) {
895 m = ieee80211_beacon_alloc(ic, ni, &sc->sc_bo);
897 device_printf(sc->sc_dev,
898 "could not allocate beacon\n");
903 ieee80211_ref_node(ni);
904 error = rt2560_tx_bcn(sc, m, ni);
909 /* turn assocation led on */
910 rt2560_update_led(sc, 1, 0);
912 if (ic->ic_opmode != IEEE80211_M_MONITOR)
913 rt2560_enable_tsf_sync(sc);
914 if (ic->ic_opmode == IEEE80211_M_STA) {
915 /* Clear false CCA counter */
916 RAL_READ(sc, RT2560_CNT3);
917 callout_reset(&sc->calib_ch, hz, rt2560_calibrate, sc);
922 return (error != 0) ? error : sc->sc_newstate(ic, nstate, arg);
926 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
930 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
936 /* clock C once before the first command */
937 RT2560_EEPROM_CTL(sc, 0);
939 RT2560_EEPROM_CTL(sc, RT2560_S);
940 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
941 RT2560_EEPROM_CTL(sc, RT2560_S);
943 /* write start bit (1) */
944 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
945 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
947 /* write READ opcode (10) */
948 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
949 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
950 RT2560_EEPROM_CTL(sc, RT2560_S);
951 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
953 /* write address (A5-A0 or A7-A0) */
954 n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
955 for (; n >= 0; n--) {
956 RT2560_EEPROM_CTL(sc, RT2560_S |
957 (((addr >> n) & 1) << RT2560_SHIFT_D));
958 RT2560_EEPROM_CTL(sc, RT2560_S |
959 (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
962 RT2560_EEPROM_CTL(sc, RT2560_S);
964 /* read data Q15-Q0 */
966 for (n = 15; n >= 0; n--) {
967 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
968 tmp = RAL_READ(sc, RT2560_CSR21);
969 val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
970 RT2560_EEPROM_CTL(sc, RT2560_S);
973 RT2560_EEPROM_CTL(sc, 0);
975 /* clear Chip Select and clock C */
976 RT2560_EEPROM_CTL(sc, RT2560_S);
977 RT2560_EEPROM_CTL(sc, 0);
978 RT2560_EEPROM_CTL(sc, RT2560_C);
984 * Some frames were processed by the hardware cipher engine and are ready for
988 rt2560_encryption_intr(struct rt2560_softc *sc)
990 struct rt2560_tx_desc *desc;
993 /* retrieve last descriptor index processed by cipher engine */
994 hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr;
995 hw /= RT2560_TX_DESC_SIZE;
997 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
998 BUS_DMASYNC_POSTREAD);
1000 while (sc->txq.next_encrypt != hw) {
1001 if (sc->txq.next_encrypt == sc->txq.cur_encrypt) {
1002 kprintf("hw encrypt %d, cur_encrypt %d\n", hw,
1003 sc->txq.cur_encrypt);
1007 desc = &sc->txq.desc[sc->txq.next_encrypt];
1009 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
1010 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY))
1013 /* for TKIP, swap eiv field to fix a bug in ASIC */
1014 if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) ==
1015 RT2560_TX_CIPHER_TKIP)
1016 desc->eiv = bswap32(desc->eiv);
1018 /* mark the frame ready for transmission */
1019 desc->flags |= htole32(RT2560_TX_VALID);
1020 desc->flags |= htole32(RT2560_TX_BUSY);
1022 DPRINTFN(sc, 15, ("encryption done idx=%u\n",
1023 sc->txq.next_encrypt));
1025 sc->txq.next_encrypt =
1026 (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
1029 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1030 BUS_DMASYNC_PREWRITE);
1033 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
1037 rt2560_tx_intr(struct rt2560_softc *sc)
1039 struct ieee80211com *ic = &sc->sc_ic;
1040 struct ifnet *ifp = ic->ic_ifp;
1042 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1043 BUS_DMASYNC_POSTREAD);
1046 struct rt2560_tx_desc *desc;
1047 struct rt2560_tx_data *data;
1048 struct ieee80211_node *ni;
1049 int rateidx, data_retries, failed;
1053 desc = &sc->txq.desc[sc->txq.next];
1054 data = &sc->txq.data[sc->txq.next];
1056 flags = le32toh(desc->flags);
1058 if ((flags & RT2560_TX_BUSY) ||
1059 (flags & RT2560_TX_CIPHER_BUSY) ||
1060 !(flags & RT2560_TX_VALID))
1063 rateidx = data->rateidx;
1071 switch (flags & RT2560_TX_RESULT_MASK) {
1072 case RT2560_TX_SUCCESS:
1073 DPRINTFN(sc, 10, ("data frame sent successfully\n"));
1078 case RT2560_TX_SUCCESS_RETRY:
1079 data_retries = (flags >> 5) & 0x7;
1080 DPRINTFN(sc, 9, ("data frame sent after %u retries\n",
1085 case RT2560_TX_FAIL_RETRY:
1086 DPRINTFN(sc, 9, ("sending data frame failed (too much "
1093 case RT2560_TX_FAIL_INVALID:
1094 case RT2560_TX_FAIL_OTHER:
1098 device_printf(sc->sc_dev, "sending data frame failed "
1104 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1105 BUS_DMASYNC_POSTWRITE);
1106 bus_dmamap_unload(sc->txq.data_dmat, data->map);
1109 struct ieee80211_ratectl_res res;
1111 res.rc_res_tries = data_retries + 1;
1112 res.rc_res_rateidx = rateidx;
1113 ieee80211_ratectl_tx_complete(ni, m->m_pkthdr.len,
1114 &res, 1, data_retries, 0, failed);
1118 ieee80211_free_node(ni);
1120 /* descriptor is no longer valid */
1121 desc->flags &= ~htole32(RT2560_TX_VALID);
1123 DPRINTFN(sc, 15, ("tx done idx=%u\n", sc->txq.next));
1126 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
1129 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1130 BUS_DMASYNC_PREWRITE);
1132 if (sc->txq.queued == 0 && sc->prioq.queued == 0)
1133 sc->sc_tx_timer = 0;
1135 if (sc->txq.queued < RT2560_TX_RING_COUNT - 1) {
1136 sc->sc_flags &= ~RT2560_FLAG_DATA_OACT;
1138 (RT2560_FLAG_DATA_OACT | RT2560_FLAG_PRIO_OACT)) == 0)
1139 ifp->if_flags &= ~IFF_OACTIVE;
1145 rt2560_prio_intr(struct rt2560_softc *sc)
1147 struct ieee80211com *ic = &sc->sc_ic;
1148 struct ifnet *ifp = ic->ic_ifp;
1149 struct rt2560_tx_desc *desc;
1150 struct rt2560_tx_data *data;
1152 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1153 BUS_DMASYNC_POSTREAD);
1156 desc = &sc->prioq.desc[sc->prioq.next];
1157 data = &sc->prioq.data[sc->prioq.next];
1159 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
1160 !(le32toh(desc->flags) & RT2560_TX_VALID))
1163 switch (le32toh(desc->flags) & RT2560_TX_RESULT_MASK) {
1164 case RT2560_TX_SUCCESS:
1165 DPRINTFN(sc, 10, ("mgt frame sent successfully\n"));
1168 case RT2560_TX_SUCCESS_RETRY:
1169 DPRINTFN(sc, 9, ("mgt frame sent after %u retries\n",
1170 (le32toh(desc->flags) >> 5) & 0x7));
1173 case RT2560_TX_FAIL_RETRY:
1174 DPRINTFN(sc, 9, ("sending mgt frame failed (too much "
1178 case RT2560_TX_FAIL_INVALID:
1179 case RT2560_TX_FAIL_OTHER:
1181 device_printf(sc->sc_dev, "sending mgt frame failed "
1182 "0x%08x\n", le32toh(desc->flags));
1185 bus_dmamap_sync(sc->prioq.data_dmat, data->map,
1186 BUS_DMASYNC_POSTWRITE);
1187 bus_dmamap_unload(sc->prioq.data_dmat, data->map);
1191 KASSERT(data->ni == NULL, ("mgmt node is not empty\n"));
1193 /* descriptor is no longer valid */
1194 desc->flags &= ~htole32(RT2560_TX_VALID);
1196 DPRINTFN(sc, 15, ("prio done idx=%u\n", sc->prioq.next));
1199 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
1202 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1203 BUS_DMASYNC_PREWRITE);
1205 if (sc->txq.queued == 0 && sc->prioq.queued == 0)
1206 sc->sc_tx_timer = 0;
1208 if (sc->prioq.queued < RT2560_PRIO_RING_COUNT) {
1209 sc->sc_flags &= ~RT2560_FLAG_PRIO_OACT;
1211 (RT2560_FLAG_DATA_OACT | RT2560_FLAG_PRIO_OACT)) == 0)
1212 ifp->if_flags &= ~IFF_OACTIVE;
1218 * Some frames were processed by the hardware cipher engine and are ready for
1219 * transmission to the IEEE802.11 layer.
1222 rt2560_decryption_intr(struct rt2560_softc *sc)
1224 struct ieee80211com *ic = &sc->sc_ic;
1225 struct ifnet *ifp = ic->ic_ifp;
1226 struct rt2560_rx_desc *desc;
1227 struct rt2560_rx_data *data;
1228 bus_addr_t physaddr;
1229 struct ieee80211_frame *wh;
1230 struct ieee80211_node *ni;
1231 struct mbuf *mnew, *m;
1234 /* retrieve last decriptor index processed by cipher engine */
1235 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr;
1236 hw /= RT2560_RX_DESC_SIZE;
1238 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1239 BUS_DMASYNC_POSTREAD);
1241 for (; sc->rxq.cur_decrypt != hw;) {
1244 desc = &sc->rxq.desc[sc->rxq.cur_decrypt];
1245 data = &sc->rxq.data[sc->rxq.cur_decrypt];
1247 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1248 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1256 if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
1257 (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) {
1263 * Try to allocate a new mbuf for this ring element and load it
1264 * before processing the current mbuf. If the ring element
1265 * cannot be loaded, drop the received packet and reuse the old
1266 * mbuf. In the unlikely case that the old mbuf can't be
1267 * reloaded either, explicitly panic.
1269 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1275 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1276 BUS_DMASYNC_POSTREAD);
1277 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1279 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1280 mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr,
1285 /* try to reload the old mbuf */
1286 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1287 mtod(data->m, void *), MCLBYTES,
1288 rt2560_dma_map_addr, &physaddr, 0);
1290 /* very unlikely that it will fail... */
1291 panic("%s: could not load old rx mbuf",
1292 device_get_name(sc->sc_dev));
1299 * New mbuf successfully loaded, update Rx ring and continue
1304 desc->physaddr = htole32(physaddr);
1307 m->m_pkthdr.rcvif = ifp;
1308 m->m_pkthdr.len = m->m_len =
1309 (le32toh(desc->flags) >> 16) & 0xfff;
1311 rssi = RT2560_RSSI(sc, desc->rssi);
1312 if (sc->sc_avgrssi < 0)
1313 sc->sc_avgrssi = rssi;
1315 sc->sc_avgrssi = ((sc->sc_avgrssi * 7) + rssi) >> 3;
1317 if (sc->sc_drvbpf != NULL) {
1318 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
1319 uint32_t tsf_lo, tsf_hi;
1321 /* get timestamp (low and high 32 bits) */
1322 tsf_hi = RAL_READ(sc, RT2560_CSR17);
1323 tsf_lo = RAL_READ(sc, RT2560_CSR16);
1326 htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1328 tap->wr_rate = rt2560_rxrate(desc);
1329 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1330 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1331 tap->wr_antenna = sc->rx_ant;
1332 tap->wr_antsignal = rssi;
1334 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1337 wh = mtod(m, struct ieee80211_frame *);
1338 ni = ieee80211_find_rxnode(ic,
1339 (struct ieee80211_frame_min *)wh);
1341 /* send the frame to the 802.11 layer */
1342 ieee80211_input(ic, m, ni, rssi, 0);
1344 /* node is no longer needed */
1345 ieee80211_free_node(ni);
1347 skip: desc->flags = htole32(RT2560_RX_BUSY);
1349 DPRINTFN(sc, 15, ("decryption done idx=%u\n",
1350 sc->rxq.cur_decrypt));
1352 sc->rxq.cur_decrypt =
1353 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1356 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1357 BUS_DMASYNC_PREWRITE);
1361 * Some frames were received. Pass them to the hardware cipher engine before
1362 * sending them to the 802.11 layer.
1365 rt2560_rx_intr(struct rt2560_softc *sc)
1367 struct rt2560_rx_desc *desc;
1368 struct rt2560_rx_data *data;
1370 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1371 BUS_DMASYNC_POSTREAD);
1374 desc = &sc->rxq.desc[sc->rxq.cur];
1375 data = &sc->rxq.data[sc->rxq.cur];
1377 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1378 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1383 if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) ||
1384 (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) {
1386 * This should not happen since we did not request
1387 * to receive those frames when we filled RXCSR0.
1389 DPRINTFN(sc, 5, ("PHY or CRC error flags 0x%08x\n",
1390 le32toh(desc->flags)));
1394 if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
1395 DPRINTFN(sc, 5, ("bad length\n"));
1399 /* mark the frame for decryption */
1400 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
1402 DPRINTFN(sc, 15, ("rx done idx=%u\n", sc->rxq.cur));
1404 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1407 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1408 BUS_DMASYNC_PREWRITE);
1411 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1415 * This function is called periodically in IBSS mode when a new beacon must be
1419 rt2560_beacon_expire(struct rt2560_softc *sc)
1421 struct ieee80211com *ic = &sc->sc_ic;
1422 struct rt2560_tx_data *data;
1424 if (ic->ic_opmode != IEEE80211_M_IBSS &&
1425 ic->ic_opmode != IEEE80211_M_HOSTAP)
1428 data = &sc->bcnq.data[sc->bcnq.next];
1430 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1431 bus_dmamap_unload(sc->bcnq.data_dmat, data->map);
1433 ieee80211_beacon_update(ic, data->ni, &sc->sc_bo, data->m, 1);
1435 if (ic->ic_rawbpf != NULL)
1436 bpf_mtap(ic->ic_rawbpf, data->m);
1438 rt2560_tx_bcn(sc, data->m, data->ni);
1440 DPRINTFN(sc, 15, ("beacon expired\n"));
1442 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1447 rt2560_wakeup_expire(struct rt2560_softc *sc)
1449 DPRINTFN(sc, 2, ("wakeup expired\n"));
1453 rt2560_intr(void *arg)
1455 struct rt2560_softc *sc = arg;
1456 struct ifnet *ifp = &sc->sc_ic.ic_if;
1459 /* disable interrupts */
1460 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1462 /* don't re-enable interrupts if we're shutting down */
1463 if (!(ifp->if_flags & IFF_RUNNING))
1466 r = RAL_READ(sc, RT2560_CSR7);
1467 RAL_WRITE(sc, RT2560_CSR7, r);
1469 if (r & RT2560_BEACON_EXPIRE)
1470 rt2560_beacon_expire(sc);
1472 if (r & RT2560_WAKEUP_EXPIRE)
1473 rt2560_wakeup_expire(sc);
1475 if (r & RT2560_PRIO_DONE)
1476 rt2560_prio_intr(sc);
1478 if (r & (RT2560_RX_DONE | RT2560_TX_DONE | RT2560_ENCRYPTION_DONE)) {
1481 for (i = 0; i < 2; ++i) {
1483 rt2560_encryption_intr(sc);
1487 if (r & (RT2560_DECRYPTION_DONE | RT2560_RX_DONE)) {
1490 for (i = 0; i < 2; ++i) {
1491 rt2560_decryption_intr(sc);
1496 /* re-enable interrupts */
1497 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1500 /* quickly determine if a given rate is CCK or OFDM */
1501 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1503 #define RAL_ACK_SIZE (sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
1504 #define RAL_CTS_SIZE (sizeof(struct ieee80211_frame_cts) + IEEE80211_CRC_LEN)
1506 #define RT2560_TXRX_TURNAROUND 10 /* us */
1509 * This function is only used by the Rx radiotap code.
1512 rt2560_rxrate(struct rt2560_rx_desc *desc)
1514 if (le32toh(desc->flags) & RT2560_RX_OFDM) {
1515 /* reverse function of rt2560_plcp_signal */
1516 switch (desc->rate) {
1517 case 0xb: return 12;
1518 case 0xf: return 18;
1519 case 0xa: return 24;
1520 case 0xe: return 36;
1521 case 0x9: return 48;
1522 case 0xd: return 72;
1523 case 0x8: return 96;
1524 case 0xc: return 108;
1527 if (desc->rate == 10)
1529 if (desc->rate == 20)
1531 if (desc->rate == 55)
1533 if (desc->rate == 110)
1536 return 2; /* should not get there */
1540 rt2560_plcp_signal(int rate)
1543 /* CCK rates (returned values are device-dependent) */
1546 case 11: return 0x2;
1547 case 22: return 0x3;
1549 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1550 case 12: return 0xb;
1551 case 18: return 0xf;
1552 case 24: return 0xa;
1553 case 36: return 0xe;
1554 case 48: return 0x9;
1555 case 72: return 0xd;
1556 case 96: return 0x8;
1557 case 108: return 0xc;
1559 /* unsupported rates (should not get there) */
1560 default: return 0xff;
1565 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1566 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1568 struct ieee80211com *ic = &sc->sc_ic;
1569 uint16_t plcp_length;
1572 desc->flags = htole32(flags);
1573 desc->flags |= htole32(len << 16);
1575 desc->physaddr = htole32(physaddr);
1576 desc->wme = htole16(
1578 RT2560_LOGCWMIN(3) |
1579 RT2560_LOGCWMAX(8));
1581 /* setup PLCP fields */
1582 desc->plcp_signal = rt2560_plcp_signal(rate);
1583 desc->plcp_service = 4;
1585 len += IEEE80211_CRC_LEN;
1586 if (RAL_RATE_IS_OFDM(rate)) {
1587 desc->flags |= htole32(RT2560_TX_OFDM);
1589 plcp_length = len & 0xfff;
1590 desc->plcp_length_hi = plcp_length >> 6;
1591 desc->plcp_length_lo = plcp_length & 0x3f;
1593 plcp_length = (16 * len + rate - 1) / rate;
1595 remainder = (16 * len) % 22;
1596 if (remainder != 0 && remainder < 7)
1597 desc->plcp_service |= RT2560_PLCP_LENGEXT;
1599 desc->plcp_length_hi = plcp_length >> 8;
1600 desc->plcp_length_lo = plcp_length & 0xff;
1602 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1603 desc->plcp_signal |= 0x08;
1607 desc->flags |= htole32(RT2560_TX_VALID);
1608 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY)
1609 : htole32(RT2560_TX_BUSY);
1613 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1614 struct ieee80211_node *ni)
1616 struct ieee80211com *ic = &sc->sc_ic;
1617 struct rt2560_tx_desc *desc;
1618 struct rt2560_tx_data *data;
1622 desc = &sc->bcnq.desc[sc->bcnq.cur];
1623 data = &sc->bcnq.data[sc->bcnq.cur];
1625 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1627 error = bus_dmamap_load_mbuf(sc->bcnq.data_dmat, data->map, m0,
1628 rt2560_dma_map_mbuf, &paddr,
1631 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1637 if (sc->sc_drvbpf != NULL) {
1638 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1641 tap->wt_rate = rate;
1642 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1643 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1644 tap->wt_antenna = sc->tx_ant;
1646 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1652 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1653 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, paddr);
1655 DPRINTFN(sc, 10, ("sending beacon frame len=%u idx=%u rate=%u\n",
1656 m0->m_pkthdr.len, sc->bcnq.cur, rate));
1658 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1659 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map,
1660 BUS_DMASYNC_PREWRITE);
1662 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT;
1668 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1669 struct ieee80211_node *ni)
1671 struct ieee80211com *ic = &sc->sc_ic;
1672 struct rt2560_tx_desc *desc;
1673 struct rt2560_tx_data *data;
1674 struct ieee80211_frame *wh;
1680 desc = &sc->prioq.desc[sc->prioq.cur];
1681 data = &sc->prioq.data[sc->prioq.cur];
1683 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1685 error = bus_dmamap_load_mbuf(sc->prioq.data_dmat, data->map, m0,
1686 rt2560_dma_map_mbuf, &paddr, 0);
1688 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1690 ieee80211_free_node(ni);
1695 if (sc->sc_drvbpf != NULL) {
1696 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1699 tap->wt_rate = rate;
1700 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1701 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1702 tap->wt_antenna = sc->tx_ant;
1704 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1710 wh = mtod(m0, struct ieee80211_frame *);
1712 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1713 flags |= RT2560_TX_ACK;
1715 dur = ieee80211_txtime(ni, RAL_ACK_SIZE, rate, ic->ic_flags) +
1717 *(uint16_t *)wh->i_dur = htole16(dur);
1719 /* tell hardware to add timestamp for probe responses */
1720 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1721 IEEE80211_FC0_TYPE_MGT &&
1722 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1723 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1724 flags |= RT2560_TX_TIMESTAMP;
1727 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0, paddr);
1729 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1730 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1731 BUS_DMASYNC_PREWRITE);
1733 DPRINTFN(sc, 10, ("sending mgt frame len=%u idx=%u rate=%u\n",
1734 m0->m_pkthdr.len, sc->prioq.cur, rate));
1738 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1739 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1741 ieee80211_free_node(ni);
1747 * Build a RTS control frame.
1749 static struct mbuf *
1750 rt2560_get_rts(struct rt2560_softc *sc, struct ieee80211_frame *wh,
1753 struct ieee80211_frame_rts *rts;
1756 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1758 sc->sc_ic.ic_stats.is_tx_nobuf++;
1759 device_printf(sc->sc_dev, "could not allocate RTS frame\n");
1763 rts = mtod(m, struct ieee80211_frame_rts *);
1765 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1766 IEEE80211_FC0_SUBTYPE_RTS;
1767 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1768 *(uint16_t *)rts->i_dur = htole16(dur);
1769 IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1);
1770 IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2);
1772 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
1778 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1779 struct ieee80211_node *ni)
1781 struct ieee80211com *ic = &sc->sc_ic;
1782 struct rt2560_tx_desc *desc;
1783 struct rt2560_tx_data *data;
1784 struct ieee80211_frame *wh;
1785 struct ieee80211_key *k;
1790 int rate, error, ackrate, rateidx;
1792 wh = mtod(m0, struct ieee80211_frame *);
1793 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1794 k = ieee80211_crypto_encap(ic, ni, m0);
1800 /* packet header may have moved, reset our local pointer */
1801 wh = mtod(m0, struct ieee80211_frame *);
1804 ieee80211_ratectl_findrate(ni, m0->m_pkthdr.len, &rateidx, 1);
1805 rate = IEEE80211_RS_RATE(&ni->ni_rates, rateidx);
1807 ackrate = ieee80211_ack_rate(ni, rate);
1810 * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange
1811 * for directed frames only when the length of the MPDU is greater
1812 * than the length threshold indicated by [...]" ic_rtsthreshold.
1814 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1815 m0->m_pkthdr.len > ic->ic_rtsthreshold) {
1820 rtsrate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1821 dur = ieee80211_txtime(ni, m0->m_pkthdr.len + IEEE80211_CRC_LEN,
1822 rate, ic->ic_flags) +
1823 ieee80211_txtime(ni, RAL_CTS_SIZE, rtsrate, ic->ic_flags)+
1824 ieee80211_txtime(ni, RAL_ACK_SIZE, ackrate, ic->ic_flags)+
1827 m = rt2560_get_rts(sc, wh, dur);
1829 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1830 data = &sc->txq.data[sc->txq.cur_encrypt];
1832 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map,
1833 m, rt2560_dma_map_mbuf, &paddr, 0);
1835 device_printf(sc->sc_dev,
1836 "could not map mbuf (error %d)\n", error);
1842 /* avoid multiple free() of the same node for each fragment */
1843 ieee80211_ref_node(ni);
1847 data->rateidx = -1; /* don't count RTS */
1849 rt2560_setup_tx_desc(sc, desc, RT2560_TX_ACK |
1850 RT2560_TX_MORE_FRAG, m->m_pkthdr.len, rtsrate, 1, paddr);
1852 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1853 BUS_DMASYNC_PREWRITE);
1856 sc->txq.cur_encrypt =
1857 (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1860 * IEEE Std 802.11-1999: when an RTS/CTS exchange is used, the
1861 * asynchronous data frame shall be transmitted after the CTS
1862 * frame and a SIFS period.
1864 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1867 data = &sc->txq.data[sc->txq.cur_encrypt];
1868 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1870 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map, m0,
1871 rt2560_dma_map_mbuf, &paddr, 0);
1872 if (error != 0 && error != EFBIG) {
1873 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1879 mnew = m_defrag(m0, MB_DONTWAIT);
1881 device_printf(sc->sc_dev,
1882 "could not defragment mbuf\n");
1888 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map,
1889 m0, rt2560_dma_map_mbuf, &paddr,
1892 device_printf(sc->sc_dev,
1893 "could not map mbuf (error %d)\n", error);
1898 /* packet header may have moved, reset our local pointer */
1899 wh = mtod(m0, struct ieee80211_frame *);
1902 if (sc->sc_drvbpf != NULL) {
1903 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1906 tap->wt_rate = rate;
1907 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1908 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1909 tap->wt_antenna = sc->tx_ant;
1911 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1916 data->rateidx = rateidx;
1918 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1919 flags |= RT2560_TX_ACK;
1920 dur = ieee80211_txtime(ni, RAL_ACK_SIZE, ackrate, ic->ic_flags)+
1922 *(uint16_t *)wh->i_dur = htole16(dur);
1925 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1, paddr);
1927 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1928 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1929 BUS_DMASYNC_PREWRITE);
1931 DPRINTFN(sc, 10, ("sending data frame len=%u idx=%u rate=%u\n",
1932 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate));
1936 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1937 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1943 rt2560_start(struct ifnet *ifp)
1945 struct rt2560_softc *sc = ifp->if_softc;
1946 struct ieee80211com *ic = &sc->sc_ic;
1948 struct ether_header *eh;
1949 struct ieee80211_node *ni;
1951 /* prevent management frames from being sent if we're not ready */
1952 if (!(ifp->if_flags & IFF_RUNNING))
1956 IF_POLL(&ic->ic_mgtq, m0);
1958 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
1959 ifp->if_flags |= IFF_OACTIVE;
1960 sc->sc_flags |= RT2560_FLAG_PRIO_OACT;
1963 IF_DEQUEUE(&ic->ic_mgtq, m0);
1965 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1966 m0->m_pkthdr.rcvif = NULL;
1968 if (ic->ic_rawbpf != NULL)
1969 bpf_mtap(ic->ic_rawbpf, m0);
1971 if (rt2560_tx_mgt(sc, m0, ni) != 0)
1974 if (ic->ic_state != IEEE80211_S_RUN) {
1975 ifq_purge(&ifp->if_snd);
1979 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
1980 ifp->if_flags |= IFF_OACTIVE;
1981 sc->sc_flags |= RT2560_FLAG_DATA_OACT;
1984 m0 = ifq_dequeue(&ifp->if_snd, NULL);
1988 if (m0->m_len < sizeof (struct ether_header) &&
1989 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1992 eh = mtod(m0, struct ether_header *);
1993 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
2000 m0 = ieee80211_encap(ic, m0, ni);
2002 ieee80211_free_node(ni);
2006 if (ic->ic_rawbpf != NULL)
2007 bpf_mtap(ic->ic_rawbpf, m0);
2009 if (rt2560_tx_data(sc, m0, ni) != 0) {
2010 ieee80211_free_node(ni);
2016 sc->sc_tx_timer = 5;
2022 rt2560_watchdog(struct ifnet *ifp)
2024 struct rt2560_softc *sc = ifp->if_softc;
2025 struct ieee80211com *ic = &sc->sc_ic;
2029 if (sc->sc_tx_timer > 0) {
2030 if (--sc->sc_tx_timer == 0) {
2031 device_printf(sc->sc_dev, "device timeout\n");
2039 ieee80211_watchdog(ic);
2043 * This function allows for fast channel switching in monitor mode (used by
2044 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
2045 * generate a new beacon frame.
2048 rt2560_reset(struct ifnet *ifp)
2050 struct rt2560_softc *sc = ifp->if_softc;
2051 struct ieee80211com *ic = &sc->sc_ic;
2053 if (ic->ic_opmode != IEEE80211_M_MONITOR)
2056 rt2560_set_chan(sc, ic->ic_curchan);
2062 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
2064 struct rt2560_softc *sc = ifp->if_softc;
2065 struct ieee80211com *ic = &sc->sc_ic;
2070 if (ifp->if_flags & IFF_UP) {
2071 if (ifp->if_flags & IFF_RUNNING)
2072 rt2560_update_promisc(sc);
2076 if (ifp->if_flags & IFF_RUNNING)
2082 error = ieee80211_ioctl(ic, cmd, data, cr);
2085 if (error == ENETRESET) {
2086 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
2087 (IFF_UP | IFF_RUNNING) &&
2088 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
2097 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2102 for (ntries = 0; ntries < 100; ntries++) {
2103 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2107 if (ntries == 100) {
2108 device_printf(sc->sc_dev, "could not write to BBP\n");
2112 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2113 RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2115 DPRINTFN(sc, 15, ("BBP R%u <- 0x%02x\n", reg, val));
2119 DPRINTF(sc, ("%s record bbp17 %#x\n", __func__, val));
2125 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2130 for (ntries = 0; ntries < 100; ntries++) {
2131 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2135 if (ntries == 100) {
2136 device_printf(sc->sc_dev, "could not read from BBP\n");
2140 val = RT2560_BBP_BUSY | reg << 8;
2141 RAL_WRITE(sc, RT2560_BBPCSR, val);
2143 for (ntries = 0; ntries < 100; ntries++) {
2144 val = RAL_READ(sc, RT2560_BBPCSR);
2145 if (!(val & RT2560_BBP_BUSY))
2150 device_printf(sc->sc_dev, "could not read from BBP\n");
2155 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2160 for (ntries = 0; ntries < 100; ntries++) {
2161 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2165 if (ntries == 100) {
2166 device_printf(sc->sc_dev, "could not write to RF\n");
2170 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2172 RAL_WRITE(sc, RT2560_RFCSR, tmp);
2174 /* remember last written value in sc */
2175 sc->rf_regs[reg] = val;
2177 DPRINTFN(sc, 15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
2181 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2183 struct ieee80211com *ic = &sc->sc_ic;
2187 chan = ieee80211_chan2ieee(ic, c);
2188 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2191 if (IEEE80211_IS_CHAN_2GHZ(c))
2192 sc->sc_curtxpow = sc->txpow[chan - 1];
2194 sc->sc_curtxpow = 31;
2196 if (ic->ic_txpowlimit > sc->sc_curtxpow)
2197 ic->ic_txpowlimit = sc->sc_curtxpow;
2199 sc->sc_curtxpow = ic->ic_txpowlimit;
2200 ic->ic_bss->ni_txpower = sc->sc_curtxpow;
2202 power = sc->sc_curtxpow;
2204 DPRINTFN(sc, 2, ("setting channel to %u, txpower to %u\n",
2207 switch (sc->rf_rev) {
2208 case RT2560_RF_2522:
2209 rt2560_rf_write(sc, RAL_RF1, 0x00814);
2210 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2211 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2214 case RT2560_RF_2523:
2215 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2216 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2217 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2218 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2221 case RT2560_RF_2524:
2222 rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2223 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2224 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2225 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2228 case RT2560_RF_2525:
2229 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2230 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2231 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2232 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2234 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2235 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2236 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2237 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2240 case RT2560_RF_2525E:
2241 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2242 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2243 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2244 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2247 case RT2560_RF_2526:
2248 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2249 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2250 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2252 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2253 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2254 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2258 case RT2560_RF_5222:
2259 for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2261 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2262 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2263 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2264 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2268 if (ic->ic_state != IEEE80211_S_SCAN) {
2269 /* set Japan filter bit for channel 14 */
2270 tmp = rt2560_bbp_read(sc, 70);
2272 tmp &= ~RT2560_JAPAN_FILTER;
2274 tmp |= RT2560_JAPAN_FILTER;
2276 rt2560_bbp_write(sc, 70, tmp);
2278 /* clear CRC errors */
2279 RAL_READ(sc, RT2560_CNT0);
2282 sc->sc_sifs = IEEE80211_IS_CHAN_5GHZ(c) ? IEEE80211_DUR_OFDM_SIFS
2283 : IEEE80211_DUR_SIFS;
2287 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2291 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2293 struct ieee80211com *ic = &sc->sc_ic;
2294 uint16_t logcwmin, preload;
2297 /* first, disable TSF synchronization */
2298 RAL_WRITE(sc, RT2560_CSR14, 0);
2300 tmp = 16 * ic->ic_bss->ni_intval;
2301 RAL_WRITE(sc, RT2560_CSR12, tmp);
2303 RAL_WRITE(sc, RT2560_CSR13, 0);
2306 preload = (ic->ic_opmode == IEEE80211_M_STA) ? 384 : 1024;
2307 tmp = logcwmin << 16 | preload;
2308 RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2310 /* finally, enable TSF synchronization */
2311 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2312 if (ic->ic_opmode == IEEE80211_M_STA)
2313 tmp |= RT2560_ENABLE_TSF_SYNC(1);
2315 tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2316 RT2560_ENABLE_BEACON_GENERATOR;
2317 RAL_WRITE(sc, RT2560_CSR14, tmp);
2319 DPRINTF(sc, ("enabling TSF synchronization\n"));
2323 rt2560_update_plcp(struct rt2560_softc *sc)
2325 struct ieee80211com *ic = &sc->sc_ic;
2327 /* no short preamble for 1Mbps */
2328 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2330 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2331 /* values taken from the reference driver */
2332 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401);
2333 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2334 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403);
2336 /* same values as above or'ed 0x8 */
2337 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409);
2338 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2339 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b);
2342 DPRINTF(sc, ("updating PLCP for %s preamble\n",
2343 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"));
2347 * This function can be called by ieee80211_set_shortslottime(). Refer to
2348 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
2351 rt2560_update_slot(struct ifnet *ifp)
2353 struct rt2560_softc *sc = ifp->if_softc;
2354 struct ieee80211com *ic = &sc->sc_ic;
2356 uint16_t tx_sifs, tx_pifs, tx_difs, eifs;
2360 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2363 * Setting slot time according to "short slot time" capability
2364 * in beacon/probe_resp seems to cause problem to acknowledge
2365 * certain AP's data frames transimitted at CCK/DS rates: the
2366 * problematic AP keeps retransmitting data frames, probably
2367 * because MAC level acks are not received by hardware.
2368 * So we cheat a little bit here by claiming we are capable of
2369 * "short slot time" but setting hardware slot time to the normal
2370 * slot time. ral(4) does not seem to have trouble to receive
2371 * frames transmitted using short slot time even if hardware
2372 * slot time is set to normal slot time. If we didn't use this
2373 * trick, we would have to claim that short slot time is not
2374 * supported; this would give relative poor TX performance
2375 * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short
2378 slottime = (ic->ic_curmode == IEEE80211_MODE_11A) ? 9 : 20;
2381 /* update the MAC slot boundaries */
2382 tx_sifs = sc->sc_sifs - RT2560_TXRX_TURNAROUND;
2383 tx_pifs = tx_sifs + slottime;
2384 tx_difs = tx_sifs + 2 * slottime;
2385 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2387 tmp = RAL_READ(sc, RT2560_CSR11);
2388 tmp = (tmp & ~0x1f00) | slottime << 8;
2389 RAL_WRITE(sc, RT2560_CSR11, tmp);
2391 tmp = tx_pifs << 16 | tx_sifs;
2392 RAL_WRITE(sc, RT2560_CSR18, tmp);
2394 tmp = eifs << 16 | tx_difs;
2395 RAL_WRITE(sc, RT2560_CSR19, tmp);
2397 DPRINTF(sc, ("setting slottime to %uus\n", slottime));
2401 rt2560_set_basicrates(struct rt2560_softc *sc)
2403 struct ieee80211com *ic = &sc->sc_ic;
2405 /* update basic rate set */
2406 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2407 /* 11b basic rates: 1, 2Mbps */
2408 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2409 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
2410 /* 11a basic rates: 6, 12, 24Mbps */
2411 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2413 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2414 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2419 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2423 /* set ON period to 70ms and OFF period to 30ms */
2424 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2425 RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2429 rt2560_set_bssid(struct rt2560_softc *sc, uint8_t *bssid)
2433 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2434 RAL_WRITE(sc, RT2560_CSR5, tmp);
2436 tmp = bssid[4] | bssid[5] << 8;
2437 RAL_WRITE(sc, RT2560_CSR6, tmp);
2439 DPRINTF(sc, ("setting BSSID to %6D\n", bssid, ":"));
2443 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2447 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2448 RAL_WRITE(sc, RT2560_CSR3, tmp);
2450 tmp = addr[4] | addr[5] << 8;
2451 RAL_WRITE(sc, RT2560_CSR4, tmp);
2453 DPRINTF(sc, ("setting MAC address to %6D\n", addr, ":"));
2457 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2461 tmp = RAL_READ(sc, RT2560_CSR3);
2462 addr[0] = tmp & 0xff;
2463 addr[1] = (tmp >> 8) & 0xff;
2464 addr[2] = (tmp >> 16) & 0xff;
2465 addr[3] = (tmp >> 24);
2467 tmp = RAL_READ(sc, RT2560_CSR4);
2468 addr[4] = tmp & 0xff;
2469 addr[5] = (tmp >> 8) & 0xff;
2473 rt2560_update_promisc(struct rt2560_softc *sc)
2475 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2478 tmp = RAL_READ(sc, RT2560_RXCSR0);
2480 tmp &= ~RT2560_DROP_NOT_TO_ME;
2481 if (!(ifp->if_flags & IFF_PROMISC))
2482 tmp |= RT2560_DROP_NOT_TO_ME;
2484 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2486 DPRINTF(sc, ("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2487 "entering" : "leaving"));
2491 rt2560_get_rf(int rev)
2494 case RT2560_RF_2522: return "RT2522";
2495 case RT2560_RF_2523: return "RT2523";
2496 case RT2560_RF_2524: return "RT2524";
2497 case RT2560_RF_2525: return "RT2525";
2498 case RT2560_RF_2525E: return "RT2525e";
2499 case RT2560_RF_2526: return "RT2526";
2500 case RT2560_RF_5222: return "RT5222";
2501 default: return "unknown";
2506 rt2560_read_config(struct rt2560_softc *sc)
2509 int i, find_bbp17 = 0;
2511 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2512 sc->rf_rev = (val >> 11) & 0x7;
2513 sc->hw_radio = (val >> 10) & 0x1;
2514 sc->led_mode = (val >> 6) & 0x7;
2515 sc->rx_ant = (val >> 4) & 0x3;
2516 sc->tx_ant = (val >> 2) & 0x3;
2517 sc->nb_ant = val & 0x3;
2519 /* read default values for BBP registers */
2520 for (i = 0; i < 16; i++) {
2521 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2522 if (val == 0xffff || val == 0)
2524 sc->bbp_prom[i].reg = val >> 8;
2525 sc->bbp_prom[i].val = val & 0xff;
2526 DPRINTF(sc, ("rom bbp reg:%u val:%#x\n",
2527 sc->bbp_prom[i].reg, sc->bbp_prom[i].val));
2529 if (sc->bbp_prom[i].reg == 17) {
2530 if (sc->bbp_prom[i].val > 6)
2531 sc->sc_bbp17_dynmin = sc->bbp_prom[i].val - 6;
2533 sc->sc_bbp17_dynmin = 0;
2538 sc->sc_bbp17_dynmax = RT2560_RXSNS_DYNMAX;
2540 sc->sc_bbp17_dynmin = sc->sc_bbp17_dynmax - 6;
2542 /* read Tx power for all b/g channels */
2543 for (i = 0; i < 14 / 2; i++) {
2544 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2545 sc->txpow[i * 2] = val & 0xff;
2546 sc->txpow[i * 2 + 1] = val >> 8;
2548 for (i = 0; i < 14; ++i) {
2549 if (sc->txpow[i] > 31)
2550 sc->txpow[i] = RT2560_DEFAULT_TXPOWER;
2551 DPRINTF(sc, ("tx power chan %d: %u\n", i + 1, sc->txpow[i]));
2554 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE);
2555 if ((val & 0xff) == 0xff)
2556 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR;
2558 sc->rssi_corr = val & 0xff;
2559 DPRINTF(sc, ("rssi correction %d, calibrate 0x%02x\n",
2560 sc->rssi_corr, val));
2562 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG1);
2565 if ((val & 0x2) == 0 && sc->asic_rev >= RT2560_ASICREV_D) {
2566 DPRINTF(sc, ("capable of RX sensitivity calibration\n"));
2567 sc->sc_flags |= RT2560_FLAG_RXSNS;
2572 rt2560_bbp_init(struct rt2560_softc *sc)
2574 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2577 /* wait for BBP to be ready */
2578 for (ntries = 0; ntries < 100; ntries++) {
2579 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2583 if (ntries == 100) {
2584 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2588 /* initialize BBP registers to default values */
2589 for (i = 0; i < N(rt2560_def_bbp); i++) {
2590 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2591 rt2560_def_bbp[i].val);
2594 /* initialize BBP registers to values stored in EEPROM */
2595 for (i = 0; i < 16; i++) {
2596 if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0)
2598 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2600 /* Set rx sensitivity to user specified value */
2601 rt2560_bbp_write(sc, 17, sc->sc_rxsns);
2608 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2613 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2615 tx |= RT2560_BBP_ANTA;
2616 else if (antenna == 2)
2617 tx |= RT2560_BBP_ANTB;
2619 tx |= RT2560_BBP_DIVERSITY;
2621 /* need to force I/Q flip for RF 2525e and 5222 */
2622 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_5222)
2623 tx |= RT2560_BBP_FLIPIQ;
2625 rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2627 /* update values for CCK and OFDM in BBPCSR1 */
2628 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2629 tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2630 RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2634 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2638 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2640 rx |= RT2560_BBP_ANTA;
2641 else if (antenna == 2)
2642 rx |= RT2560_BBP_ANTB;
2644 rx |= RT2560_BBP_DIVERSITY;
2646 /* need to force no I/Q flip for RF 2525e */
2647 if (sc->rf_rev == RT2560_RF_2525E)
2648 rx &= ~RT2560_BBP_FLIPIQ;
2650 rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2654 rt2560_init(void *priv)
2656 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2657 struct rt2560_softc *sc = priv;
2658 struct ieee80211com *ic = &sc->sc_ic;
2659 struct ifnet *ifp = ic->ic_ifp;
2665 /* setup tx rings */
2666 tmp = RT2560_PRIO_RING_COUNT << 24 |
2667 RT2560_ATIM_RING_COUNT << 16 |
2668 RT2560_TX_RING_COUNT << 8 |
2669 RT2560_TX_DESC_SIZE;
2671 /* rings must be initialized in this exact order */
2672 RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2673 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2674 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2675 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2676 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2679 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2681 RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2682 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2684 /* initialize MAC registers to default values */
2685 for (i = 0; i < N(rt2560_def_mac); i++)
2686 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2688 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2689 rt2560_set_macaddr(sc, ic->ic_myaddr);
2691 /* set basic rate set (will be updated later) */
2692 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2694 rt2560_update_slot(ifp);
2695 rt2560_update_plcp(sc);
2696 rt2560_update_led(sc, 0, 0);
2698 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2699 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2701 if (rt2560_bbp_init(sc) != 0) {
2706 rt2560_set_txantenna(sc, sc->tx_ant);
2707 rt2560_set_rxantenna(sc, sc->rx_ant);
2709 /* set default BSS channel */
2710 rt2560_set_chan(sc, ic->ic_curchan);
2713 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2714 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2715 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2716 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2717 tmp |= RT2560_DROP_TODS;
2718 if (!(ifp->if_flags & IFF_PROMISC))
2719 tmp |= RT2560_DROP_NOT_TO_ME;
2721 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2723 /* clear old FCS and Rx FIFO errors */
2724 RAL_READ(sc, RT2560_CNT0);
2725 RAL_READ(sc, RT2560_CNT4);
2727 /* clear any pending interrupts */
2728 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2730 /* enable interrupts */
2731 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2733 ifp->if_flags &= ~IFF_OACTIVE;
2734 ifp->if_flags |= IFF_RUNNING;
2737 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2740 ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
2741 for (i = 0; i < IEEE80211_WEP_NKID; ++i) {
2742 struct ieee80211_key *wk = &ic->ic_nw_keys[i];
2744 if (wk->wk_keylen == 0)
2746 if (wk->wk_flags & IEEE80211_KEY_XMIT)
2747 wk->wk_flags |= IEEE80211_KEY_SWCRYPT;
2751 sc->sc_avgrssi = -1;
2753 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2754 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2755 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2757 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2763 rt2560_stop(void *priv)
2765 struct rt2560_softc *sc = priv;
2766 struct ieee80211com *ic = &sc->sc_ic;
2767 struct ifnet *ifp = ic->ic_ifp;
2769 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2771 sc->sc_tx_timer = 0;
2772 sc->sc_flags &= ~(RT2560_FLAG_DATA_OACT | RT2560_FLAG_PRIO_OACT);
2774 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2777 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2780 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2782 /* reset ASIC (imply reset BBP) */
2783 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2784 RAL_WRITE(sc, RT2560_CSR1, 0);
2786 /* disable interrupts */
2787 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2789 /* reset Tx and Rx rings */
2790 rt2560_reset_tx_ring(sc, &sc->txq);
2791 rt2560_reset_tx_ring(sc, &sc->atimq);
2792 rt2560_reset_tx_ring(sc, &sc->prioq);
2793 rt2560_reset_tx_ring(sc, &sc->bcnq);
2794 rt2560_reset_rx_ring(sc, &sc->rxq);
2798 rt2560_dma_map_mbuf(void *arg, bus_dma_segment_t *seg, int nseg,
2799 bus_size_t map_size __unused, int error)
2804 KASSERT(nseg == 1, ("too many dma segments\n"));
2805 *((bus_addr_t *)arg) = seg->ds_addr;
2809 rt2560_ratectl_attach(struct ieee80211com *ic, u_int rc)
2811 struct rt2560_softc *sc = ic->ic_if.if_softc;
2814 case IEEE80211_RATECTL_SAMPLE:
2815 return &sc->sc_sample_param;
2816 case IEEE80211_RATECTL_ONOE:
2817 return &sc->sc_onoe_param;
2818 case IEEE80211_RATECTL_NONE:
2819 /* This could only happen during detaching */
2822 panic("unknown rate control algo %u\n", rc);
2828 rt2560_calib_rxsensitivity(struct rt2560_softc *sc, uint32_t false_cca)
2830 #define MID_RX_SENSITIVITY (RT2560_RXSNS_DYNMAX + 1)
2834 if (sc->sc_ic.ic_state != IEEE80211_S_RUN)
2837 rssi_dbm = sc->sc_avgrssi + RT2560_NOISE_FLOOR;
2838 DPRINTF(sc, ("rssi dbm %d\n", rssi_dbm));
2841 * Rx sensitivity is reduced, if bbp17 is increased, and vice versa.
2842 * Lower rx sensitivity could do a better job of reducing false CCA,
2843 * but on the other hand roaming range is decreased.
2846 if (rssi_dbm < -80) {
2847 /* Signal is too weak */
2849 } else if (rssi_dbm >= -74) {
2852 if (rssi_dbm >= -58)
2853 bbp17 = RT2560_RXSNS_MAX;
2855 bbp17 = MID_RX_SENSITIVITY;
2856 if (sc->sc_bbp17 != bbp17)
2857 rt2560_bbp_write(sc, 17, bbp17);
2861 /* RSSI is [-80,74)dBm, if we reach here */
2863 if (sc->sc_bbp17 > MID_RX_SENSITIVITY) {
2864 rt2560_bbp_write(sc, 17, MID_RX_SENSITIVITY);
2869 * Dynamic rx sensitivity tuning to keep balance between number
2870 * of false CCA per second and roaming range:
2871 * Reduce rx sensitivity if false CCA is too high.
2872 * If false CCA is relatively low, rx sensitivity is increased to
2873 * extend roaming range.
2875 if (false_cca > 512 && sc->sc_bbp17 < sc->sc_bbp17_dynmax)
2876 rt2560_bbp_write(sc, 17, sc->sc_bbp17 + 1);
2877 else if (false_cca < 100 && sc->sc_bbp17 > sc->sc_bbp17_dynmin)
2878 rt2560_bbp_write(sc, 17, sc->sc_bbp17 - 1);
2880 #undef MID_RX_SENSITIVITY
2884 rt2560_calibrate(void *xsc)
2886 struct rt2560_softc *sc = xsc;
2887 struct ifnet *ifp = &sc->sc_ic.ic_if;
2890 lwkt_serialize_enter(ifp->if_serializer);
2892 false_cca = RAL_READ(sc, RT2560_CNT3) & 0xffff;
2893 DPRINTF(sc, ("false CCA %u\n", false_cca));
2895 if (sc->sc_calib_rxsns)
2896 rt2560_calib_rxsensitivity(sc, false_cca);
2898 callout_reset(&sc->calib_ch, hz, rt2560_calibrate, sc);
2900 lwkt_serialize_exit(ifp->if_serializer);
2904 rt2560_sysctl_rxsns(SYSCTL_HANDLER_ARGS)
2906 struct rt2560_softc *sc = arg1;
2907 struct ifnet *ifp = &sc->sc_ic.ic_if;
2910 lwkt_serialize_enter(ifp->if_serializer);
2913 error = sysctl_handle_int(oidp, &v, 0, req);
2914 if (error || req->newptr == NULL)
2916 if (v < sc->sc_bbp17_dynmin || v > RT2560_RXSNS_MAX) {
2921 if (sc->sc_rxsns != v) {
2923 * Adjust bbp17 iff ral(4) is up and running (i.e. hardware
2924 * is initialized)and rx sensitivity calibration is _not_
2927 if ((ifp->if_flags & IFF_RUNNING) && !sc->sc_calib_rxsns)
2928 rt2560_bbp_write(sc, 17, v);
2932 lwkt_serialize_exit(ifp->if_serializer);