1 /* $NetBSD: rt2661.c,v 1.25 2008/11/07 00:20:03 dyoung Exp $ */
2 /* $OpenBSD: rt2661.c,v 1.17 2006/05/01 08:41:11 damien Exp $ */
3 /* $FreeBSD: rt2560.c,v 1.5 2006/06/02 19:59:31 csjp Exp $ */
7 * Damien Bergamini <damien.bergamini@free.fr>
9 * Permission to use, copy, modify, and distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
13 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
14 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
16 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
19 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23 * Ralink Technology RT2561, RT2561S and RT2661 chipset driver
24 * http://www.ralinktech.com/
27 #include <sys/cdefs.h>
28 __KERNEL_RCSID(0, "$NetBSD: rt2661.c,v 1.25 2008/11/07 00:20:03 dyoung Exp $");
32 #include <sys/param.h>
33 #include <sys/sockio.h>
34 #include <sys/sysctl.h>
36 #include <sys/kernel.h>
37 #include <sys/socket.h>
38 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/callout.h>
42 #include <sys/device.h>
45 #include <machine/endian.h>
52 #include <net/if_arp.h>
53 #include <net/if_dl.h>
54 #include <net/if_media.h>
55 #include <net/if_types.h>
56 #include <net/if_ether.h>
58 #include <netinet/in.h>
59 #include <netinet/in_systm.h>
60 #include <netinet/in_var.h>
61 #include <netinet/ip.h>
63 #include <net80211/ieee80211_var.h>
64 #include <net80211/ieee80211_amrr.h>
65 #include <net80211/ieee80211_radiotap.h>
67 #include <dev/ic/rt2661reg.h>
68 #include <dev/ic/rt2661var.h>
70 #include <dev/pci/pcireg.h>
71 #include <dev/pci/pcivar.h>
72 #include <dev/pci/pcidevs.h>
74 #include <dev/firmload.h>
77 #define DPRINTF(x) do { if (rt2661_debug > 0) printf x; } while (0)
78 #define DPRINTFN(n, x) do { if (rt2661_debug >= (n)) printf x; } while (0)
82 #define DPRINTFN(n, x)
85 static int rt2661_alloc_tx_ring(struct rt2661_softc
*,
86 struct rt2661_tx_ring
*, int);
87 static void rt2661_reset_tx_ring(struct rt2661_softc
*,
88 struct rt2661_tx_ring
*);
89 static void rt2661_free_tx_ring(struct rt2661_softc
*,
90 struct rt2661_tx_ring
*);
91 static int rt2661_alloc_rx_ring(struct rt2661_softc
*,
92 struct rt2661_rx_ring
*, int);
93 static void rt2661_reset_rx_ring(struct rt2661_softc
*,
94 struct rt2661_rx_ring
*);
95 static void rt2661_free_rx_ring(struct rt2661_softc
*,
96 struct rt2661_rx_ring
*);
97 static struct ieee80211_node
*
98 rt2661_node_alloc(struct ieee80211_node_table
*);
99 static int rt2661_media_change(struct ifnet
*);
100 static void rt2661_next_scan(void *);
101 static void rt2661_iter_func(void *, struct ieee80211_node
*);
102 static void rt2661_updatestats(void *);
103 static void rt2661_newassoc(struct ieee80211_node
*, int);
104 static int rt2661_newstate(struct ieee80211com
*, enum ieee80211_state
,
106 static uint16_t rt2661_eeprom_read(struct rt2661_softc
*, uint8_t);
107 static void rt2661_tx_intr(struct rt2661_softc
*);
108 static void rt2661_tx_dma_intr(struct rt2661_softc
*,
109 struct rt2661_tx_ring
*);
110 static void rt2661_rx_intr(struct rt2661_softc
*);
111 static void rt2661_mcu_beacon_expire(struct rt2661_softc
*);
112 static void rt2661_mcu_wakeup(struct rt2661_softc
*);
113 static void rt2661_mcu_cmd_intr(struct rt2661_softc
*);
114 int rt2661_intr(void *);
116 static uint8_t rt2661_rxrate(struct rt2661_rx_desc
*);
118 static int rt2661_ack_rate(struct ieee80211com
*, int);
119 static uint16_t rt2661_txtime(int, int, uint32_t);
120 static uint8_t rt2661_plcp_signal(int);
121 static void rt2661_setup_tx_desc(struct rt2661_softc
*,
122 struct rt2661_tx_desc
*, uint32_t, uint16_t, int, int,
123 const bus_dma_segment_t
*, int, int);
124 static int rt2661_tx_mgt(struct rt2661_softc
*, struct mbuf
*,
125 struct ieee80211_node
*);
127 rt2661_get_rts(struct rt2661_softc
*,
128 struct ieee80211_frame
*, uint16_t);
129 static int rt2661_tx_data(struct rt2661_softc
*, struct mbuf
*,
130 struct ieee80211_node
*, int);
131 static void rt2661_start(struct ifnet
*);
132 static void rt2661_watchdog(struct ifnet
*);
133 static int rt2661_reset(struct ifnet
*);
134 static int rt2661_ioctl(struct ifnet
*, u_long
, void *);
135 static void rt2661_bbp_write(struct rt2661_softc
*, uint8_t, uint8_t);
136 static uint8_t rt2661_bbp_read(struct rt2661_softc
*, uint8_t);
137 static void rt2661_rf_write(struct rt2661_softc
*, uint8_t, uint32_t);
138 static int rt2661_tx_cmd(struct rt2661_softc
*, uint8_t, uint16_t);
139 static void rt2661_select_antenna(struct rt2661_softc
*);
140 static void rt2661_enable_mrr(struct rt2661_softc
*);
141 static void rt2661_set_txpreamble(struct rt2661_softc
*);
142 static void rt2661_set_basicrates(struct rt2661_softc
*,
143 const struct ieee80211_rateset
*);
144 static void rt2661_select_band(struct rt2661_softc
*,
145 struct ieee80211_channel
*);
146 static void rt2661_set_chan(struct rt2661_softc
*,
147 struct ieee80211_channel
*);
148 static void rt2661_set_bssid(struct rt2661_softc
*, const uint8_t *);
149 static void rt2661_set_macaddr(struct rt2661_softc
*, const uint8_t *);
150 static void rt2661_update_promisc(struct rt2661_softc
*);
152 static int rt2661_wme_update(struct ieee80211com
*);
155 static void rt2661_updateslot(struct ifnet
*);
156 static void rt2661_set_slottime(struct rt2661_softc
*);
159 static void rt2661_read_eeprom(struct rt2661_softc
*);
160 static int rt2661_bbp_init(struct rt2661_softc
*);
161 static int rt2661_init(struct ifnet
*);
162 static void rt2661_stop(struct ifnet
*, int);
163 static int rt2661_load_microcode(struct rt2661_softc
*, const uint8_t *,
165 static void rt2661_rx_tune(struct rt2661_softc
*);
167 static void rt2661_radar_start(struct rt2661_softc
*);
168 static int rt2661_radar_stop(struct rt2661_softc
*);
170 static int rt2661_prepare_beacon(struct rt2661_softc
*);
171 static void rt2661_enable_tsf_sync(struct rt2661_softc
*);
172 static int rt2661_get_rssi(struct rt2661_softc
*, uint8_t);
175 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
177 static const struct ieee80211_rateset rt2661_rateset_11a
=
178 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
180 static const struct ieee80211_rateset rt2661_rateset_11b
=
181 { 4, { 2, 4, 11, 22 } };
183 static const struct ieee80211_rateset rt2661_rateset_11g
=
184 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
186 static const struct {
189 } rt2661_def_mac
[] = {
193 static const struct {
196 } rt2661_def_bbp
[] = {
200 static const struct rfprog
{
202 uint32_t r1
, r2
, r3
, r4
;
203 } rt2661_rf5225_1
[] = {
205 }, rt2661_rf5225_2
[] = {
210 rt2661_attach(void *xsc
, int id
)
212 struct rt2661_softc
*sc
= xsc
;
213 struct ieee80211com
*ic
= &sc
->sc_ic
;
214 struct ifnet
*ifp
= &sc
->sc_if
;
216 int error
, i
, ntries
;
220 sc
->amrr
.amrr_min_success_threshold
= 1;
221 sc
->amrr
.amrr_max_success_threshold
= 15;
222 callout_init(&sc
->scan_ch
, 0);
223 callout_init(&sc
->amrr_ch
, 0);
225 /* wait for NIC to initialize */
226 for (ntries
= 0; ntries
< 1000; ntries
++) {
227 if ((val
= RAL_READ(sc
, RT2661_MAC_CSR0
)) != 0)
231 if (ntries
== 1000) {
232 aprint_error_dev(&sc
->sc_dev
, "timeout waiting for NIC to initialize\n");
236 /* retrieve RF rev. no and various other things from EEPROM */
237 rt2661_read_eeprom(sc
);
238 aprint_normal_dev(&sc
->sc_dev
, "802.11 address %s\n",
239 ether_sprintf(ic
->ic_myaddr
));
241 aprint_normal_dev(&sc
->sc_dev
, "MAC/BBP RT%X, RF %s\n", val
,
242 rt2661_get_rf(sc
->rf_rev
));
245 * Allocate Tx and Rx rings.
247 error
= rt2661_alloc_tx_ring(sc
, &sc
->txq
[0], RT2661_TX_RING_COUNT
);
249 aprint_error_dev(&sc
->sc_dev
, "could not allocate Tx ring 0\n");
253 error
= rt2661_alloc_tx_ring(sc
, &sc
->txq
[1], RT2661_TX_RING_COUNT
);
255 aprint_error_dev(&sc
->sc_dev
, "could not allocate Tx ring 1\n");
259 error
= rt2661_alloc_tx_ring(sc
, &sc
->txq
[2], RT2661_TX_RING_COUNT
);
261 aprint_error_dev(&sc
->sc_dev
, "could not allocate Tx ring 2\n");
265 error
= rt2661_alloc_tx_ring(sc
, &sc
->txq
[3], RT2661_TX_RING_COUNT
);
267 aprint_error_dev(&sc
->sc_dev
, "could not allocate Tx ring 3\n");
271 error
= rt2661_alloc_tx_ring(sc
, &sc
->mgtq
, RT2661_MGT_RING_COUNT
);
273 aprint_error_dev(&sc
->sc_dev
, "could not allocate Mgt ring\n");
277 error
= rt2661_alloc_rx_ring(sc
, &sc
->rxq
, RT2661_RX_RING_COUNT
);
279 aprint_error_dev(&sc
->sc_dev
, "could not allocate Rx ring\n");
284 ifp
->if_flags
= IFF_BROADCAST
| IFF_SIMPLEX
| IFF_MULTICAST
;
285 ifp
->if_init
= rt2661_init
;
286 ifp
->if_stop
= rt2661_stop
;
287 ifp
->if_ioctl
= rt2661_ioctl
;
288 ifp
->if_start
= rt2661_start
;
289 ifp
->if_watchdog
= rt2661_watchdog
;
290 IFQ_SET_READY(&ifp
->if_snd
);
291 memcpy(ifp
->if_xname
, device_xname(&sc
->sc_dev
), IFNAMSIZ
);
294 ic
->ic_phytype
= IEEE80211_T_OFDM
; /* not only, but not used */
295 ic
->ic_opmode
= IEEE80211_M_STA
; /* default to BSS mode */
296 ic
->ic_state
= IEEE80211_S_INIT
;
298 /* set device capabilities */
300 IEEE80211_C_IBSS
| /* IBSS mode supported */
301 IEEE80211_C_MONITOR
| /* monitor mode supported */
302 IEEE80211_C_HOSTAP
| /* HostAP mode supported */
303 IEEE80211_C_TXPMGT
| /* tx power management */
304 IEEE80211_C_SHPREAMBLE
| /* short preamble supported */
305 IEEE80211_C_SHSLOT
| /* short slot time supported */
306 IEEE80211_C_WPA
; /* 802.11i */
308 if (sc
->rf_rev
== RT2661_RF_5225
|| sc
->rf_rev
== RT2661_RF_5325
) {
309 /* set supported .11a rates */
310 ic
->ic_sup_rates
[IEEE80211_MODE_11A
] = rt2661_rateset_11a
;
312 /* set supported .11a channels */
313 for (i
= 36; i
<= 64; 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
;
318 for (i
= 100; i
<= 140; i
+= 4) {
319 ic
->ic_channels
[i
].ic_freq
=
320 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_5GHZ
);
321 ic
->ic_channels
[i
].ic_flags
= IEEE80211_CHAN_A
;
323 for (i
= 149; i
<= 165; i
+= 4) {
324 ic
->ic_channels
[i
].ic_freq
=
325 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_5GHZ
);
326 ic
->ic_channels
[i
].ic_flags
= IEEE80211_CHAN_A
;
330 /* set supported .11b and .11g rates */
331 ic
->ic_sup_rates
[IEEE80211_MODE_11B
] = rt2661_rateset_11b
;
332 ic
->ic_sup_rates
[IEEE80211_MODE_11G
] = rt2661_rateset_11g
;
334 /* set supported .11b and .11g channels (1 through 14) */
335 for (i
= 1; i
<= 14; i
++) {
336 ic
->ic_channels
[i
].ic_freq
=
337 ieee80211_ieee2mhz(i
, IEEE80211_CHAN_2GHZ
);
338 ic
->ic_channels
[i
].ic_flags
=
339 IEEE80211_CHAN_CCK
| IEEE80211_CHAN_OFDM
|
340 IEEE80211_CHAN_DYN
| IEEE80211_CHAN_2GHZ
;
344 ieee80211_ifattach(ic
);
345 ic
->ic_node_alloc
= rt2661_node_alloc
;
346 ic
->ic_newassoc
= rt2661_newassoc
;
347 ic
->ic_updateslot
= rt2661_updateslot
;
348 ic
->ic_reset
= rt2661_reset
;
350 /* override state transition machine */
351 sc
->sc_newstate
= ic
->ic_newstate
;
352 ic
->ic_newstate
= rt2661_newstate
;
353 ieee80211_media_init(ic
, rt2661_media_change
, ieee80211_media_status
);
356 bpfattach2(ifp
, DLT_IEEE802_11_RADIO
,
357 sizeof (struct ieee80211_frame
) + sizeof(sc
->sc_txtap
),
360 sc
->sc_rxtap_len
= roundup(sizeof(sc
->sc_rxtap
), sizeof(u_int32_t
));
361 sc
->sc_rxtap
.wr_ihdr
.it_len
= htole16(sc
->sc_rxtap_len
);
362 sc
->sc_rxtap
.wr_ihdr
.it_present
= htole32(RT2661_RX_RADIOTAP_PRESENT
);
364 sc
->sc_txtap_len
= roundup(sizeof(sc
->sc_txtap
), sizeof(u_int32_t
));
365 sc
->sc_txtap
.wt_ihdr
.it_len
= htole16(sc
->sc_txtap_len
);
366 sc
->sc_txtap
.wt_ihdr
.it_present
= htole32(RT2661_TX_RADIOTAP_PRESENT
);
369 ieee80211_announce(ic
);
371 if (pmf_device_register(&sc
->sc_dev
, NULL
, NULL
))
372 pmf_class_network_register(&sc
->sc_dev
, ifp
);
374 aprint_error_dev(&sc
->sc_dev
,
375 "couldn't establish power handler\n");
379 fail6
: rt2661_free_tx_ring(sc
, &sc
->mgtq
);
380 fail5
: rt2661_free_tx_ring(sc
, &sc
->txq
[3]);
381 fail4
: rt2661_free_tx_ring(sc
, &sc
->txq
[2]);
382 fail3
: rt2661_free_tx_ring(sc
, &sc
->txq
[1]);
383 fail2
: rt2661_free_tx_ring(sc
, &sc
->txq
[0]);
388 rt2661_detach(void *xsc
)
390 struct rt2661_softc
*sc
= xsc
;
391 struct ifnet
*ifp
= &sc
->sc_if
;
393 callout_stop(&sc
->scan_ch
);
394 callout_stop(&sc
->amrr_ch
);
396 pmf_device_deregister(&sc
->sc_dev
);
398 ieee80211_ifdetach(&sc
->sc_ic
);
401 rt2661_free_tx_ring(sc
, &sc
->txq
[0]);
402 rt2661_free_tx_ring(sc
, &sc
->txq
[1]);
403 rt2661_free_tx_ring(sc
, &sc
->txq
[2]);
404 rt2661_free_tx_ring(sc
, &sc
->txq
[3]);
405 rt2661_free_tx_ring(sc
, &sc
->mgtq
);
406 rt2661_free_rx_ring(sc
, &sc
->rxq
);
412 rt2661_alloc_tx_ring(struct rt2661_softc
*sc
, struct rt2661_tx_ring
*ring
,
419 ring
->cur
= ring
->next
= ring
->stat
= 0;
421 error
= bus_dmamap_create(sc
->sc_dmat
, count
* RT2661_TX_DESC_SIZE
, 1,
422 count
* RT2661_TX_DESC_SIZE
, 0, BUS_DMA_NOWAIT
, &ring
->map
);
424 aprint_error_dev(&sc
->sc_dev
, "could not create desc DMA map\n");
428 error
= bus_dmamem_alloc(sc
->sc_dmat
, count
* RT2661_TX_DESC_SIZE
,
429 PAGE_SIZE
, 0, &ring
->seg
, 1, &nsegs
, BUS_DMA_NOWAIT
);
431 aprint_error_dev(&sc
->sc_dev
, "could not allocate DMA memory\n");
435 error
= bus_dmamem_map(sc
->sc_dmat
, &ring
->seg
, nsegs
,
436 count
* RT2661_TX_DESC_SIZE
, (void **)&ring
->desc
,
439 aprint_error_dev(&sc
->sc_dev
, "could not map desc DMA memory\n");
443 error
= bus_dmamap_load(sc
->sc_dmat
, ring
->map
, ring
->desc
,
444 count
* RT2661_TX_DESC_SIZE
, NULL
, BUS_DMA_NOWAIT
);
446 aprint_error_dev(&sc
->sc_dev
, "could not load desc DMA map\n");
450 memset(ring
->desc
, 0, count
* RT2661_TX_DESC_SIZE
);
451 ring
->physaddr
= ring
->map
->dm_segs
->ds_addr
;
453 ring
->data
= malloc(count
* sizeof (struct rt2661_tx_data
), M_DEVBUF
,
455 if (ring
->data
== NULL
) {
456 aprint_error_dev(&sc
->sc_dev
, "could not allocate soft data\n");
461 memset(ring
->data
, 0, count
* sizeof (struct rt2661_tx_data
));
462 for (i
= 0; i
< count
; i
++) {
463 error
= bus_dmamap_create(sc
->sc_dmat
, MCLBYTES
,
464 RT2661_MAX_SCATTER
, MCLBYTES
, 0, BUS_DMA_NOWAIT
,
467 aprint_error_dev(&sc
->sc_dev
, "could not create DMA map\n");
474 fail
: rt2661_free_tx_ring(sc
, ring
);
479 rt2661_reset_tx_ring(struct rt2661_softc
*sc
, struct rt2661_tx_ring
*ring
)
481 struct rt2661_tx_desc
*desc
;
482 struct rt2661_tx_data
*data
;
485 for (i
= 0; i
< ring
->count
; i
++) {
486 desc
= &ring
->desc
[i
];
487 data
= &ring
->data
[i
];
489 if (data
->m
!= NULL
) {
490 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0,
491 data
->map
->dm_mapsize
, BUS_DMASYNC_POSTWRITE
);
492 bus_dmamap_unload(sc
->sc_dmat
, data
->map
);
497 if (data
->ni
!= NULL
) {
498 ieee80211_free_node(data
->ni
);
505 bus_dmamap_sync(sc
->sc_dmat
, ring
->map
, 0, ring
->map
->dm_mapsize
,
506 BUS_DMASYNC_PREWRITE
);
509 ring
->cur
= ring
->next
= ring
->stat
= 0;
514 rt2661_free_tx_ring(struct rt2661_softc
*sc
, struct rt2661_tx_ring
*ring
)
516 struct rt2661_tx_data
*data
;
519 if (ring
->desc
!= NULL
) {
520 bus_dmamap_sync(sc
->sc_dmat
, ring
->map
, 0,
521 ring
->map
->dm_mapsize
, BUS_DMASYNC_POSTWRITE
);
522 bus_dmamap_unload(sc
->sc_dmat
, ring
->map
);
523 bus_dmamem_unmap(sc
->sc_dmat
, (void *)ring
->desc
,
524 ring
->count
* RT2661_TX_DESC_SIZE
);
525 bus_dmamem_free(sc
->sc_dmat
, &ring
->seg
, 1);
528 if (ring
->data
!= NULL
) {
529 for (i
= 0; i
< ring
->count
; i
++) {
530 data
= &ring
->data
[i
];
532 if (data
->m
!= NULL
) {
533 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0,
534 data
->map
->dm_mapsize
,
535 BUS_DMASYNC_POSTWRITE
);
536 bus_dmamap_unload(sc
->sc_dmat
, data
->map
);
540 if (data
->ni
!= NULL
)
541 ieee80211_free_node(data
->ni
);
543 if (data
->map
!= NULL
)
544 bus_dmamap_destroy(sc
->sc_dmat
, data
->map
);
546 free(ring
->data
, M_DEVBUF
);
551 rt2661_alloc_rx_ring(struct rt2661_softc
*sc
, struct rt2661_rx_ring
*ring
,
554 struct rt2661_rx_desc
*desc
;
555 struct rt2661_rx_data
*data
;
559 ring
->cur
= ring
->next
= 0;
561 error
= bus_dmamap_create(sc
->sc_dmat
, count
* RT2661_RX_DESC_SIZE
, 1,
562 count
* RT2661_RX_DESC_SIZE
, 0, BUS_DMA_NOWAIT
, &ring
->map
);
564 aprint_error_dev(&sc
->sc_dev
, "could not create desc DMA map\n");
568 error
= bus_dmamem_alloc(sc
->sc_dmat
, count
* RT2661_RX_DESC_SIZE
,
569 PAGE_SIZE
, 0, &ring
->seg
, 1, &nsegs
, BUS_DMA_NOWAIT
);
571 aprint_error_dev(&sc
->sc_dev
, "could not allocate DMA memory\n");
575 error
= bus_dmamem_map(sc
->sc_dmat
, &ring
->seg
, nsegs
,
576 count
* RT2661_RX_DESC_SIZE
, (void **)&ring
->desc
,
579 aprint_error_dev(&sc
->sc_dev
, "could not map desc DMA memory\n");
583 error
= bus_dmamap_load(sc
->sc_dmat
, ring
->map
, ring
->desc
,
584 count
* RT2661_RX_DESC_SIZE
, NULL
, BUS_DMA_NOWAIT
);
586 aprint_error_dev(&sc
->sc_dev
, "could not load desc DMA map\n");
590 memset(ring
->desc
, 0, count
* RT2661_RX_DESC_SIZE
);
591 ring
->physaddr
= ring
->map
->dm_segs
->ds_addr
;
593 ring
->data
= malloc(count
* sizeof (struct rt2661_rx_data
), M_DEVBUF
,
595 if (ring
->data
== NULL
) {
596 aprint_error_dev(&sc
->sc_dev
, "could not allocate soft data\n");
602 * Pre-allocate Rx buffers and populate Rx ring.
604 memset(ring
->data
, 0, count
* sizeof (struct rt2661_rx_data
));
605 for (i
= 0; i
< count
; i
++) {
606 desc
= &sc
->rxq
.desc
[i
];
607 data
= &sc
->rxq
.data
[i
];
609 error
= bus_dmamap_create(sc
->sc_dmat
, MCLBYTES
, 1, MCLBYTES
,
610 0, BUS_DMA_NOWAIT
, &data
->map
);
612 aprint_error_dev(&sc
->sc_dev
, "could not create DMA map\n");
616 MGETHDR(data
->m
, M_DONTWAIT
, MT_DATA
);
617 if (data
->m
== NULL
) {
618 aprint_error_dev(&sc
->sc_dev
, "could not allocate rx mbuf\n");
623 MCLGET(data
->m
, M_DONTWAIT
);
624 if (!(data
->m
->m_flags
& M_EXT
)) {
625 aprint_error_dev(&sc
->sc_dev
, "could not allocate rx mbuf cluster\n");
630 error
= bus_dmamap_load(sc
->sc_dmat
, data
->map
,
631 mtod(data
->m
, void *), MCLBYTES
, NULL
, BUS_DMA_NOWAIT
);
633 aprint_error_dev(&sc
->sc_dev
, "could not load rx buf DMA map");
637 desc
->physaddr
= htole32(data
->map
->dm_segs
->ds_addr
);
638 desc
->flags
= htole32(RT2661_RX_BUSY
);
641 bus_dmamap_sync(sc
->sc_dmat
, ring
->map
, 0, ring
->map
->dm_mapsize
,
642 BUS_DMASYNC_PREWRITE
);
646 fail
: rt2661_free_rx_ring(sc
, ring
);
651 rt2661_reset_rx_ring(struct rt2661_softc
*sc
, struct rt2661_rx_ring
*ring
)
655 for (i
= 0; i
< ring
->count
; i
++)
656 ring
->desc
[i
].flags
= htole32(RT2661_RX_BUSY
);
658 bus_dmamap_sync(sc
->sc_dmat
, ring
->map
, 0, ring
->map
->dm_mapsize
,
659 BUS_DMASYNC_PREWRITE
);
661 ring
->cur
= ring
->next
= 0;
665 rt2661_free_rx_ring(struct rt2661_softc
*sc
, struct rt2661_rx_ring
*ring
)
667 struct rt2661_rx_data
*data
;
670 if (ring
->desc
!= NULL
) {
671 bus_dmamap_sync(sc
->sc_dmat
, ring
->map
, 0,
672 ring
->map
->dm_mapsize
, BUS_DMASYNC_POSTWRITE
);
673 bus_dmamap_unload(sc
->sc_dmat
, ring
->map
);
674 bus_dmamem_unmap(sc
->sc_dmat
, (void *)ring
->desc
,
675 ring
->count
* RT2661_RX_DESC_SIZE
);
676 bus_dmamem_free(sc
->sc_dmat
, &ring
->seg
, 1);
679 if (ring
->data
!= NULL
) {
680 for (i
= 0; i
< ring
->count
; i
++) {
681 data
= &ring
->data
[i
];
683 if (data
->m
!= NULL
) {
684 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0,
685 data
->map
->dm_mapsize
,
686 BUS_DMASYNC_POSTREAD
);
687 bus_dmamap_unload(sc
->sc_dmat
, data
->map
);
691 if (data
->map
!= NULL
)
692 bus_dmamap_destroy(sc
->sc_dmat
, data
->map
);
694 free(ring
->data
, M_DEVBUF
);
698 static struct ieee80211_node
*
699 rt2661_node_alloc(struct ieee80211_node_table
*nt
)
701 struct rt2661_node
*rn
;
703 rn
= malloc(sizeof (struct rt2661_node
), M_80211_NODE
,
706 return (rn
!= NULL
) ? &rn
->ni
: NULL
;
710 rt2661_media_change(struct ifnet
*ifp
)
714 error
= ieee80211_media_change(ifp
);
715 if (error
!= ENETRESET
)
718 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) == (IFF_UP
| IFF_RUNNING
))
725 * This function is called periodically (every 200ms) during scanning to
726 * switch from one channel to another.
729 rt2661_next_scan(void *arg
)
731 struct rt2661_softc
*sc
= arg
;
732 struct ieee80211com
*ic
= &sc
->sc_ic
;
736 if (ic
->ic_state
== IEEE80211_S_SCAN
)
737 ieee80211_next_scan(ic
);
742 * This function is called for each neighbor node.
745 rt2661_iter_func(void *arg
, struct ieee80211_node
*ni
)
747 struct rt2661_softc
*sc
= arg
;
748 struct rt2661_node
*rn
= (struct rt2661_node
*)ni
;
750 ieee80211_amrr_choose(&sc
->amrr
, ni
, &rn
->amn
);
754 * This function is called periodically (every 500ms) in RUN state to update
755 * various settings like rate control statistics or Rx sensitivity.
758 rt2661_updatestats(void *arg
)
760 struct rt2661_softc
*sc
= arg
;
761 struct ieee80211com
*ic
= &sc
->sc_ic
;
765 if (ic
->ic_opmode
== IEEE80211_M_STA
)
766 rt2661_iter_func(sc
, ic
->ic_bss
);
768 ieee80211_iterate_nodes(&ic
->ic_sta
, rt2661_iter_func
, arg
);
770 /* update rx sensitivity every 1 sec */
771 if (++sc
->ncalls
& 1)
775 callout_reset(&sc
->amrr_ch
, hz
/ 2, rt2661_updatestats
, sc
);
779 rt2661_newassoc(struct ieee80211_node
*ni
, int isnew
)
781 struct rt2661_softc
*sc
= ni
->ni_ic
->ic_ifp
->if_softc
;
784 ieee80211_amrr_node_init(&sc
->amrr
, &((struct rt2661_node
*)ni
)->amn
);
786 /* set rate to some reasonable initial value */
787 for (i
= ni
->ni_rates
.rs_nrates
- 1;
788 i
> 0 && (ni
->ni_rates
.rs_rates
[i
] & IEEE80211_RATE_VAL
) > 72;
794 rt2661_newstate(struct ieee80211com
*ic
, enum ieee80211_state nstate
, int arg
)
796 struct rt2661_softc
*sc
= ic
->ic_ifp
->if_softc
;
797 enum ieee80211_state ostate
;
798 struct ieee80211_node
*ni
;
801 ostate
= ic
->ic_state
;
802 callout_stop(&sc
->scan_ch
);
805 case IEEE80211_S_INIT
:
806 callout_stop(&sc
->amrr_ch
);
808 if (ostate
== IEEE80211_S_RUN
) {
809 /* abort TSF synchronization */
810 tmp
= RAL_READ(sc
, RT2661_TXRX_CSR9
);
811 RAL_WRITE(sc
, RT2661_TXRX_CSR9
, tmp
& ~0x00ffffff);
815 case IEEE80211_S_SCAN
:
816 rt2661_set_chan(sc
, ic
->ic_curchan
);
817 callout_reset(&sc
->scan_ch
, hz
/ 5, rt2661_next_scan
, sc
);
820 case IEEE80211_S_AUTH
:
821 case IEEE80211_S_ASSOC
:
822 rt2661_set_chan(sc
, ic
->ic_curchan
);
825 case IEEE80211_S_RUN
:
826 rt2661_set_chan(sc
, ic
->ic_curchan
);
830 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
831 rt2661_set_slottime(sc
);
832 rt2661_enable_mrr(sc
);
833 rt2661_set_txpreamble(sc
);
834 rt2661_set_basicrates(sc
, &ni
->ni_rates
);
835 rt2661_set_bssid(sc
, ni
->ni_bssid
);
838 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
||
839 ic
->ic_opmode
== IEEE80211_M_IBSS
)
840 rt2661_prepare_beacon(sc
);
842 if (ic
->ic_opmode
== IEEE80211_M_STA
) {
843 /* fake a join to init the tx rate */
844 rt2661_newassoc(ni
, 1);
847 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
849 sc
->avg_rssi
= -95; /* reset EMA */
850 callout_reset(&sc
->amrr_ch
, hz
/ 2,
851 rt2661_updatestats
, sc
);
852 rt2661_enable_tsf_sync(sc
);
857 return sc
->sc_newstate(ic
, nstate
, arg
);
861 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
865 rt2661_eeprom_read(struct rt2661_softc
*sc
, uint8_t addr
)
871 /* clock C once before the first command */
872 RT2661_EEPROM_CTL(sc
, 0);
874 RT2661_EEPROM_CTL(sc
, RT2661_S
);
875 RT2661_EEPROM_CTL(sc
, RT2661_S
| RT2661_C
);
876 RT2661_EEPROM_CTL(sc
, RT2661_S
);
878 /* write start bit (1) */
879 RT2661_EEPROM_CTL(sc
, RT2661_S
| RT2661_D
);
880 RT2661_EEPROM_CTL(sc
, RT2661_S
| RT2661_D
| RT2661_C
);
882 /* write READ opcode (10) */
883 RT2661_EEPROM_CTL(sc
, RT2661_S
| RT2661_D
);
884 RT2661_EEPROM_CTL(sc
, RT2661_S
| RT2661_D
| RT2661_C
);
885 RT2661_EEPROM_CTL(sc
, RT2661_S
);
886 RT2661_EEPROM_CTL(sc
, RT2661_S
| RT2661_C
);
888 /* write address (A5-A0 or A7-A0) */
889 n
= (RAL_READ(sc
, RT2661_E2PROM_CSR
) & RT2661_93C46
) ? 5 : 7;
890 for (; n
>= 0; n
--) {
891 RT2661_EEPROM_CTL(sc
, RT2661_S
|
892 (((addr
>> n
) & 1) << RT2661_SHIFT_D
));
893 RT2661_EEPROM_CTL(sc
, RT2661_S
|
894 (((addr
>> n
) & 1) << RT2661_SHIFT_D
) | RT2661_C
);
897 RT2661_EEPROM_CTL(sc
, RT2661_S
);
899 /* read data Q15-Q0 */
901 for (n
= 15; n
>= 0; n
--) {
902 RT2661_EEPROM_CTL(sc
, RT2661_S
| RT2661_C
);
903 tmp
= RAL_READ(sc
, RT2661_E2PROM_CSR
);
904 val
|= ((tmp
& RT2661_Q
) >> RT2661_SHIFT_Q
) << n
;
905 RT2661_EEPROM_CTL(sc
, RT2661_S
);
908 RT2661_EEPROM_CTL(sc
, 0);
910 /* clear Chip Select and clock C */
911 RT2661_EEPROM_CTL(sc
, RT2661_S
);
912 RT2661_EEPROM_CTL(sc
, 0);
913 RT2661_EEPROM_CTL(sc
, RT2661_C
);
919 rt2661_tx_intr(struct rt2661_softc
*sc
)
921 struct ifnet
*ifp
= &sc
->sc_if
;
922 struct rt2661_tx_ring
*txq
;
923 struct rt2661_tx_data
*data
;
924 struct rt2661_node
*rn
;
929 val
= RAL_READ(sc
, RT2661_STA_CSR4
);
930 if (!(val
& RT2661_TX_STAT_VALID
))
933 /* retrieve the queue in which this frame was sent */
934 qid
= RT2661_TX_QID(val
);
935 txq
= (qid
<= 3) ? &sc
->txq
[qid
] : &sc
->mgtq
;
937 /* retrieve rate control algorithm context */
938 data
= &txq
->data
[txq
->stat
];
939 rn
= (struct rt2661_node
*)data
->ni
;
941 /* if no frame has been sent, ignore */
945 switch (RT2661_TX_RESULT(val
)) {
946 case RT2661_TX_SUCCESS
:
947 retrycnt
= RT2661_TX_RETRYCNT(val
);
949 DPRINTFN(10, ("data frame sent successfully after "
950 "%d retries\n", retrycnt
));
953 rn
->amn
.amn_retrycnt
++;
957 case RT2661_TX_RETRY_FAIL
:
958 DPRINTFN(9, ("sending data frame failed (too much "
961 rn
->amn
.amn_retrycnt
++;
967 aprint_error_dev(&sc
->sc_dev
, "sending data frame failed 0x%08x\n", val
);
971 ieee80211_free_node(data
->ni
);
974 DPRINTFN(15, ("tx done q=%d idx=%u\n", qid
, txq
->stat
));
977 if (++txq
->stat
>= txq
->count
) /* faster than % count */
982 ifp
->if_flags
&= ~IFF_OACTIVE
;
987 rt2661_tx_dma_intr(struct rt2661_softc
*sc
, struct rt2661_tx_ring
*txq
)
989 struct rt2661_tx_desc
*desc
;
990 struct rt2661_tx_data
*data
;
993 desc
= &txq
->desc
[txq
->next
];
994 data
= &txq
->data
[txq
->next
];
996 bus_dmamap_sync(sc
->sc_dmat
, txq
->map
,
997 txq
->next
* RT2661_TX_DESC_SIZE
, RT2661_TX_DESC_SIZE
,
998 BUS_DMASYNC_POSTREAD
);
1000 if ((le32toh(desc
->flags
) & RT2661_TX_BUSY
) ||
1001 !(le32toh(desc
->flags
) & RT2661_TX_VALID
))
1004 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0,
1005 data
->map
->dm_mapsize
, BUS_DMASYNC_POSTWRITE
);
1006 bus_dmamap_unload(sc
->sc_dmat
, data
->map
);
1009 /* node reference is released in rt2661_tx_intr() */
1011 /* descriptor is no longer valid */
1012 desc
->flags
&= ~htole32(RT2661_TX_VALID
);
1014 bus_dmamap_sync(sc
->sc_dmat
, txq
->map
,
1015 txq
->next
* RT2661_TX_DESC_SIZE
, RT2661_TX_DESC_SIZE
,
1016 BUS_DMASYNC_PREWRITE
);
1018 DPRINTFN(15, ("tx dma done q=%p idx=%u\n", txq
, txq
->next
));
1020 if (++txq
->next
>= txq
->count
) /* faster than % count */
1026 rt2661_rx_intr(struct rt2661_softc
*sc
)
1028 struct ieee80211com
*ic
= &sc
->sc_ic
;
1029 struct ifnet
*ifp
= &sc
->sc_if
;
1030 struct rt2661_rx_desc
*desc
;
1031 struct rt2661_rx_data
*data
;
1032 struct ieee80211_frame
*wh
;
1033 struct ieee80211_node
*ni
;
1034 struct mbuf
*mnew
, *m
;
1038 desc
= &sc
->rxq
.desc
[sc
->rxq
.cur
];
1039 data
= &sc
->rxq
.data
[sc
->rxq
.cur
];
1041 bus_dmamap_sync(sc
->sc_dmat
, sc
->rxq
.map
,
1042 sc
->rxq
.cur
* RT2661_RX_DESC_SIZE
, RT2661_RX_DESC_SIZE
,
1043 BUS_DMASYNC_POSTREAD
);
1045 if (le32toh(desc
->flags
) & RT2661_RX_BUSY
)
1048 if ((le32toh(desc
->flags
) & RT2661_RX_PHY_ERROR
) ||
1049 (le32toh(desc
->flags
) & RT2661_RX_CRC_ERROR
)) {
1051 * This should not happen since we did not request
1052 * to receive those frames when we filled TXRX_CSR0.
1054 DPRINTFN(5, ("PHY or CRC error flags 0x%08x\n",
1055 le32toh(desc
->flags
)));
1060 if ((le32toh(desc
->flags
) & RT2661_RX_CIPHER_MASK
) != 0) {
1066 * Try to allocate a new mbuf for this ring element and load it
1067 * before processing the current mbuf. If the ring element
1068 * cannot be loaded, drop the received packet and reuse the old
1069 * mbuf. In the unlikely case that the old mbuf can't be
1070 * reloaded either, explicitly panic.
1072 MGETHDR(mnew
, M_DONTWAIT
, MT_DATA
);
1078 MCLGET(mnew
, M_DONTWAIT
);
1079 if (!(mnew
->m_flags
& M_EXT
)) {
1085 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0,
1086 data
->map
->dm_mapsize
, BUS_DMASYNC_POSTREAD
);
1087 bus_dmamap_unload(sc
->sc_dmat
, data
->map
);
1089 error
= bus_dmamap_load(sc
->sc_dmat
, data
->map
,
1090 mtod(mnew
, void *), MCLBYTES
, NULL
, BUS_DMA_NOWAIT
);
1094 /* try to reload the old mbuf */
1095 error
= bus_dmamap_load(sc
->sc_dmat
, data
->map
,
1096 mtod(data
->m
, void *), MCLBYTES
, NULL
,
1099 /* very unlikely that it will fail... */
1100 panic("%s: could not load old rx mbuf",
1101 device_xname(&sc
->sc_dev
));
1103 /* physical address may have changed */
1104 desc
->physaddr
= htole32(data
->map
->dm_segs
->ds_addr
);
1110 * New mbuf successfully loaded, update Rx ring and continue
1115 desc
->physaddr
= htole32(data
->map
->dm_segs
->ds_addr
);
1118 m
->m_pkthdr
.rcvif
= ifp
;
1119 m
->m_pkthdr
.len
= m
->m_len
=
1120 (le32toh(desc
->flags
) >> 16) & 0xfff;
1123 if (sc
->sc_drvbpf
!= NULL
) {
1124 struct rt2661_rx_radiotap_header
*tap
= &sc
->sc_rxtap
;
1125 uint32_t tsf_lo
, tsf_hi
;
1127 /* get timestamp (low and high 32 bits) */
1128 tsf_hi
= RAL_READ(sc
, RT2661_TXRX_CSR13
);
1129 tsf_lo
= RAL_READ(sc
, RT2661_TXRX_CSR12
);
1132 htole64(((uint64_t)tsf_hi
<< 32) | tsf_lo
);
1134 tap
->wr_rate
= rt2661_rxrate(desc
);
1135 tap
->wr_chan_freq
= htole16(sc
->sc_curchan
->ic_freq
);
1136 tap
->wr_chan_flags
= htole16(sc
->sc_curchan
->ic_flags
);
1137 tap
->wr_antsignal
= desc
->rssi
;
1139 bpf_mtap2(sc
->sc_drvbpf
, tap
, sc
->sc_rxtap_len
, m
);
1143 wh
= mtod(m
, struct ieee80211_frame
*);
1144 ni
= ieee80211_find_rxnode(ic
,
1145 (struct ieee80211_frame_min
*)wh
);
1147 /* send the frame to the 802.11 layer */
1148 ieee80211_input(ic
, m
, ni
, desc
->rssi
, 0);
1151 * Keep track of the average RSSI using an Exponential Moving
1152 * Average (EMA) of 8 Wilder's days:
1153 * avg = (1 / N) x rssi + ((N - 1) / N) x avg
1155 rssi
= rt2661_get_rssi(sc
, desc
->rssi
);
1156 sc
->avg_rssi
= (rssi
+ 7 * sc
->avg_rssi
) / 8;
1158 /* node is no longer needed */
1159 ieee80211_free_node(ni
);
1161 skip
: desc
->flags
|= htole32(RT2661_RX_BUSY
);
1163 bus_dmamap_sync(sc
->sc_dmat
, sc
->rxq
.map
,
1164 sc
->rxq
.cur
* RT2661_RX_DESC_SIZE
, RT2661_RX_DESC_SIZE
,
1165 BUS_DMASYNC_PREWRITE
);
1167 DPRINTFN(16, ("rx intr idx=%u\n", sc
->rxq
.cur
));
1169 sc
->rxq
.cur
= (sc
->rxq
.cur
+ 1) % RT2661_RX_RING_COUNT
;
1173 * In HostAP mode, ieee80211_input() will enqueue packets in if_snd
1174 * without calling if_start().
1176 if (!IFQ_IS_EMPTY(&ifp
->if_snd
) && !(ifp
->if_flags
& IFF_OACTIVE
))
1181 * This function is called in HostAP or IBSS modes when it's time to send a
1182 * new beacon (every ni_intval milliseconds).
1185 rt2661_mcu_beacon_expire(struct rt2661_softc
*sc
)
1187 struct ieee80211com
*ic
= &sc
->sc_ic
;
1189 if (sc
->sc_flags
& RT2661_UPDATE_SLOT
) {
1190 sc
->sc_flags
&= ~RT2661_UPDATE_SLOT
;
1191 sc
->sc_flags
|= RT2661_SET_SLOTTIME
;
1192 } else if (sc
->sc_flags
& RT2661_SET_SLOTTIME
) {
1193 sc
->sc_flags
&= ~RT2661_SET_SLOTTIME
;
1194 rt2661_set_slottime(sc
);
1197 if (ic
->ic_curmode
== IEEE80211_MODE_11G
) {
1198 /* update ERP Information Element */
1199 RAL_WRITE_1(sc
, sc
->erp_csr
, ic
->ic_bss
->ni_erp
);
1200 RAL_RW_BARRIER_1(sc
, sc
->erp_csr
);
1203 DPRINTFN(15, ("beacon expired\n"));
1207 rt2661_mcu_wakeup(struct rt2661_softc
*sc
)
1209 RAL_WRITE(sc
, RT2661_MAC_CSR11
, 5 << 16);
1211 RAL_WRITE(sc
, RT2661_SOFT_RESET_CSR
, 0x7);
1212 RAL_WRITE(sc
, RT2661_IO_CNTL_CSR
, 0x18);
1213 RAL_WRITE(sc
, RT2661_PCI_USEC_CSR
, 0x20);
1215 /* send wakeup command to MCU */
1216 rt2661_tx_cmd(sc
, RT2661_MCU_CMD_WAKEUP
, 0);
1220 rt2661_mcu_cmd_intr(struct rt2661_softc
*sc
)
1222 RAL_READ(sc
, RT2661_M2H_CMD_DONE_CSR
);
1223 RAL_WRITE(sc
, RT2661_M2H_CMD_DONE_CSR
, 0xffffffff);
1227 rt2661_intr(void *arg
)
1229 struct rt2661_softc
*sc
= arg
;
1230 struct ifnet
*ifp
= &sc
->sc_if
;
1234 /* don't re-enable interrupts if we're shutting down */
1235 if (!(ifp
->if_flags
& IFF_RUNNING
)) {
1236 /* disable MAC and MCU interrupts */
1237 RAL_WRITE(sc
, RT2661_INT_MASK_CSR
, 0xffffff7f);
1238 RAL_WRITE(sc
, RT2661_MCU_INT_MASK_CSR
, 0xffffffff);
1243 r1
= RAL_READ(sc
, RT2661_INT_SOURCE_CSR
);
1244 r2
= RAL_READ(sc
, RT2661_MCU_INT_SOURCE_CSR
);
1246 if ((r1
& RT2661_INT_CSR_ALL
) == 0 &&
1247 (r2
& RT2661_MCU_INT_ALL
) == 0)
1250 RAL_WRITE(sc
, RT2661_INT_SOURCE_CSR
, r1
);
1251 RAL_WRITE(sc
, RT2661_MCU_INT_SOURCE_CSR
, r2
);
1255 if (r1
& RT2661_MGT_DONE
)
1256 rt2661_tx_dma_intr(sc
, &sc
->mgtq
);
1258 if (r1
& RT2661_RX_DONE
)
1261 if (r1
& RT2661_TX0_DMA_DONE
)
1262 rt2661_tx_dma_intr(sc
, &sc
->txq
[0]);
1264 if (r1
& RT2661_TX1_DMA_DONE
)
1265 rt2661_tx_dma_intr(sc
, &sc
->txq
[1]);
1267 if (r1
& RT2661_TX2_DMA_DONE
)
1268 rt2661_tx_dma_intr(sc
, &sc
->txq
[2]);
1270 if (r1
& RT2661_TX3_DMA_DONE
)
1271 rt2661_tx_dma_intr(sc
, &sc
->txq
[3]);
1273 if (r1
& RT2661_TX_DONE
)
1276 if (r2
& RT2661_MCU_CMD_DONE
)
1277 rt2661_mcu_cmd_intr(sc
);
1279 if (r2
& RT2661_MCU_BEACON_EXPIRE
)
1280 rt2661_mcu_beacon_expire(sc
);
1282 if (r2
& RT2661_MCU_WAKEUP
)
1283 rt2661_mcu_wakeup(sc
);
1289 /* quickly determine if a given rate is CCK or OFDM */
1290 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1292 #define RAL_ACK_SIZE 14 /* 10 + 4(FCS) */
1293 #define RAL_CTS_SIZE 14 /* 10 + 4(FCS) */
1296 * This function is only used by the Rx radiotap code. It returns the rate at
1297 * which a given frame was received.
1301 rt2661_rxrate(struct rt2661_rx_desc
*desc
)
1303 if (le32toh(desc
->flags
) & RT2661_RX_OFDM
) {
1304 /* reverse function of rt2661_plcp_signal */
1305 switch (desc
->rate
& 0xf) {
1306 case 0xb: return 12;
1307 case 0xf: return 18;
1308 case 0xa: return 24;
1309 case 0xe: return 36;
1310 case 0x9: return 48;
1311 case 0xd: return 72;
1312 case 0x8: return 96;
1313 case 0xc: return 108;
1316 if (desc
->rate
== 10)
1318 if (desc
->rate
== 20)
1320 if (desc
->rate
== 55)
1322 if (desc
->rate
== 110)
1325 return 2; /* should not get there */
1330 * Return the expected ack rate for a frame transmitted at rate `rate'.
1331 * XXX: this should depend on the destination node basic rate set.
1334 rt2661_ack_rate(struct ieee80211com
*ic
, int rate
)
1343 return (ic
->ic_curmode
== IEEE80211_MODE_11B
) ? 4 : rate
;
1359 /* default to 1Mbps */
1364 * Compute the duration (in us) needed to transmit `len' bytes at rate `rate'.
1365 * The function automatically determines the operating mode depending on the
1366 * given rate. `flags' indicates whether short preamble is in use or not.
1369 rt2661_txtime(int len
, int rate
, uint32_t flags
)
1373 if (RAL_RATE_IS_OFDM(rate
)) {
1374 /* IEEE Std 802.11g-2003, pp. 44 */
1375 txtime
= (8 + 4 * len
+ 3 + rate
- 1) / rate
;
1376 txtime
= 16 + 4 + 4 * txtime
+ 6;
1378 /* IEEE Std 802.11b-1999, pp. 28 */
1379 txtime
= (16 * len
+ rate
- 1) / rate
;
1380 if (rate
!= 2 && (flags
& IEEE80211_F_SHPREAMBLE
))
1389 rt2661_plcp_signal(int rate
)
1392 /* CCK rates (returned values are device-dependent) */
1395 case 11: return 0x2;
1396 case 22: return 0x3;
1398 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1399 case 12: return 0xb;
1400 case 18: return 0xf;
1401 case 24: return 0xa;
1402 case 36: return 0xe;
1403 case 48: return 0x9;
1404 case 72: return 0xd;
1405 case 96: return 0x8;
1406 case 108: return 0xc;
1408 /* unsupported rates (should not get there) */
1409 default: return 0xff;
1414 rt2661_setup_tx_desc(struct rt2661_softc
*sc
, struct rt2661_tx_desc
*desc
,
1415 uint32_t flags
, uint16_t xflags
, int len
, int rate
,
1416 const bus_dma_segment_t
*segs
, int nsegs
, int ac
)
1418 struct ieee80211com
*ic
= &sc
->sc_ic
;
1419 uint16_t plcp_length
;
1422 desc
->flags
= htole32(flags
);
1423 desc
->flags
|= htole32(len
<< 16);
1425 desc
->xflags
= htole16(xflags
);
1426 desc
->xflags
|= htole16(nsegs
<< 13);
1428 desc
->wme
= htole16(
1431 RT2661_LOGCWMIN(4) |
1432 RT2661_LOGCWMAX(10));
1435 * Remember in which queue this frame was sent. This field is driver
1436 * private data only. It will be made available by the NIC in STA_CSR4
1441 /* setup PLCP fields */
1442 desc
->plcp_signal
= rt2661_plcp_signal(rate
);
1443 desc
->plcp_service
= 4;
1445 len
+= IEEE80211_CRC_LEN
;
1446 if (RAL_RATE_IS_OFDM(rate
)) {
1447 desc
->flags
|= htole32(RT2661_TX_OFDM
);
1449 plcp_length
= len
& 0xfff;
1450 desc
->plcp_length_hi
= plcp_length
>> 6;
1451 desc
->plcp_length_lo
= plcp_length
& 0x3f;
1453 plcp_length
= (16 * len
+ rate
- 1) / rate
;
1455 remainder
= (16 * len
) % 22;
1456 if (remainder
!= 0 && remainder
< 7)
1457 desc
->plcp_service
|= RT2661_PLCP_LENGEXT
;
1459 desc
->plcp_length_hi
= plcp_length
>> 8;
1460 desc
->plcp_length_lo
= plcp_length
& 0xff;
1462 if (rate
!= 2 && (ic
->ic_flags
& IEEE80211_F_SHPREAMBLE
))
1463 desc
->plcp_signal
|= 0x08;
1466 /* RT2x61 supports scatter with up to 5 segments */
1467 for (i
= 0; i
< nsegs
; i
++) {
1468 desc
->addr
[i
] = htole32(segs
[i
].ds_addr
);
1469 desc
->len
[i
] = htole16(segs
[i
].ds_len
);
1472 desc
->flags
|= htole32(RT2661_TX_BUSY
| RT2661_TX_VALID
);
1476 rt2661_tx_mgt(struct rt2661_softc
*sc
, struct mbuf
*m0
,
1477 struct ieee80211_node
*ni
)
1479 struct ieee80211com
*ic
= &sc
->sc_ic
;
1480 struct rt2661_tx_desc
*desc
;
1481 struct rt2661_tx_data
*data
;
1482 struct ieee80211_frame
*wh
;
1487 desc
= &sc
->mgtq
.desc
[sc
->mgtq
.cur
];
1488 data
= &sc
->mgtq
.data
[sc
->mgtq
.cur
];
1490 /* send mgt frames at the lowest available rate */
1491 rate
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
) ? 12 : 2;
1493 wh
= mtod(m0
, struct ieee80211_frame
*);
1495 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
) {
1496 if (ieee80211_crypto_encap(ic
, ni
, m0
) == NULL
) {
1501 /* packet header may have moved, reset our local pointer */
1502 wh
= mtod(m0
, struct ieee80211_frame
*);
1505 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, data
->map
, m0
,
1508 aprint_error_dev(&sc
->sc_dev
, "could not map mbuf (error %d)\n",
1515 if (sc
->sc_drvbpf
!= NULL
) {
1516 struct rt2661_tx_radiotap_header
*tap
= &sc
->sc_txtap
;
1519 tap
->wt_rate
= rate
;
1520 tap
->wt_chan_freq
= htole16(sc
->sc_curchan
->ic_freq
);
1521 tap
->wt_chan_flags
= htole16(sc
->sc_curchan
->ic_flags
);
1523 bpf_mtap2(sc
->sc_drvbpf
, tap
, sc
->sc_txtap_len
, m0
);
1530 if (!IEEE80211_IS_MULTICAST(wh
->i_addr1
)) {
1531 flags
|= RT2661_TX_NEED_ACK
;
1533 dur
= rt2661_txtime(RAL_ACK_SIZE
, rate
, ic
->ic_flags
) +
1535 *(uint16_t *)wh
->i_dur
= htole16(dur
);
1537 /* tell hardware to set timestamp in probe responses */
1539 (IEEE80211_FC0_TYPE_MASK
| IEEE80211_FC0_SUBTYPE_MASK
)) ==
1540 (IEEE80211_FC0_TYPE_MGT
| IEEE80211_FC0_SUBTYPE_PROBE_RESP
))
1541 flags
|= RT2661_TX_TIMESTAMP
;
1544 rt2661_setup_tx_desc(sc
, desc
, flags
, 0 /* XXX HWSEQ */,
1545 m0
->m_pkthdr
.len
, rate
, data
->map
->dm_segs
, data
->map
->dm_nsegs
,
1548 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0, data
->map
->dm_mapsize
,
1549 BUS_DMASYNC_PREWRITE
);
1550 bus_dmamap_sync(sc
->sc_dmat
, sc
->mgtq
.map
,
1551 sc
->mgtq
.cur
* RT2661_TX_DESC_SIZE
, RT2661_TX_DESC_SIZE
,
1552 BUS_DMASYNC_PREWRITE
);
1554 DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n",
1555 m0
->m_pkthdr
.len
, sc
->mgtq
.cur
, rate
));
1559 sc
->mgtq
.cur
= (sc
->mgtq
.cur
+ 1) % RT2661_MGT_RING_COUNT
;
1560 RAL_WRITE(sc
, RT2661_TX_CNTL_CSR
, RT2661_KICK_MGT
);
1566 * Build a RTS control frame.
1568 static struct mbuf
*
1569 rt2661_get_rts(struct rt2661_softc
*sc
, struct ieee80211_frame
*wh
,
1572 struct ieee80211_frame_rts
*rts
;
1575 MGETHDR(m
, M_DONTWAIT
, MT_DATA
);
1577 sc
->sc_ic
.ic_stats
.is_tx_nobuf
++;
1578 aprint_error_dev(&sc
->sc_dev
, "could not allocate RTS frame\n");
1582 rts
= mtod(m
, struct ieee80211_frame_rts
*);
1584 rts
->i_fc
[0] = IEEE80211_FC0_VERSION_0
| IEEE80211_FC0_TYPE_CTL
|
1585 IEEE80211_FC0_SUBTYPE_RTS
;
1586 rts
->i_fc
[1] = IEEE80211_FC1_DIR_NODS
;
1587 *(uint16_t *)rts
->i_dur
= htole16(dur
);
1588 IEEE80211_ADDR_COPY(rts
->i_ra
, wh
->i_addr1
);
1589 IEEE80211_ADDR_COPY(rts
->i_ta
, wh
->i_addr2
);
1591 m
->m_pkthdr
.len
= m
->m_len
= sizeof (struct ieee80211_frame_rts
);
1597 rt2661_tx_data(struct rt2661_softc
*sc
, struct mbuf
*m0
,
1598 struct ieee80211_node
*ni
, int ac
)
1600 struct ieee80211com
*ic
= &sc
->sc_ic
;
1601 struct rt2661_tx_ring
*txq
= &sc
->txq
[ac
];
1602 struct rt2661_tx_desc
*desc
;
1603 struct rt2661_tx_data
*data
;
1604 struct ieee80211_frame
*wh
;
1605 struct ieee80211_key
*k
;
1609 int rate
, useprot
, error
, tid
;
1611 wh
= mtod(m0
, struct ieee80211_frame
*);
1613 if (ic
->ic_fixed_rate
!= IEEE80211_FIXED_RATE_NONE
) {
1614 rate
= ic
->ic_sup_rates
[ic
->ic_curmode
].
1615 rs_rates
[ic
->ic_fixed_rate
];
1617 rate
= ni
->ni_rates
.rs_rates
[ni
->ni_txrate
];
1618 rate
&= IEEE80211_RATE_VAL
;
1620 rate
= 2; /* XXX should not happen */
1622 if (wh
->i_fc
[1] & IEEE80211_FC1_WEP
) {
1623 k
= ieee80211_crypto_encap(ic
, ni
, m0
);
1629 /* packet header may have moved, reset our local pointer */
1630 wh
= mtod(m0
, struct ieee80211_frame
*);
1634 * Packet Bursting: backoff after ppb=8 frames to give other STAs a
1635 * chance to contend for the wireless medium.
1637 tid
= WME_AC_TO_TID(M_WME_GETAC(m0
));
1638 if (ic
->ic_opmode
== IEEE80211_M_STA
&& (ni
->ni_txseqs
[tid
] & 7))
1639 flags
|= RT2661_TX_IFS_SIFS
;
1642 * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange
1643 * for directed frames only when the length of the MPDU is greater
1644 * than the length threshold indicated by" ic_rtsthreshold.
1646 * IEEE Std 802.11-2003g, pp 13: "ERP STAs shall use protection
1647 * mechanism (such as RTS/CTS or CTS-to-self) for ERP-OFDM MPDUs of
1648 * type Data or an MMPDU".
1650 useprot
= !IEEE80211_IS_MULTICAST(wh
->i_addr1
) &&
1651 (m0
->m_pkthdr
.len
+ IEEE80211_CRC_LEN
> ic
->ic_rtsthreshold
||
1652 ((ic
->ic_flags
& IEEE80211_F_USEPROT
) && RAL_RATE_IS_OFDM(rate
)));
1655 int rtsrate
, ackrate
;
1657 rtsrate
= IEEE80211_IS_CHAN_5GHZ(ic
->ic_curchan
) ? 12 : 2;
1658 ackrate
= rt2661_ack_rate(ic
, rate
);
1660 dur
= rt2661_txtime(m0
->m_pkthdr
.len
+ 4, rate
, ic
->ic_flags
) +
1661 rt2661_txtime(RAL_CTS_SIZE
, rtsrate
, ic
->ic_flags
) +
1662 rt2661_txtime(RAL_ACK_SIZE
, ackrate
, ic
->ic_flags
) +
1665 m
= rt2661_get_rts(sc
, wh
, dur
);
1667 aprint_error_dev(&sc
->sc_dev
, "could not allocate RTS "
1673 desc
= &txq
->desc
[txq
->cur
];
1674 data
= &txq
->data
[txq
->cur
];
1676 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, data
->map
, m
,
1679 aprint_error_dev(&sc
->sc_dev
, "could not map mbuf (error %d)\n", error
);
1685 /* avoid multiple free() of the same node for each fragment */
1686 ieee80211_ref_node(ni
);
1691 rt2661_setup_tx_desc(sc
, desc
, RT2661_TX_NEED_ACK
|
1692 RT2661_TX_MORE_FRAG
, 0, m
->m_pkthdr
.len
, rtsrate
,
1693 data
->map
->dm_segs
, data
->map
->dm_nsegs
, ac
);
1695 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0,
1696 data
->map
->dm_mapsize
, BUS_DMASYNC_PREWRITE
);
1697 bus_dmamap_sync(sc
->sc_dmat
, txq
->map
,
1698 txq
->cur
* RT2661_TX_DESC_SIZE
, RT2661_TX_DESC_SIZE
,
1699 BUS_DMASYNC_PREWRITE
);
1702 txq
->cur
= (txq
->cur
+ 1) % RT2661_TX_RING_COUNT
;
1704 flags
|= RT2661_TX_LONG_RETRY
| RT2661_TX_IFS_SIFS
;
1707 data
= &txq
->data
[txq
->cur
];
1708 desc
= &txq
->desc
[txq
->cur
];
1710 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, data
->map
, m0
,
1712 if (error
!= 0 && error
!= EFBIG
) {
1713 aprint_error_dev(&sc
->sc_dev
, "could not map mbuf (error %d)\n",
1719 /* too many fragments, linearize */
1721 MGETHDR(mnew
, M_DONTWAIT
, MT_DATA
);
1727 M_COPY_PKTHDR(mnew
, m0
);
1728 if (m0
->m_pkthdr
.len
> MHLEN
) {
1729 MCLGET(mnew
, M_DONTWAIT
);
1730 if (!(mnew
->m_flags
& M_EXT
)) {
1737 m_copydata(m0
, 0, m0
->m_pkthdr
.len
, mtod(mnew
, void *));
1739 mnew
->m_len
= mnew
->m_pkthdr
.len
;
1742 error
= bus_dmamap_load_mbuf(sc
->sc_dmat
, data
->map
, m0
,
1745 aprint_error_dev(&sc
->sc_dev
, "could not map mbuf (error %d)\n", error
);
1750 /* packet header have moved, reset our local pointer */
1751 wh
= mtod(m0
, struct ieee80211_frame
*);
1755 if (sc
->sc_drvbpf
!= NULL
) {
1756 struct rt2661_tx_radiotap_header
*tap
= &sc
->sc_txtap
;
1759 tap
->wt_rate
= rate
;
1760 tap
->wt_chan_freq
= htole16(sc
->sc_curchan
->ic_freq
);
1761 tap
->wt_chan_flags
= htole16(sc
->sc_curchan
->ic_flags
);
1763 bpf_mtap2(sc
->sc_drvbpf
, tap
, sc
->sc_txtap_len
, m0
);
1770 if (!IEEE80211_IS_MULTICAST(wh
->i_addr1
)) {
1771 flags
|= RT2661_TX_NEED_ACK
;
1773 dur
= rt2661_txtime(RAL_ACK_SIZE
, rt2661_ack_rate(ic
, rate
),
1774 ic
->ic_flags
) + sc
->sifs
;
1775 *(uint16_t *)wh
->i_dur
= htole16(dur
);
1778 rt2661_setup_tx_desc(sc
, desc
, flags
, 0, m0
->m_pkthdr
.len
, rate
,
1779 data
->map
->dm_segs
, data
->map
->dm_nsegs
, ac
);
1781 bus_dmamap_sync(sc
->sc_dmat
, data
->map
, 0, data
->map
->dm_mapsize
,
1782 BUS_DMASYNC_PREWRITE
);
1783 bus_dmamap_sync(sc
->sc_dmat
, txq
->map
, txq
->cur
* RT2661_TX_DESC_SIZE
,
1784 RT2661_TX_DESC_SIZE
, BUS_DMASYNC_PREWRITE
);
1786 DPRINTFN(10, ("sending data frame len=%u idx=%u rate=%u\n",
1787 m0
->m_pkthdr
.len
, txq
->cur
, rate
));
1791 txq
->cur
= (txq
->cur
+ 1) % RT2661_TX_RING_COUNT
;
1792 RAL_WRITE(sc
, RT2661_TX_CNTL_CSR
, 1);
1798 rt2661_start(struct ifnet
*ifp
)
1800 struct rt2661_softc
*sc
= ifp
->if_softc
;
1801 struct ieee80211com
*ic
= &sc
->sc_ic
;
1803 struct ether_header
*eh
;
1804 struct ieee80211_node
*ni
= NULL
;
1807 * net80211 may still try to send management frames even if the
1808 * IFF_RUNNING flag is not set...
1810 if ((ifp
->if_flags
& (IFF_RUNNING
| IFF_OACTIVE
)) != IFF_RUNNING
)
1814 IF_POLL(&ic
->ic_mgtq
, m0
);
1816 if (sc
->mgtq
.queued
>= RT2661_MGT_RING_COUNT
) {
1817 ifp
->if_flags
|= IFF_OACTIVE
;
1820 IF_DEQUEUE(&ic
->ic_mgtq
, m0
);
1824 ni
= (struct ieee80211_node
*)m0
->m_pkthdr
.rcvif
;
1825 m0
->m_pkthdr
.rcvif
= NULL
;
1827 if (ic
->ic_rawbpf
!= NULL
)
1828 bpf_mtap(ic
->ic_rawbpf
, m0
);
1830 if (rt2661_tx_mgt(sc
, m0
, ni
) != 0)
1834 IF_POLL(&ifp
->if_snd
, m0
);
1835 if (m0
== NULL
|| ic
->ic_state
!= IEEE80211_S_RUN
)
1838 if (sc
->txq
[0].queued
>= RT2661_TX_RING_COUNT
- 1) {
1839 /* there is no place left in this ring */
1840 ifp
->if_flags
|= IFF_OACTIVE
;
1844 IFQ_DEQUEUE(&ifp
->if_snd
, m0
);
1846 if (m0
->m_len
< sizeof (struct ether_header
) &&
1847 !(m0
= m_pullup(m0
, sizeof (struct ether_header
))))
1850 eh
= mtod(m0
, struct ether_header
*);
1851 ni
= ieee80211_find_txnode(ic
, eh
->ether_dhost
);
1859 if (ifp
->if_bpf
!= NULL
)
1860 bpf_mtap(ifp
->if_bpf
, m0
);
1862 m0
= ieee80211_encap(ic
, m0
, ni
);
1864 ieee80211_free_node(ni
);
1869 if (ic
->ic_rawbpf
!= NULL
)
1870 bpf_mtap(ic
->ic_rawbpf
, m0
);
1872 if (rt2661_tx_data(sc
, m0
, ni
, 0) != 0) {
1874 ieee80211_free_node(ni
);
1880 sc
->sc_tx_timer
= 5;
1886 rt2661_watchdog(struct ifnet
*ifp
)
1888 struct rt2661_softc
*sc
= ifp
->if_softc
;
1892 if (sc
->sc_tx_timer
> 0) {
1893 if (--sc
->sc_tx_timer
== 0) {
1894 aprint_error_dev(&sc
->sc_dev
, "device timeout\n");
1902 ieee80211_watchdog(&sc
->sc_ic
);
1906 * This function allows for fast channel switching in monitor mode (used by
1907 * kismet). In IBSS mode, we must explicitly reset the interface to
1908 * generate a new beacon frame.
1911 rt2661_reset(struct ifnet
*ifp
)
1913 struct rt2661_softc
*sc
= ifp
->if_softc
;
1914 struct ieee80211com
*ic
= &sc
->sc_ic
;
1916 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
)
1919 rt2661_set_chan(sc
, ic
->ic_curchan
);
1925 rt2661_ioctl(struct ifnet
*ifp
, u_long cmd
, void *data
)
1927 struct rt2661_softc
*sc
= ifp
->if_softc
;
1928 struct ieee80211com
*ic
= &sc
->sc_ic
;
1935 if ((error
= ifioctl_common(ifp
, cmd
, data
)) != 0)
1937 if (ifp
->if_flags
& IFF_UP
) {
1938 if (ifp
->if_flags
& IFF_RUNNING
)
1939 rt2661_update_promisc(sc
);
1943 if (ifp
->if_flags
& IFF_RUNNING
)
1944 rt2661_stop(ifp
, 1);
1950 /* XXX no h/w multicast filter? --dyoung */
1951 if ((error
= ether_ioctl(ifp
, cmd
, data
)) == ENETRESET
)
1955 case SIOCS80211CHANNEL
:
1957 * This allows for fast channel switching in monitor mode
1958 * (used by kismet). In IBSS mode, we must explicitly reset
1959 * the interface to generate a new beacon frame.
1961 error
= ieee80211_ioctl(ic
, cmd
, data
);
1962 if (error
== ENETRESET
&&
1963 ic
->ic_opmode
== IEEE80211_M_MONITOR
) {
1964 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) ==
1965 (IFF_UP
| IFF_RUNNING
))
1966 rt2661_set_chan(sc
, ic
->ic_ibss_chan
);
1972 error
= ieee80211_ioctl(ic
, cmd
, data
);
1976 if (error
== ENETRESET
) {
1977 if ((ifp
->if_flags
& (IFF_UP
| IFF_RUNNING
)) ==
1978 (IFF_UP
| IFF_RUNNING
))
1989 rt2661_bbp_write(struct rt2661_softc
*sc
, uint8_t reg
, uint8_t val
)
1994 for (ntries
= 0; ntries
< 100; ntries
++) {
1995 if (!(RAL_READ(sc
, RT2661_PHY_CSR3
) & RT2661_BBP_BUSY
))
1999 if (ntries
== 100) {
2000 aprint_error_dev(&sc
->sc_dev
, "could not write to BBP\n");
2004 tmp
= RT2661_BBP_BUSY
| (reg
& 0x7f) << 8 | val
;
2005 RAL_WRITE(sc
, RT2661_PHY_CSR3
, tmp
);
2007 DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg
, val
));
2011 rt2661_bbp_read(struct rt2661_softc
*sc
, uint8_t reg
)
2016 for (ntries
= 0; ntries
< 100; ntries
++) {
2017 if (!(RAL_READ(sc
, RT2661_PHY_CSR3
) & RT2661_BBP_BUSY
))
2021 if (ntries
== 100) {
2022 aprint_error_dev(&sc
->sc_dev
, "could not read from BBP\n");
2026 val
= RT2661_BBP_BUSY
| RT2661_BBP_READ
| reg
<< 8;
2027 RAL_WRITE(sc
, RT2661_PHY_CSR3
, val
);
2029 for (ntries
= 0; ntries
< 100; ntries
++) {
2030 val
= RAL_READ(sc
, RT2661_PHY_CSR3
);
2031 if (!(val
& RT2661_BBP_BUSY
))
2036 aprint_error_dev(&sc
->sc_dev
, "could not read from BBP\n");
2041 rt2661_rf_write(struct rt2661_softc
*sc
, uint8_t reg
, uint32_t val
)
2046 for (ntries
= 0; ntries
< 100; ntries
++) {
2047 if (!(RAL_READ(sc
, RT2661_PHY_CSR4
) & RT2661_RF_BUSY
))
2051 if (ntries
== 100) {
2052 aprint_error_dev(&sc
->sc_dev
, "could not write to RF\n");
2055 tmp
= RT2661_RF_BUSY
| RT2661_RF_21BIT
| (val
& 0x1fffff) << 2 |
2057 RAL_WRITE(sc
, RT2661_PHY_CSR4
, tmp
);
2059 /* remember last written value in sc */
2060 sc
->rf_regs
[reg
] = val
;
2062 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg
& 3, val
& 0x1fffff));
2066 rt2661_tx_cmd(struct rt2661_softc
*sc
, uint8_t cmd
, uint16_t arg
)
2068 if (RAL_READ(sc
, RT2661_H2M_MAILBOX_CSR
) & RT2661_H2M_BUSY
)
2069 return EIO
; /* there is already a command pending */
2071 RAL_WRITE(sc
, RT2661_H2M_MAILBOX_CSR
,
2072 RT2661_H2M_BUSY
| RT2661_TOKEN_NO_INTR
<< 16 | arg
);
2074 RAL_WRITE(sc
, RT2661_HOST_CMD_CSR
, RT2661_KICK_CMD
| cmd
);
2080 rt2661_select_antenna(struct rt2661_softc
*sc
)
2082 uint8_t bbp4
, bbp77
;
2085 bbp4
= rt2661_bbp_read(sc
, 4);
2086 bbp77
= rt2661_bbp_read(sc
, 77);
2090 /* make sure Rx is disabled before switching antenna */
2091 tmp
= RAL_READ(sc
, RT2661_TXRX_CSR0
);
2092 RAL_WRITE(sc
, RT2661_TXRX_CSR0
, tmp
| RT2661_DISABLE_RX
);
2094 rt2661_bbp_write(sc
, 4, bbp4
);
2095 rt2661_bbp_write(sc
, 77, bbp77
);
2097 /* restore Rx filter */
2098 RAL_WRITE(sc
, RT2661_TXRX_CSR0
, tmp
);
2102 * Enable multi-rate retries for frames sent at OFDM rates.
2103 * In 802.11b/g mode, allow fallback to CCK rates.
2106 rt2661_enable_mrr(struct rt2661_softc
*sc
)
2108 struct ieee80211com
*ic
= &sc
->sc_ic
;
2111 tmp
= RAL_READ(sc
, RT2661_TXRX_CSR4
);
2113 tmp
&= ~RT2661_MRR_CCK_FALLBACK
;
2114 if (!IEEE80211_IS_CHAN_5GHZ(ic
->ic_bss
->ni_chan
))
2115 tmp
|= RT2661_MRR_CCK_FALLBACK
;
2116 tmp
|= RT2661_MRR_ENABLED
;
2118 RAL_WRITE(sc
, RT2661_TXRX_CSR4
, tmp
);
2122 rt2661_set_txpreamble(struct rt2661_softc
*sc
)
2126 tmp
= RAL_READ(sc
, RT2661_TXRX_CSR4
);
2128 tmp
&= ~RT2661_SHORT_PREAMBLE
;
2129 if (sc
->sc_ic
.ic_flags
& IEEE80211_F_SHPREAMBLE
)
2130 tmp
|= RT2661_SHORT_PREAMBLE
;
2132 RAL_WRITE(sc
, RT2661_TXRX_CSR4
, tmp
);
2136 rt2661_set_basicrates(struct rt2661_softc
*sc
,
2137 const struct ieee80211_rateset
*rs
)
2139 #define RV(r) ((r) & IEEE80211_RATE_VAL)
2144 for (i
= 0; i
< rs
->rs_nrates
; i
++) {
2145 rate
= rs
->rs_rates
[i
];
2147 if (!(rate
& IEEE80211_RATE_BASIC
))
2151 * Find h/w rate index. We know it exists because the rate
2152 * set has already been negotiated.
2154 for (j
= 0; rt2661_rateset_11g
.rs_rates
[j
] != RV(rate
); j
++);
2159 RAL_WRITE(sc
, RT2661_TXRX_CSR5
, mask
);
2161 DPRINTF(("Setting basic rate mask to 0x%x\n", mask
));
2166 * Reprogram MAC/BBP to switch to a new band. Values taken from the reference
2170 rt2661_select_band(struct rt2661_softc
*sc
, struct ieee80211_channel
*c
)
2172 uint8_t bbp17
, bbp35
, bbp96
, bbp97
, bbp98
, bbp104
;
2175 /* update all BBP registers that depend on the band */
2176 bbp17
= 0x20; bbp96
= 0x48; bbp104
= 0x2c;
2177 bbp35
= 0x50; bbp97
= 0x48; bbp98
= 0x48;
2178 if (IEEE80211_IS_CHAN_5GHZ(c
)) {
2179 bbp17
+= 0x08; bbp96
+= 0x10; bbp104
+= 0x0c;
2180 bbp35
+= 0x10; bbp97
+= 0x10; bbp98
+= 0x10;
2182 if ((IEEE80211_IS_CHAN_2GHZ(c
) && sc
->ext_2ghz_lna
) ||
2183 (IEEE80211_IS_CHAN_5GHZ(c
) && sc
->ext_5ghz_lna
)) {
2184 bbp17
+= 0x10; bbp96
+= 0x10; bbp104
+= 0x10;
2188 rt2661_bbp_write(sc
, 17, bbp17
);
2189 rt2661_bbp_write(sc
, 96, bbp96
);
2190 rt2661_bbp_write(sc
, 104, bbp104
);
2192 if ((IEEE80211_IS_CHAN_2GHZ(c
) && sc
->ext_2ghz_lna
) ||
2193 (IEEE80211_IS_CHAN_5GHZ(c
) && sc
->ext_5ghz_lna
)) {
2194 rt2661_bbp_write(sc
, 75, 0x80);
2195 rt2661_bbp_write(sc
, 86, 0x80);
2196 rt2661_bbp_write(sc
, 88, 0x80);
2199 rt2661_bbp_write(sc
, 35, bbp35
);
2200 rt2661_bbp_write(sc
, 97, bbp97
);
2201 rt2661_bbp_write(sc
, 98, bbp98
);
2203 tmp
= RAL_READ(sc
, RT2661_PHY_CSR0
);
2204 tmp
&= ~(RT2661_PA_PE_2GHZ
| RT2661_PA_PE_5GHZ
);
2205 if (IEEE80211_IS_CHAN_2GHZ(c
))
2206 tmp
|= RT2661_PA_PE_2GHZ
;
2208 tmp
|= RT2661_PA_PE_5GHZ
;
2209 RAL_WRITE(sc
, RT2661_PHY_CSR0
, tmp
);
2211 /* 802.11a uses a 16 microseconds short interframe space */
2212 sc
->sifs
= IEEE80211_IS_CHAN_5GHZ(c
) ? 16 : 10;
2216 rt2661_set_chan(struct rt2661_softc
*sc
, struct ieee80211_channel
*c
)
2218 struct ieee80211com
*ic
= &sc
->sc_ic
;
2219 const struct rfprog
*rfprog
;
2220 uint8_t bbp3
, bbp94
= RT2661_BBPR94_DEFAULT
;
2224 chan
= ieee80211_chan2ieee(ic
, c
);
2225 if (chan
== 0 || chan
== IEEE80211_CHAN_ANY
)
2228 /* select the appropriate RF settings based on what EEPROM says */
2229 rfprog
= (sc
->rfprog
== 0) ? rt2661_rf5225_1
: rt2661_rf5225_2
;
2231 /* find the settings for this channel (we know it exists) */
2232 for (i
= 0; rfprog
[i
].chan
!= chan
; i
++);
2234 power
= sc
->txpow
[i
];
2238 } else if (power
> 31) {
2239 bbp94
+= power
- 31;
2244 * If we've yet to select a channel, or we are switching from the
2245 * 2GHz band to the 5GHz band or vice-versa, BBP registers need to
2248 if (sc
->sc_curchan
== NULL
|| c
->ic_flags
!= sc
->sc_curchan
->ic_flags
) {
2249 rt2661_select_band(sc
, c
);
2250 rt2661_select_antenna(sc
);
2254 rt2661_rf_write(sc
, RAL_RF1
, rfprog
[i
].r1
);
2255 rt2661_rf_write(sc
, RAL_RF2
, rfprog
[i
].r2
);
2256 rt2661_rf_write(sc
, RAL_RF3
, rfprog
[i
].r3
| power
<< 7);
2257 rt2661_rf_write(sc
, RAL_RF4
, rfprog
[i
].r4
| sc
->rffreq
<< 10);
2261 rt2661_rf_write(sc
, RAL_RF1
, rfprog
[i
].r1
);
2262 rt2661_rf_write(sc
, RAL_RF2
, rfprog
[i
].r2
);
2263 rt2661_rf_write(sc
, RAL_RF3
, rfprog
[i
].r3
| power
<< 7 | 1);
2264 rt2661_rf_write(sc
, RAL_RF4
, rfprog
[i
].r4
| sc
->rffreq
<< 10);
2268 rt2661_rf_write(sc
, RAL_RF1
, rfprog
[i
].r1
);
2269 rt2661_rf_write(sc
, RAL_RF2
, rfprog
[i
].r2
);
2270 rt2661_rf_write(sc
, RAL_RF3
, rfprog
[i
].r3
| power
<< 7);
2271 rt2661_rf_write(sc
, RAL_RF4
, rfprog
[i
].r4
| sc
->rffreq
<< 10);
2273 /* enable smart mode for MIMO-capable RFs */
2274 bbp3
= rt2661_bbp_read(sc
, 3);
2276 bbp3
&= ~RT2661_SMART_MODE
;
2277 if (sc
->rf_rev
== RT2661_RF_5325
|| sc
->rf_rev
== RT2661_RF_2529
)
2278 bbp3
|= RT2661_SMART_MODE
;
2280 rt2661_bbp_write(sc
, 3, bbp3
);
2282 if (bbp94
!= RT2661_BBPR94_DEFAULT
)
2283 rt2661_bbp_write(sc
, 94, bbp94
);
2285 /* 5GHz radio needs a 1ms delay here */
2286 if (IEEE80211_IS_CHAN_5GHZ(c
))
2291 rt2661_set_bssid(struct rt2661_softc
*sc
, const uint8_t *bssid
)
2295 tmp
= bssid
[0] | bssid
[1] << 8 | bssid
[2] << 16 | bssid
[3] << 24;
2296 RAL_WRITE(sc
, RT2661_MAC_CSR4
, tmp
);
2298 tmp
= bssid
[4] | bssid
[5] << 8 | RT2661_ONE_BSSID
<< 16;
2299 RAL_WRITE(sc
, RT2661_MAC_CSR5
, tmp
);
2303 rt2661_set_macaddr(struct rt2661_softc
*sc
, const uint8_t *addr
)
2307 tmp
= addr
[0] | addr
[1] << 8 | addr
[2] << 16 | addr
[3] << 24;
2308 RAL_WRITE(sc
, RT2661_MAC_CSR2
, tmp
);
2310 tmp
= addr
[4] | addr
[5] << 8 | 0xff << 16;
2311 RAL_WRITE(sc
, RT2661_MAC_CSR3
, tmp
);
2315 rt2661_update_promisc(struct rt2661_softc
*sc
)
2317 struct ifnet
*ifp
= sc
->sc_ic
.ic_ifp
;
2320 tmp
= RAL_READ(sc
, RT2661_TXRX_CSR0
);
2322 tmp
&= ~RT2661_DROP_NOT_TO_ME
;
2323 if (!(ifp
->if_flags
& IFF_PROMISC
))
2324 tmp
|= RT2661_DROP_NOT_TO_ME
;
2326 RAL_WRITE(sc
, RT2661_TXRX_CSR0
, tmp
);
2328 DPRINTF(("%s promiscuous mode\n", (ifp
->if_flags
& IFF_PROMISC
) ?
2329 "entering" : "leaving"));
2334 * Update QoS (802.11e) settings for each h/w Tx ring.
2337 rt2661_wme_update(struct ieee80211com
*ic
)
2339 struct rt2661_softc
*sc
= ic
->ic_ifp
->if_softc
;
2340 const struct wmeParams
*wmep
;
2342 wmep
= ic
->ic_wme
.wme_chanParams
.cap_wmeParams
;
2344 /* XXX: not sure about shifts. */
2345 /* XXX: the reference driver plays with AC_VI settings too. */
2348 RAL_WRITE(sc
, RT2661_AC_TXOP_CSR0
,
2349 wmep
[WME_AC_BE
].wmep_txopLimit
<< 16 |
2350 wmep
[WME_AC_BK
].wmep_txopLimit
);
2351 RAL_WRITE(sc
, RT2661_AC_TXOP_CSR1
,
2352 wmep
[WME_AC_VI
].wmep_txopLimit
<< 16 |
2353 wmep
[WME_AC_VO
].wmep_txopLimit
);
2356 RAL_WRITE(sc
, RT2661_CWMIN_CSR
,
2357 wmep
[WME_AC_BE
].wmep_logcwmin
<< 12 |
2358 wmep
[WME_AC_BK
].wmep_logcwmin
<< 8 |
2359 wmep
[WME_AC_VI
].wmep_logcwmin
<< 4 |
2360 wmep
[WME_AC_VO
].wmep_logcwmin
);
2363 RAL_WRITE(sc
, RT2661_CWMAX_CSR
,
2364 wmep
[WME_AC_BE
].wmep_logcwmax
<< 12 |
2365 wmep
[WME_AC_BK
].wmep_logcwmax
<< 8 |
2366 wmep
[WME_AC_VI
].wmep_logcwmax
<< 4 |
2367 wmep
[WME_AC_VO
].wmep_logcwmax
);
2370 RAL_WRITE(sc
, RT2661_AIFSN_CSR
,
2371 wmep
[WME_AC_BE
].wmep_aifsn
<< 12 |
2372 wmep
[WME_AC_BK
].wmep_aifsn
<< 8 |
2373 wmep
[WME_AC_VI
].wmep_aifsn
<< 4 |
2374 wmep
[WME_AC_VO
].wmep_aifsn
);
2381 rt2661_updateslot(struct ifnet
*ifp
)
2383 struct rt2661_softc
*sc
= ifp
->if_softc
;
2384 struct ieee80211com
*ic
= &sc
->sc_ic
;
2386 if (ic
->ic_opmode
== IEEE80211_M_HOSTAP
) {
2388 * In HostAP mode, we defer setting of new slot time until
2389 * updated ERP Information Element has propagated to all
2392 sc
->sc_flags
|= RT2661_UPDATE_SLOT
;
2394 rt2661_set_slottime(sc
);
2398 rt2661_set_slottime(struct rt2661_softc
*sc
)
2400 struct ieee80211com
*ic
= &sc
->sc_ic
;
2404 slottime
= (ic
->ic_flags
& IEEE80211_F_SHSLOT
) ? 9 : 20;
2406 tmp
= RAL_READ(sc
, RT2661_MAC_CSR9
);
2407 tmp
= (tmp
& ~0xff) | slottime
;
2408 RAL_WRITE(sc
, RT2661_MAC_CSR9
, tmp
);
2410 DPRINTF(("setting slot time to %uus\n", slottime
));
2414 rt2661_get_rf(int rev
)
2417 case RT2661_RF_5225
: return "RT5225";
2418 case RT2661_RF_5325
: return "RT5325 (MIMO XR)";
2419 case RT2661_RF_2527
: return "RT2527";
2420 case RT2661_RF_2529
: return "RT2529 (MIMO XR)";
2421 default: return "unknown";
2426 rt2661_read_eeprom(struct rt2661_softc
*sc
)
2428 struct ieee80211com
*ic
= &sc
->sc_ic
;
2432 /* read MAC address */
2433 val
= rt2661_eeprom_read(sc
, RT2661_EEPROM_MAC01
);
2434 ic
->ic_myaddr
[0] = val
& 0xff;
2435 ic
->ic_myaddr
[1] = val
>> 8;
2437 val
= rt2661_eeprom_read(sc
, RT2661_EEPROM_MAC23
);
2438 ic
->ic_myaddr
[2] = val
& 0xff;
2439 ic
->ic_myaddr
[3] = val
>> 8;
2441 val
= rt2661_eeprom_read(sc
, RT2661_EEPROM_MAC45
);
2442 ic
->ic_myaddr
[4] = val
& 0xff;
2443 ic
->ic_myaddr
[5] = val
>> 8;
2445 val
= rt2661_eeprom_read(sc
, RT2661_EEPROM_ANTENNA
);
2446 /* XXX: test if different from 0xffff? */
2447 sc
->rf_rev
= (val
>> 11) & 0x1f;
2448 sc
->hw_radio
= (val
>> 10) & 0x1;
2449 sc
->rx_ant
= (val
>> 4) & 0x3;
2450 sc
->tx_ant
= (val
>> 2) & 0x3;
2451 sc
->nb_ant
= val
& 0x3;
2453 DPRINTF(("RF revision=%d\n", sc
->rf_rev
));
2455 val
= rt2661_eeprom_read(sc
, RT2661_EEPROM_CONFIG2
);
2456 sc
->ext_5ghz_lna
= (val
>> 6) & 0x1;
2457 sc
->ext_2ghz_lna
= (val
>> 4) & 0x1;
2459 DPRINTF(("External 2GHz LNA=%d\nExternal 5GHz LNA=%d\n",
2460 sc
->ext_2ghz_lna
, sc
->ext_5ghz_lna
));
2462 val
= rt2661_eeprom_read(sc
, RT2661_EEPROM_RSSI_2GHZ_OFFSET
);
2463 if ((val
& 0xff) != 0xff)
2464 sc
->rssi_2ghz_corr
= (int8_t)(val
& 0xff); /* signed */
2466 val
= rt2661_eeprom_read(sc
, RT2661_EEPROM_RSSI_5GHZ_OFFSET
);
2467 if ((val
& 0xff) != 0xff)
2468 sc
->rssi_5ghz_corr
= (int8_t)(val
& 0xff); /* signed */
2470 /* adjust RSSI correction for external low-noise amplifier */
2471 if (sc
->ext_2ghz_lna
)
2472 sc
->rssi_2ghz_corr
-= 14;
2473 if (sc
->ext_5ghz_lna
)
2474 sc
->rssi_5ghz_corr
-= 14;
2476 DPRINTF(("RSSI 2GHz corr=%d\nRSSI 5GHz corr=%d\n",
2477 sc
->rssi_2ghz_corr
, sc
->rssi_5ghz_corr
));
2479 val
= rt2661_eeprom_read(sc
, RT2661_EEPROM_FREQ_OFFSET
);
2480 if ((val
>> 8) != 0xff)
2481 sc
->rfprog
= (val
>> 8) & 0x3;
2482 if ((val
& 0xff) != 0xff)
2483 sc
->rffreq
= val
& 0xff;
2485 DPRINTF(("RF prog=%d\nRF freq=%d\n", sc
->rfprog
, sc
->rffreq
));
2487 /* read Tx power for all a/b/g channels */
2488 for (i
= 0; i
< 19; i
++) {
2489 val
= rt2661_eeprom_read(sc
, RT2661_EEPROM_TXPOWER
+ i
);
2490 sc
->txpow
[i
* 2] = (int8_t)(val
>> 8); /* signed */
2491 DPRINTF(("Channel=%d Tx power=%d\n",
2492 rt2661_rf5225_1
[i
* 2].chan
, sc
->txpow
[i
* 2]));
2493 sc
->txpow
[i
* 2 + 1] = (int8_t)(val
& 0xff); /* signed */
2494 DPRINTF(("Channel=%d Tx power=%d\n",
2495 rt2661_rf5225_1
[i
* 2 + 1].chan
, sc
->txpow
[i
* 2 + 1]));
2498 /* read vendor-specific BBP values */
2499 for (i
= 0; i
< 16; i
++) {
2500 val
= rt2661_eeprom_read(sc
, RT2661_EEPROM_BBP_BASE
+ i
);
2501 if (val
== 0 || val
== 0xffff)
2502 continue; /* skip invalid entries */
2503 sc
->bbp_prom
[i
].reg
= val
>> 8;
2504 sc
->bbp_prom
[i
].val
= val
& 0xff;
2505 DPRINTF(("BBP R%d=%02x\n", sc
->bbp_prom
[i
].reg
,
2506 sc
->bbp_prom
[i
].val
));
2511 rt2661_bbp_init(struct rt2661_softc
*sc
)
2513 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2517 /* wait for BBP to be ready */
2518 for (ntries
= 0; ntries
< 100; ntries
++) {
2519 val
= rt2661_bbp_read(sc
, 0);
2520 if (val
!= 0 && val
!= 0xff)
2524 if (ntries
== 100) {
2525 aprint_error_dev(&sc
->sc_dev
, "timeout waiting for BBP\n");
2529 /* initialize BBP registers to default values */
2530 for (i
= 0; i
< N(rt2661_def_bbp
); i
++) {
2531 rt2661_bbp_write(sc
, rt2661_def_bbp
[i
].reg
,
2532 rt2661_def_bbp
[i
].val
);
2535 /* write vendor-specific BBP values (from EEPROM) */
2536 for (i
= 0; i
< 16; i
++) {
2537 if (sc
->bbp_prom
[i
].reg
== 0)
2539 rt2661_bbp_write(sc
, sc
->bbp_prom
[i
].reg
, sc
->bbp_prom
[i
].val
);
2547 rt2661_init(struct ifnet
*ifp
)
2549 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2550 struct rt2661_softc
*sc
= ifp
->if_softc
;
2551 struct ieee80211com
*ic
= &sc
->sc_ic
;
2552 const char *name
= NULL
; /* make lint happy */
2555 uint32_t tmp
, star
[3];
2557 firmware_handle_t fh
;
2559 /* for CardBus, power on the socket */
2560 if (!(sc
->sc_flags
& RT2661_ENABLED
)) {
2561 if (sc
->sc_enable
!= NULL
&& (*sc
->sc_enable
)(sc
) != 0) {
2562 aprint_error_dev(&sc
->sc_dev
, "could not enable device\n");
2565 sc
->sc_flags
|= RT2661_ENABLED
;
2568 rt2661_stop(ifp
, 0);
2570 if (!(sc
->sc_flags
& RT2661_FWLOADED
)) {
2571 switch (sc
->sc_id
) {
2572 case PCI_PRODUCT_RALINK_RT2561
:
2573 name
= "ral-rt2561";
2575 case PCI_PRODUCT_RALINK_RT2561S
:
2576 name
= "ral-rt2561s";
2578 case PCI_PRODUCT_RALINK_RT2661
:
2579 name
= "ral-rt2661";
2583 if (firmware_open("ral", name
, &fh
) != 0) {
2584 aprint_error_dev(&sc
->sc_dev
, "could not open microcode %s\n", name
);
2585 rt2661_stop(ifp
, 1);
2589 size
= firmware_get_size(fh
);
2590 if (!(ucode
= firmware_malloc(size
))) {
2591 aprint_error_dev(&sc
->sc_dev
, "could not alloc microcode memory\n");
2593 rt2661_stop(ifp
, 1);
2597 if (firmware_read(fh
, 0, ucode
, size
) != 0) {
2598 aprint_error_dev(&sc
->sc_dev
, "could not read microcode %s\n", name
);
2599 firmware_free(ucode
, 0);
2601 rt2661_stop(ifp
, 1);
2605 if (rt2661_load_microcode(sc
, ucode
, size
) != 0) {
2606 aprint_error_dev(&sc
->sc_dev
, "could not load 8051 microcode\n");
2607 firmware_free(ucode
, 0);
2609 rt2661_stop(ifp
, 1);
2613 firmware_free(ucode
, 0);
2615 sc
->sc_flags
|= RT2661_FWLOADED
;
2618 /* initialize Tx rings */
2619 RAL_WRITE(sc
, RT2661_AC1_BASE_CSR
, sc
->txq
[1].physaddr
);
2620 RAL_WRITE(sc
, RT2661_AC0_BASE_CSR
, sc
->txq
[0].physaddr
);
2621 RAL_WRITE(sc
, RT2661_AC2_BASE_CSR
, sc
->txq
[2].physaddr
);
2622 RAL_WRITE(sc
, RT2661_AC3_BASE_CSR
, sc
->txq
[3].physaddr
);
2624 /* initialize Mgt ring */
2625 RAL_WRITE(sc
, RT2661_MGT_BASE_CSR
, sc
->mgtq
.physaddr
);
2627 /* initialize Rx ring */
2628 RAL_WRITE(sc
, RT2661_RX_BASE_CSR
, sc
->rxq
.physaddr
);
2630 /* initialize Tx rings sizes */
2631 RAL_WRITE(sc
, RT2661_TX_RING_CSR0
,
2632 RT2661_TX_RING_COUNT
<< 24 |
2633 RT2661_TX_RING_COUNT
<< 16 |
2634 RT2661_TX_RING_COUNT
<< 8 |
2635 RT2661_TX_RING_COUNT
);
2637 RAL_WRITE(sc
, RT2661_TX_RING_CSR1
,
2638 RT2661_TX_DESC_WSIZE
<< 16 |
2639 RT2661_TX_RING_COUNT
<< 8 | /* XXX: HCCA ring unused */
2640 RT2661_MGT_RING_COUNT
);
2642 /* initialize Rx rings */
2643 RAL_WRITE(sc
, RT2661_RX_RING_CSR
,
2644 RT2661_RX_DESC_BACK
<< 16 |
2645 RT2661_RX_DESC_WSIZE
<< 8 |
2646 RT2661_RX_RING_COUNT
);
2648 /* XXX: some magic here */
2649 RAL_WRITE(sc
, RT2661_TX_DMA_DST_CSR
, 0xaa);
2651 /* load base addresses of all 5 Tx rings (4 data + 1 mgt) */
2652 RAL_WRITE(sc
, RT2661_LOAD_TX_RING_CSR
, 0x1f);
2654 /* load base address of Rx ring */
2655 RAL_WRITE(sc
, RT2661_RX_CNTL_CSR
, 2);
2657 /* initialize MAC registers to default values */
2658 for (i
= 0; i
< N(rt2661_def_mac
); i
++)
2659 RAL_WRITE(sc
, rt2661_def_mac
[i
].reg
, rt2661_def_mac
[i
].val
);
2661 IEEE80211_ADDR_COPY(ic
->ic_myaddr
, CLLADDR(ifp
->if_sadl
));
2662 rt2661_set_macaddr(sc
, ic
->ic_myaddr
);
2664 /* set host ready */
2665 RAL_WRITE(sc
, RT2661_MAC_CSR1
, 3);
2666 RAL_WRITE(sc
, RT2661_MAC_CSR1
, 0);
2668 /* wait for BBP/RF to wakeup */
2669 for (ntries
= 0; ntries
< 1000; ntries
++) {
2670 if (RAL_READ(sc
, RT2661_MAC_CSR12
) & 8)
2674 if (ntries
== 1000) {
2675 printf("timeout waiting for BBP/RF to wakeup\n");
2676 rt2661_stop(ifp
, 1);
2680 if (rt2661_bbp_init(sc
) != 0) {
2681 rt2661_stop(ifp
, 1);
2685 /* select default channel */
2686 sc
->sc_curchan
= ic
->ic_curchan
;
2687 rt2661_select_band(sc
, sc
->sc_curchan
);
2688 rt2661_select_antenna(sc
);
2689 rt2661_set_chan(sc
, sc
->sc_curchan
);
2691 /* update Rx filter */
2692 tmp
= RAL_READ(sc
, RT2661_TXRX_CSR0
) & 0xffff;
2694 tmp
|= RT2661_DROP_PHY_ERROR
| RT2661_DROP_CRC_ERROR
;
2695 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
2696 tmp
|= RT2661_DROP_CTL
| RT2661_DROP_VER_ERROR
|
2698 if (ic
->ic_opmode
!= IEEE80211_M_HOSTAP
)
2699 tmp
|= RT2661_DROP_TODS
;
2700 if (!(ifp
->if_flags
& IFF_PROMISC
))
2701 tmp
|= RT2661_DROP_NOT_TO_ME
;
2704 RAL_WRITE(sc
, RT2661_TXRX_CSR0
, tmp
);
2706 /* clear STA registers */
2707 RAL_READ_REGION_4(sc
, RT2661_STA_CSR0
, star
, N(star
));
2709 /* initialize ASIC */
2710 RAL_WRITE(sc
, RT2661_MAC_CSR1
, 4);
2712 /* clear any pending interrupt */
2713 RAL_WRITE(sc
, RT2661_INT_SOURCE_CSR
, 0xffffffff);
2715 /* enable interrupts */
2716 RAL_WRITE(sc
, RT2661_INT_MASK_CSR
, 0x0000ff10);
2717 RAL_WRITE(sc
, RT2661_MCU_INT_MASK_CSR
, 0);
2720 RAL_WRITE(sc
, RT2661_RX_CNTL_CSR
, 1);
2722 ifp
->if_flags
&= ~IFF_OACTIVE
;
2723 ifp
->if_flags
|= IFF_RUNNING
;
2725 if (ic
->ic_opmode
!= IEEE80211_M_MONITOR
) {
2726 if (ic
->ic_roaming
!= IEEE80211_ROAMING_MANUAL
)
2727 ieee80211_new_state(ic
, IEEE80211_S_SCAN
, -1);
2729 ieee80211_new_state(ic
, IEEE80211_S_RUN
, -1);
2736 rt2661_stop(struct ifnet
*ifp
, int disable
)
2738 struct rt2661_softc
*sc
= ifp
->if_softc
;
2739 struct ieee80211com
*ic
= &sc
->sc_ic
;
2742 sc
->sc_tx_timer
= 0;
2744 ifp
->if_flags
&= ~(IFF_RUNNING
| IFF_OACTIVE
);
2746 ieee80211_new_state(ic
, IEEE80211_S_INIT
, -1); /* free all nodes */
2748 /* abort Tx (for all 5 Tx rings) */
2749 RAL_WRITE(sc
, RT2661_TX_CNTL_CSR
, 0x1f << 16);
2751 /* disable Rx (value remains after reset!) */
2752 tmp
= RAL_READ(sc
, RT2661_TXRX_CSR0
);
2753 RAL_WRITE(sc
, RT2661_TXRX_CSR0
, tmp
| RT2661_DISABLE_RX
);
2756 RAL_WRITE(sc
, RT2661_MAC_CSR1
, 3);
2757 RAL_WRITE(sc
, RT2661_MAC_CSR1
, 0);
2759 /* disable interrupts */
2760 RAL_WRITE(sc
, RT2661_INT_MASK_CSR
, 0xffffff7f);
2761 RAL_WRITE(sc
, RT2661_MCU_INT_MASK_CSR
, 0xffffffff);
2763 /* clear any pending interrupt */
2764 RAL_WRITE(sc
, RT2661_INT_SOURCE_CSR
, 0xffffffff);
2765 RAL_WRITE(sc
, RT2661_MCU_INT_SOURCE_CSR
, 0xffffffff);
2767 /* reset Tx and Rx rings */
2768 rt2661_reset_tx_ring(sc
, &sc
->txq
[0]);
2769 rt2661_reset_tx_ring(sc
, &sc
->txq
[1]);
2770 rt2661_reset_tx_ring(sc
, &sc
->txq
[2]);
2771 rt2661_reset_tx_ring(sc
, &sc
->txq
[3]);
2772 rt2661_reset_tx_ring(sc
, &sc
->mgtq
);
2773 rt2661_reset_rx_ring(sc
, &sc
->rxq
);
2775 /* for CardBus, power down the socket */
2776 if (disable
&& sc
->sc_disable
!= NULL
) {
2777 if (sc
->sc_flags
& RT2661_ENABLED
) {
2778 (*sc
->sc_disable
)(sc
);
2779 sc
->sc_flags
&= ~(RT2661_ENABLED
| RT2661_FWLOADED
);
2785 rt2661_load_microcode(struct rt2661_softc
*sc
, const uint8_t *ucode
, int size
)
2790 RAL_WRITE(sc
, RT2661_MCU_CNTL_CSR
, RT2661_MCU_RESET
);
2792 /* cancel any pending Host to MCU command */
2793 RAL_WRITE(sc
, RT2661_H2M_MAILBOX_CSR
, 0);
2794 RAL_WRITE(sc
, RT2661_M2H_CMD_DONE_CSR
, 0xffffffff);
2795 RAL_WRITE(sc
, RT2661_HOST_CMD_CSR
, 0);
2797 /* write 8051's microcode */
2798 RAL_WRITE(sc
, RT2661_MCU_CNTL_CSR
, RT2661_MCU_RESET
| RT2661_MCU_SEL
);
2799 RAL_WRITE_REGION_1(sc
, RT2661_MCU_CODE_BASE
, ucode
, size
);
2800 RAL_WRITE(sc
, RT2661_MCU_CNTL_CSR
, RT2661_MCU_RESET
);
2802 /* kick 8051's ass */
2803 RAL_WRITE(sc
, RT2661_MCU_CNTL_CSR
, 0);
2805 /* wait for 8051 to initialize */
2806 for (ntries
= 0; ntries
< 500; ntries
++) {
2807 if (RAL_READ(sc
, RT2661_MCU_CNTL_CSR
) & RT2661_MCU_READY
)
2811 if (ntries
== 500) {
2812 printf("timeout waiting for MCU to initialize\n");
2819 * Dynamically tune Rx sensitivity (BBP register 17) based on average RSSI and
2820 * false CCA count. This function is called periodically (every seconds) when
2821 * in the RUN state. Values taken from the reference driver.
2824 rt2661_rx_tune(struct rt2661_softc
*sc
)
2831 * Tuning range depends on operating band and on the presence of an
2832 * external low-noise amplifier.
2835 if (IEEE80211_IS_CHAN_5GHZ(sc
->sc_curchan
))
2837 if ((IEEE80211_IS_CHAN_2GHZ(sc
->sc_curchan
) && sc
->ext_2ghz_lna
) ||
2838 (IEEE80211_IS_CHAN_5GHZ(sc
->sc_curchan
) && sc
->ext_5ghz_lna
))
2843 /* retrieve false CCA count since last call (clear on read) */
2844 cca
= RAL_READ(sc
, RT2661_STA_CSR1
) & 0xffff;
2846 DPRINTFN(2, ("RSSI=%ddBm false CCA=%d\n", dbm
, cca
));
2849 /* very bad RSSI, tune using false CCA count */
2850 bbp17
= sc
->bbp17
; /* current value */
2852 hi
-= 2 * (-74 - dbm
);
2859 bbp17
= min(bbp17
+ 1, hi
);
2861 bbp17
= max(bbp17
- 1, lo
);
2863 } else if (dbm
< -66) {
2865 } else if (dbm
< -58) {
2867 } else if (dbm
< -35) {
2869 } else { /* very good RSSI >= -35dBm */
2870 bbp17
= 0x60; /* very low sensitivity */
2873 if (bbp17
!= sc
->bbp17
) {
2874 DPRINTF(("BBP17 %x->%x\n", sc
->bbp17
, bbp17
));
2875 rt2661_bbp_write(sc
, 17, bbp17
);
2882 * Enter/Leave radar detection mode.
2883 * This is for 802.11h additional regulatory domains.
2886 rt2661_radar_start(struct rt2661_softc
*sc
)
2891 tmp
= RAL_READ(sc
, RT2661_TXRX_CSR0
);
2892 RAL_WRITE(sc
, RT2661_TXRX_CSR0
, tmp
| RT2661_DISABLE_RX
);
2894 rt2661_bbp_write(sc
, 82, 0x20);
2895 rt2661_bbp_write(sc
, 83, 0x00);
2896 rt2661_bbp_write(sc
, 84, 0x40);
2898 /* save current BBP registers values */
2899 sc
->bbp18
= rt2661_bbp_read(sc
, 18);
2900 sc
->bbp21
= rt2661_bbp_read(sc
, 21);
2901 sc
->bbp22
= rt2661_bbp_read(sc
, 22);
2902 sc
->bbp16
= rt2661_bbp_read(sc
, 16);
2903 sc
->bbp17
= rt2661_bbp_read(sc
, 17);
2904 sc
->bbp64
= rt2661_bbp_read(sc
, 64);
2906 rt2661_bbp_write(sc
, 18, 0xff);
2907 rt2661_bbp_write(sc
, 21, 0x3f);
2908 rt2661_bbp_write(sc
, 22, 0x3f);
2909 rt2661_bbp_write(sc
, 16, 0xbd);
2910 rt2661_bbp_write(sc
, 17, sc
->ext_5ghz_lna
? 0x44 : 0x34);
2911 rt2661_bbp_write(sc
, 64, 0x21);
2913 /* restore Rx filter */
2914 RAL_WRITE(sc
, RT2661_TXRX_CSR0
, tmp
);
2918 rt2661_radar_stop(struct rt2661_softc
*sc
)
2922 /* read radar detection result */
2923 bbp66
= rt2661_bbp_read(sc
, 66);
2925 /* restore BBP registers values */
2926 rt2661_bbp_write(sc
, 16, sc
->bbp16
);
2927 rt2661_bbp_write(sc
, 17, sc
->bbp17
);
2928 rt2661_bbp_write(sc
, 18, sc
->bbp18
);
2929 rt2661_bbp_write(sc
, 21, sc
->bbp21
);
2930 rt2661_bbp_write(sc
, 22, sc
->bbp22
);
2931 rt2661_bbp_write(sc
, 64, sc
->bbp64
);
2938 rt2661_prepare_beacon(struct rt2661_softc
*sc
)
2940 struct ieee80211com
*ic
= &sc
->sc_ic
;
2941 struct ieee80211_node
*ni
= ic
->ic_bss
;
2942 struct rt2661_tx_desc desc
;
2944 struct ieee80211_beacon_offsets bo
;
2947 m0
= ieee80211_beacon_alloc(ic
, ni
, &bo
);
2949 aprint_error_dev(&sc
->sc_dev
, "could not allocate beacon frame\n");
2953 /* send beacons at the lowest available rate */
2954 rate
= IEEE80211_IS_CHAN_5GHZ(ni
->ni_chan
) ? 12 : 2;
2956 rt2661_setup_tx_desc(sc
, &desc
, RT2661_TX_TIMESTAMP
, RT2661_TX_HWSEQ
,
2957 m0
->m_pkthdr
.len
, rate
, NULL
, 0, RT2661_QID_MGT
);
2959 /* copy the first 24 bytes of Tx descriptor into NIC memory */
2960 RAL_WRITE_REGION_1(sc
, RT2661_HW_BEACON_BASE0
, (uint8_t *)&desc
, 24);
2962 /* copy beacon header and payload into NIC memory */
2963 RAL_WRITE_REGION_1(sc
, RT2661_HW_BEACON_BASE0
+ 24,
2964 mtod(m0
, uint8_t *), m0
->m_pkthdr
.len
);
2969 * Store offset of ERP Information Element so that we can update it
2970 * dynamically when the slot time changes.
2971 * XXX: this is ugly since it depends on how net80211 builds beacon
2972 * frames but ieee80211_beacon_alloc() doesn't store offsets for us.
2974 if (ic
->ic_curmode
== IEEE80211_MODE_11G
) {
2976 RT2661_HW_BEACON_BASE0
+ 24 +
2977 sizeof (struct ieee80211_frame
) +
2978 8 + 2 + 2 + 2 + ni
->ni_esslen
+
2979 2 + min(ni
->ni_rates
.rs_nrates
, IEEE80211_RATE_SIZE
) +
2981 ((ic
->ic_opmode
== IEEE80211_M_IBSS
) ? 4 : 6) +
2989 * Enable TSF synchronization and tell h/w to start sending beacons for IBSS
2990 * and HostAP operating modes.
2993 rt2661_enable_tsf_sync(struct rt2661_softc
*sc
)
2995 struct ieee80211com
*ic
= &sc
->sc_ic
;
2998 if (ic
->ic_opmode
!= IEEE80211_M_STA
) {
3000 * Change default 16ms TBTT adjustment to 8ms.
3001 * Must be done before enabling beacon generation.
3003 RAL_WRITE(sc
, RT2661_TXRX_CSR10
, 1 << 12 | 8);
3006 tmp
= RAL_READ(sc
, RT2661_TXRX_CSR9
) & 0xff000000;
3008 /* set beacon interval (in 1/16ms unit) */
3009 tmp
|= ic
->ic_bss
->ni_intval
* 16;
3011 tmp
|= RT2661_TSF_TICKING
| RT2661_ENABLE_TBTT
;
3012 if (ic
->ic_opmode
== IEEE80211_M_STA
)
3013 tmp
|= RT2661_TSF_MODE(1);
3015 tmp
|= RT2661_TSF_MODE(2) | RT2661_GENERATE_BEACON
;
3017 RAL_WRITE(sc
, RT2661_TXRX_CSR9
, tmp
);
3021 * Retrieve the "Received Signal Strength Indicator" from the raw values
3022 * contained in Rx descriptors. The computation depends on which band the
3023 * frame was received. Correction values taken from the reference driver.
3026 rt2661_get_rssi(struct rt2661_softc
*sc
, uint8_t raw
)
3030 lna
= (raw
>> 5) & 0x3;
3035 if (IEEE80211_IS_CHAN_2GHZ(sc
->sc_curchan
)) {
3036 rssi
+= sc
->rssi_2ghz_corr
;
3045 rssi
+= sc
->rssi_5ghz_corr
;