Set 2560 part default txpower to 24dBm. This value works _much_ better then
[dragonfly.git] / sys / dev / netif / ral / rt2560.c
blob0d3bf3e8f1deae52ee866fd75cd06df404345997
1 /*
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.33 2008/01/25 15:09:42 sephe Exp $
22 * Ralink Technology RT2560 chipset driver
23 * http://www.ralinktech.com/
26 #include <sys/param.h>
27 #include <sys/bus.h>
28 #include <sys/endian.h>
29 #include <sys/kernel.h>
30 #include <sys/malloc.h>
31 #include <sys/mbuf.h>
32 #include <sys/module.h>
33 #include <sys/rman.h>
34 #include <sys/socket.h>
35 #include <sys/sockio.h>
36 #include <sys/sysctl.h>
37 #include <sys/serialize.h>
39 #include <net/bpf.h>
40 #include <net/if.h>
41 #include <net/if_arp.h>
42 #include <net/ethernet.h>
43 #include <net/if_dl.h>
44 #include <net/if_media.h>
45 #include <net/ifq_var.h>
47 #include <netproto/802_11/ieee80211_var.h>
48 #include <netproto/802_11/ieee80211_radiotap.h>
49 #include <netproto/802_11/wlan_ratectl/onoe/ieee80211_onoe_param.h>
50 #include <netproto/802_11/wlan_ratectl/sample/ieee80211_sample_param.h>
52 #include <dev/netif/ral/rt2560reg.h>
53 #include <dev/netif/ral/rt2560var.h>
55 #define RT2560_RSSI(sc, rssi) \
56 ((rssi) > (RT2560_NOISE_FLOOR + (sc)->rssi_corr) ? \
57 ((rssi) - RT2560_NOISE_FLOOR - (sc)->rssi_corr) : 0)
59 #ifdef RAL_DEBUG
60 #define DPRINTF(x) do { if (ral_debug > 0) kprintf x; } while (0)
61 #define DPRINTFN(n, x) do { if (ral_debug >= (n)) kprintf x; } while (0)
62 extern int ral_debug;
63 #else
64 #define DPRINTF(x)
65 #define DPRINTFN(n, x)
66 #endif
68 static void rt2560_dma_map_addr(void *, bus_dma_segment_t *, int,
69 int);
70 static void rt2560_dma_map_mbuf(void *, bus_dma_segment_t *, int,
71 bus_size_t, int);
72 static int rt2560_alloc_tx_ring(struct rt2560_softc *,
73 struct rt2560_tx_ring *, int);
74 static void rt2560_reset_tx_ring(struct rt2560_softc *,
75 struct rt2560_tx_ring *);
76 static void rt2560_free_tx_ring(struct rt2560_softc *,
77 struct rt2560_tx_ring *);
78 static int rt2560_alloc_rx_ring(struct rt2560_softc *,
79 struct rt2560_rx_ring *, int);
80 static void rt2560_reset_rx_ring(struct rt2560_softc *,
81 struct rt2560_rx_ring *);
82 static void rt2560_free_rx_ring(struct rt2560_softc *,
83 struct rt2560_rx_ring *);
84 static int rt2560_media_change(struct ifnet *);
85 static void rt2560_next_scan(void *);
86 static int rt2560_newstate(struct ieee80211com *,
87 enum ieee80211_state, int);
88 static uint16_t rt2560_eeprom_read(struct rt2560_softc *, uint8_t);
89 static void rt2560_encryption_intr(struct rt2560_softc *);
90 static void rt2560_tx_intr(struct rt2560_softc *);
91 static void rt2560_prio_intr(struct rt2560_softc *);
92 static void rt2560_decryption_intr(struct rt2560_softc *);
93 static void rt2560_rx_intr(struct rt2560_softc *);
94 static void rt2560_beacon_expire(struct rt2560_softc *);
95 static void rt2560_wakeup_expire(struct rt2560_softc *);
96 static uint8_t rt2560_rxrate(struct rt2560_rx_desc *);
97 static uint8_t rt2560_plcp_signal(int);
98 static void rt2560_setup_tx_desc(struct rt2560_softc *,
99 struct rt2560_tx_desc *, uint32_t, int, int, int,
100 bus_addr_t);
101 static int rt2560_tx_bcn(struct rt2560_softc *, struct mbuf *,
102 struct ieee80211_node *);
103 static int rt2560_tx_mgt(struct rt2560_softc *, struct mbuf *,
104 struct ieee80211_node *);
105 static struct mbuf *rt2560_get_rts(struct rt2560_softc *,
106 struct ieee80211_frame *, uint16_t);
107 static int rt2560_tx_data(struct rt2560_softc *, struct mbuf *,
108 struct ieee80211_node *);
109 static void rt2560_start(struct ifnet *);
110 static void rt2560_watchdog(struct ifnet *);
111 static int rt2560_reset(struct ifnet *);
112 static int rt2560_ioctl(struct ifnet *, u_long, caddr_t,
113 struct ucred *);
114 static void rt2560_bbp_write(struct rt2560_softc *, uint8_t,
115 uint8_t);
116 static uint8_t rt2560_bbp_read(struct rt2560_softc *, uint8_t);
117 static void rt2560_rf_write(struct rt2560_softc *, uint8_t,
118 uint32_t);
119 static void rt2560_set_chan(struct rt2560_softc *,
120 struct ieee80211_channel *);
121 static void rt2560_enable_tsf_sync(struct rt2560_softc *);
122 static void rt2560_update_plcp(struct rt2560_softc *);
123 static void rt2560_update_slot(struct ifnet *);
124 static void rt2560_set_basicrates(struct rt2560_softc *);
125 static void rt2560_update_led(struct rt2560_softc *, int, int);
126 static void rt2560_set_bssid(struct rt2560_softc *, uint8_t *);
127 static void rt2560_set_macaddr(struct rt2560_softc *, uint8_t *);
128 static void rt2560_get_macaddr(struct rt2560_softc *, uint8_t *);
129 static void rt2560_update_promisc(struct rt2560_softc *);
130 static const char *rt2560_get_rf(int);
131 static void rt2560_read_config(struct rt2560_softc *);
132 static int rt2560_bbp_init(struct rt2560_softc *);
133 static void rt2560_set_txantenna(struct rt2560_softc *, int);
134 static void rt2560_set_rxantenna(struct rt2560_softc *, int);
135 static void rt2560_init(void *);
136 static void rt2560_stop(void *);
137 static void rt2560_intr(void *);
138 static void *rt2560_ratectl_attach(struct ieee80211com *, u_int);
139 static void rt2560_calibrate(void *);
140 static void rt2560_calib_rxsensitivity(struct rt2560_softc *,
141 uint32_t);
142 static int rt2560_sysctl_rxsns(SYSCTL_HANDLER_ARGS);
145 * Supported rates for 802.11a/b/g modes (in 500Kbps unit).
147 static const struct ieee80211_rateset rt2560_rateset_11a =
148 { 8, { 12, 18, 24, 36, 48, 72, 96, 108 } };
150 static const struct ieee80211_rateset rt2560_rateset_11b =
151 { 4, { 2, 4, 11, 22 } };
153 static const struct ieee80211_rateset rt2560_rateset_11g =
154 { 12, { 2, 4, 11, 22, 12, 18, 24, 36, 48, 72, 96, 108 } };
156 static const struct {
157 uint32_t reg;
158 uint32_t val;
159 } rt2560_def_mac[] = {
160 RT2560_DEF_MAC
163 static const struct {
164 uint8_t reg;
165 uint8_t val;
166 } rt2560_def_bbp[] = {
167 RT2560_DEF_BBP
170 static const uint32_t rt2560_rf2522_r2[] = RT2560_RF2522_R2;
171 static const uint32_t rt2560_rf2523_r2[] = RT2560_RF2523_R2;
172 static const uint32_t rt2560_rf2524_r2[] = RT2560_RF2524_R2;
173 static const uint32_t rt2560_rf2525_r2[] = RT2560_RF2525_R2;
174 static const uint32_t rt2560_rf2525_hi_r2[] = RT2560_RF2525_HI_R2;
175 static const uint32_t rt2560_rf2525e_r2[] = RT2560_RF2525E_R2;
176 static const uint32_t rt2560_rf2526_r2[] = RT2560_RF2526_R2;
177 static const uint32_t rt2560_rf2526_hi_r2[] = RT2560_RF2526_HI_R2;
179 static const struct {
180 uint8_t chan;
181 uint32_t r1, r2, r4;
182 } rt2560_rf5222[] = {
183 RT2560_RF5222
187 rt2560_attach(device_t dev, int id)
189 struct rt2560_softc *sc = device_get_softc(dev);
190 struct ieee80211com *ic = &sc->sc_ic;
191 struct ifnet *ifp = &ic->ic_if;
192 int error, i;
194 callout_init(&sc->scan_ch);
195 callout_init(&sc->calib_ch);
197 sc->sc_irq_rid = 0;
198 sc->sc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irq_rid,
199 RF_ACTIVE | RF_SHAREABLE);
200 if (sc->sc_irq == NULL) {
201 device_printf(dev, "could not allocate interrupt resource\n");
202 return ENXIO;
205 /* retrieve RT2560 rev. no */
206 sc->asic_rev = RAL_READ(sc, RT2560_CSR0);
208 /* retrieve MAC address */
209 rt2560_get_macaddr(sc, ic->ic_myaddr);
211 /* retrieve RF rev. no and various other things from EEPROM */
212 rt2560_read_config(sc);
214 device_printf(dev, "MAC/BBP RT2560 (rev 0x%02x), RF %s\n",
215 sc->asic_rev, rt2560_get_rf(sc->rf_rev));
218 * Allocate Tx and Rx rings.
220 error = rt2560_alloc_tx_ring(sc, &sc->txq, RT2560_TX_RING_COUNT);
221 if (error != 0) {
222 device_printf(sc->sc_dev, "could not allocate Tx ring\n");
223 goto fail;
226 error = rt2560_alloc_tx_ring(sc, &sc->atimq, RT2560_ATIM_RING_COUNT);
227 if (error != 0) {
228 device_printf(sc->sc_dev, "could not allocate ATIM ring\n");
229 goto fail;
232 error = rt2560_alloc_tx_ring(sc, &sc->prioq, RT2560_PRIO_RING_COUNT);
233 if (error != 0) {
234 device_printf(sc->sc_dev, "could not allocate Prio ring\n");
235 goto fail;
238 error = rt2560_alloc_tx_ring(sc, &sc->bcnq, RT2560_BEACON_RING_COUNT);
239 if (error != 0) {
240 device_printf(sc->sc_dev, "could not allocate Beacon ring\n");
241 goto fail;
244 error = rt2560_alloc_rx_ring(sc, &sc->rxq, RT2560_RX_RING_COUNT);
245 if (error != 0) {
246 device_printf(sc->sc_dev, "could not allocate Rx ring\n");
247 goto fail;
250 sysctl_ctx_init(&sc->sysctl_ctx);
251 sc->sysctl_tree = SYSCTL_ADD_NODE(&sc->sysctl_ctx,
252 SYSCTL_STATIC_CHILDREN(_hw),
253 OID_AUTO,
254 device_get_nameunit(dev),
255 CTLFLAG_RD, 0, "");
256 if (sc->sysctl_tree == NULL) {
257 device_printf(dev, "could not add sysctl node\n");
258 error = ENXIO;
259 goto fail;
262 ifp->if_softc = sc;
263 if_initname(ifp, device_get_name(dev), device_get_unit(dev));
264 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
265 ifp->if_init = rt2560_init;
266 ifp->if_ioctl = rt2560_ioctl;
267 ifp->if_start = rt2560_start;
268 ifp->if_watchdog = rt2560_watchdog;
269 ifq_set_maxlen(&ifp->if_snd, IFQ_MAXLEN);
270 ifq_set_ready(&ifp->if_snd);
272 ic->ic_phytype = IEEE80211_T_OFDM; /* not only, but not used */
273 ic->ic_opmode = IEEE80211_M_STA; /* default to BSS mode */
274 ic->ic_state = IEEE80211_S_INIT;
276 IEEE80211_ONOE_PARAM_SETUP(&sc->sc_onoe_param);
277 IEEE80211_SAMPLE_PARAM_SETUP(&sc->sc_sample_param);
278 ic->ic_ratectl.rc_st_ratectl_cap = IEEE80211_RATECTL_CAP_ONOE |
279 IEEE80211_RATECTL_CAP_SAMPLE;
280 ic->ic_ratectl.rc_st_ratectl = IEEE80211_RATECTL_SAMPLE;
281 ic->ic_ratectl.rc_st_attach = rt2560_ratectl_attach;
283 /* set device capabilities */
284 ic->ic_caps =
285 IEEE80211_C_IBSS | /* IBSS mode supported */
286 IEEE80211_C_MONITOR | /* monitor mode supported */
287 IEEE80211_C_HOSTAP | /* HostAp mode supported */
288 IEEE80211_C_TXPMGT | /* tx power management */
289 IEEE80211_C_SHPREAMBLE | /* short preamble supported */
290 IEEE80211_C_SHSLOT | /* short slot time supported */
291 IEEE80211_C_WPA; /* 802.11i */
293 if (sc->rf_rev == RT2560_RF_5222) {
294 /* set supported .11a rates */
295 ic->ic_sup_rates[IEEE80211_MODE_11A] = rt2560_rateset_11a;
297 /* set supported .11a channels */
298 for (i = 36; i <= 64; i += 4) {
299 ic->ic_channels[i].ic_freq =
300 ieee80211_ieee2mhz(i, IEEE80211_CHAN_5GHZ);
301 ic->ic_channels[i].ic_flags = IEEE80211_CHAN_A;
303 for (i = 100; i <= 140; 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 = 149; i <= 161; 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;
315 /* set supported .11b and .11g rates */
316 ic->ic_sup_rates[IEEE80211_MODE_11B] = rt2560_rateset_11b;
317 ic->ic_sup_rates[IEEE80211_MODE_11G] = rt2560_rateset_11g;
319 /* set supported .11b and .11g channels (1 through 14) */
320 for (i = 1; i <= 14; i++) {
321 ic->ic_channels[i].ic_freq =
322 ieee80211_ieee2mhz(i, IEEE80211_CHAN_2GHZ);
323 ic->ic_channels[i].ic_flags =
324 IEEE80211_CHAN_CCK | IEEE80211_CHAN_OFDM |
325 IEEE80211_CHAN_DYN | IEEE80211_CHAN_2GHZ;
328 sc->sc_sifs = IEEE80211_DUR_SIFS; /* Default SIFS */
330 ieee80211_ifattach(ic);
331 ic->ic_updateslot = rt2560_update_slot;
332 ic->ic_reset = rt2560_reset;
333 /* enable s/w bmiss handling in sta mode */
334 ic->ic_flags_ext |= IEEE80211_FEXT_SWBMISS;
335 ic->ic_txpowlimit = RT2560_DEFAULT_TXPOWER;
337 /* override state transition machine */
338 sc->sc_newstate = ic->ic_newstate;
339 ic->ic_newstate = rt2560_newstate;
340 ieee80211_media_init(ic, rt2560_media_change, ieee80211_media_status);
342 bpfattach_dlt(ifp, DLT_IEEE802_11_RADIO,
343 sizeof (struct ieee80211_frame) + 64, &sc->sc_drvbpf);
345 sc->sc_rxtap_len = sizeof sc->sc_rxtapu;
346 sc->sc_rxtap.wr_ihdr.it_len = htole16(sc->sc_rxtap_len);
347 sc->sc_rxtap.wr_ihdr.it_present = htole32(RT2560_RX_RADIOTAP_PRESENT);
349 sc->sc_txtap_len = sizeof sc->sc_txtapu;
350 sc->sc_txtap.wt_ihdr.it_len = htole16(sc->sc_txtap_len);
351 sc->sc_txtap.wt_ihdr.it_present = htole32(RT2560_TX_RADIOTAP_PRESENT);
354 * Add a few sysctl knobs.
356 sc->sc_dwelltime = 200; /* milliseconds */
357 sc->sc_calib_rxsns = 1; /* Enable */
358 sc->sc_rxsns = sc->sc_bbp17_dynmax;
360 SYSCTL_ADD_INT(&sc->sysctl_ctx,
361 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
362 "txantenna", CTLFLAG_RW, &sc->tx_ant, 0, "tx antenna (0=auto)");
364 SYSCTL_ADD_INT(&sc->sysctl_ctx,
365 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO,
366 "rxantenna", CTLFLAG_RW, &sc->rx_ant, 0, "rx antenna (0=auto)");
368 SYSCTL_ADD_INT(&sc->sysctl_ctx,
369 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "dwell",
370 CTLFLAG_RW, &sc->sc_dwelltime, 0,
371 "channel dwell time (ms) for AP/station scanning");
373 SYSCTL_ADD_PROC(&sc->sysctl_ctx,
374 SYSCTL_CHILDREN(sc->sysctl_tree),
375 OID_AUTO, "rx_sensitivity", CTLTYPE_INT | CTLFLAG_RW,
376 sc, 0, rt2560_sysctl_rxsns, "I",
377 "initial RX sensitivity");
379 if (sc->sc_flags & RT2560_FLAG_RXSNS) {
380 SYSCTL_ADD_INT(&sc->sysctl_ctx,
381 SYSCTL_CHILDREN(sc->sysctl_tree), OID_AUTO, "calib_rxsns",
382 CTLFLAG_RW, &sc->sc_calib_rxsns, 0,
383 "calibrate RX sensitivity (sta mode)");
386 error = bus_setup_intr(dev, sc->sc_irq, INTR_MPSAFE, rt2560_intr,
387 sc, &sc->sc_ih, ifp->if_serializer);
388 if (error != 0) {
389 device_printf(dev, "could not set up interrupt\n");
390 bpfdetach(ifp);
391 ieee80211_ifdetach(ic);
392 goto fail;
395 if (bootverbose)
396 ieee80211_announce(ic);
397 return 0;
398 fail:
399 rt2560_detach(sc);
400 return error;
404 rt2560_detach(void *xsc)
406 struct rt2560_softc *sc = xsc;
407 struct ieee80211com *ic = &sc->sc_ic;
408 struct ifnet *ifp = ic->ic_ifp;
410 if (device_is_attached(sc->sc_dev)) {
411 lwkt_serialize_enter(ifp->if_serializer);
413 callout_stop(&sc->scan_ch);
415 rt2560_stop(sc);
416 bus_teardown_intr(sc->sc_dev, sc->sc_irq, sc->sc_ih);
418 lwkt_serialize_exit(ifp->if_serializer);
420 bpfdetach(ifp);
421 ieee80211_ifdetach(ic);
424 rt2560_free_tx_ring(sc, &sc->txq);
425 rt2560_free_tx_ring(sc, &sc->atimq);
426 rt2560_free_tx_ring(sc, &sc->prioq);
427 rt2560_free_tx_ring(sc, &sc->bcnq);
428 rt2560_free_rx_ring(sc, &sc->rxq);
430 if (sc->sc_irq != NULL) {
431 bus_release_resource(sc->sc_dev, SYS_RES_IRQ, sc->sc_irq_rid,
432 sc->sc_irq);
435 if (sc->sysctl_tree != NULL)
436 sysctl_ctx_free(&sc->sysctl_ctx);
438 return 0;
441 void
442 rt2560_shutdown(void *xsc)
444 struct rt2560_softc *sc = xsc;
445 struct ifnet *ifp = &sc->sc_ic.ic_if;
447 lwkt_serialize_enter(ifp->if_serializer);
448 rt2560_stop(sc);
449 lwkt_serialize_exit(ifp->if_serializer);
452 void
453 rt2560_suspend(void *xsc)
455 struct rt2560_softc *sc = xsc;
456 struct ifnet *ifp = &sc->sc_ic.ic_if;
458 lwkt_serialize_enter(ifp->if_serializer);
459 rt2560_stop(sc);
460 lwkt_serialize_exit(ifp->if_serializer);
463 void
464 rt2560_resume(void *xsc)
466 struct rt2560_softc *sc = xsc;
467 struct ifnet *ifp = sc->sc_ic.ic_ifp;
469 lwkt_serialize_enter(ifp->if_serializer);
470 if (ifp->if_flags & IFF_UP) {
471 ifp->if_init(ifp->if_softc);
472 if (ifp->if_flags & IFF_RUNNING)
473 ifp->if_start(ifp);
475 lwkt_serialize_exit(ifp->if_serializer);
478 static void
479 rt2560_dma_map_addr(void *arg, bus_dma_segment_t *segs, int nseg, int error)
481 if (error != 0)
482 return;
484 KASSERT(nseg == 1, ("too many DMA segments, %d should be 1", nseg));
486 *(bus_addr_t *)arg = segs[0].ds_addr;
489 static int
490 rt2560_alloc_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring,
491 int count)
493 int i, error;
495 ring->count = count;
496 ring->queued = 0;
497 ring->cur = ring->next = 0;
498 ring->cur_encrypt = ring->next_encrypt = 0;
500 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
501 BUS_SPACE_MAXADDR, NULL, NULL, count * RT2560_TX_DESC_SIZE, 1,
502 count * RT2560_TX_DESC_SIZE, 0, &ring->desc_dmat);
503 if (error != 0) {
504 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
505 goto fail;
508 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
509 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
510 if (error != 0) {
511 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
512 goto fail;
515 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
516 count * RT2560_TX_DESC_SIZE,
517 rt2560_dma_map_addr, &ring->physaddr, 0);
518 if (error != 0) {
519 device_printf(sc->sc_dev, "could not load desc DMA map\n");
521 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
522 ring->desc = NULL;
523 goto fail;
526 ring->data = kmalloc(count * sizeof (struct rt2560_tx_data), M_DEVBUF,
527 M_WAITOK | M_ZERO);
529 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
530 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, RT2560_MAX_SCATTER,
531 MCLBYTES, 0, &ring->data_dmat);
532 if (error != 0) {
533 device_printf(sc->sc_dev, "could not create data DMA tag\n");
534 goto fail;
537 for (i = 0; i < count; i++) {
538 error = bus_dmamap_create(ring->data_dmat, 0,
539 &ring->data[i].map);
540 if (error != 0) {
541 device_printf(sc->sc_dev, "could not create DMA map\n");
542 goto fail;
545 return 0;
547 fail: rt2560_free_tx_ring(sc, ring);
548 return error;
551 static void
552 rt2560_reset_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
554 struct rt2560_tx_desc *desc;
555 struct rt2560_tx_data *data;
556 int i;
558 for (i = 0; i < ring->count; i++) {
559 desc = &ring->desc[i];
560 data = &ring->data[i];
562 if (data->m != NULL) {
563 bus_dmamap_sync(ring->data_dmat, data->map,
564 BUS_DMASYNC_POSTWRITE);
565 bus_dmamap_unload(ring->data_dmat, data->map);
566 m_freem(data->m);
567 data->m = NULL;
570 if (data->ni != NULL) {
571 ieee80211_free_node(data->ni);
572 data->ni = NULL;
575 desc->flags = 0;
578 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
580 ring->queued = 0;
581 ring->cur = ring->next = 0;
582 ring->cur_encrypt = ring->next_encrypt = 0;
585 static void
586 rt2560_free_tx_ring(struct rt2560_softc *sc, struct rt2560_tx_ring *ring)
588 struct rt2560_tx_data *data;
589 int i;
591 if (ring->desc != NULL) {
592 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
593 BUS_DMASYNC_POSTWRITE);
594 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
595 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
596 ring->desc = NULL;
599 if (ring->desc_dmat != NULL) {
600 bus_dma_tag_destroy(ring->desc_dmat);
601 ring->desc_dmat = NULL;
604 if (ring->data != NULL) {
605 for (i = 0; i < ring->count; i++) {
606 data = &ring->data[i];
608 if (data->m != NULL) {
609 bus_dmamap_sync(ring->data_dmat, data->map,
610 BUS_DMASYNC_POSTWRITE);
611 bus_dmamap_unload(ring->data_dmat, data->map);
612 m_freem(data->m);
613 data->m = NULL;
616 if (data->ni != NULL) {
617 ieee80211_free_node(data->ni);
618 data->ni = NULL;
621 if (data->map != NULL) {
622 bus_dmamap_destroy(ring->data_dmat, data->map);
623 data->map = NULL;
627 kfree(ring->data, M_DEVBUF);
628 ring->data = NULL;
631 if (ring->data_dmat != NULL) {
632 bus_dma_tag_destroy(ring->data_dmat);
633 ring->data_dmat = NULL;
637 static int
638 rt2560_alloc_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring,
639 int count)
641 struct rt2560_rx_desc *desc;
642 struct rt2560_rx_data *data;
643 bus_addr_t physaddr;
644 int i, error;
646 ring->count = count;
647 ring->cur = ring->next = 0;
648 ring->cur_decrypt = 0;
650 error = bus_dma_tag_create(NULL, 4, 0, BUS_SPACE_MAXADDR_32BIT,
651 BUS_SPACE_MAXADDR, NULL, NULL, count * RT2560_RX_DESC_SIZE, 1,
652 count * RT2560_RX_DESC_SIZE, 0, &ring->desc_dmat);
653 if (error != 0) {
654 device_printf(sc->sc_dev, "could not create desc DMA tag\n");
655 goto fail;
658 error = bus_dmamem_alloc(ring->desc_dmat, (void **)&ring->desc,
659 BUS_DMA_WAITOK | BUS_DMA_ZERO, &ring->desc_map);
660 if (error != 0) {
661 device_printf(sc->sc_dev, "could not allocate DMA memory\n");
662 goto fail;
665 error = bus_dmamap_load(ring->desc_dmat, ring->desc_map, ring->desc,
666 count * RT2560_RX_DESC_SIZE,
667 rt2560_dma_map_addr, &ring->physaddr, 0);
668 if (error != 0) {
669 device_printf(sc->sc_dev, "could not load desc DMA map\n");
671 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
672 ring->desc = NULL;
673 goto fail;
676 ring->data = kmalloc(count * sizeof (struct rt2560_rx_data), M_DEVBUF,
677 M_WAITOK | M_ZERO);
680 * Pre-allocate Rx buffers and populate Rx ring.
682 error = bus_dma_tag_create(NULL, 1, 0, BUS_SPACE_MAXADDR_32BIT,
683 BUS_SPACE_MAXADDR, NULL, NULL, MCLBYTES, 1, MCLBYTES, 0,
684 &ring->data_dmat);
685 if (error != 0) {
686 device_printf(sc->sc_dev, "could not create data DMA tag\n");
687 goto fail;
690 for (i = 0; i < count; i++) {
691 desc = &sc->rxq.desc[i];
692 data = &sc->rxq.data[i];
694 error = bus_dmamap_create(ring->data_dmat, 0, &data->map);
695 if (error != 0) {
696 device_printf(sc->sc_dev, "could not create DMA map\n");
697 goto fail;
700 data->m = m_getcl(MB_WAIT, MT_DATA, M_PKTHDR);
701 if (data->m == NULL) {
702 device_printf(sc->sc_dev,
703 "could not allocate rx mbuf\n");
704 error = ENOMEM;
705 goto fail;
708 error = bus_dmamap_load(ring->data_dmat, data->map,
709 mtod(data->m, void *), MCLBYTES, rt2560_dma_map_addr,
710 &physaddr, 0);
711 if (error != 0) {
712 device_printf(sc->sc_dev,
713 "could not load rx buf DMA map");
715 m_freem(data->m);
716 data->m = NULL;
717 goto fail;
720 desc->flags = htole32(RT2560_RX_BUSY);
721 desc->physaddr = htole32(physaddr);
724 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
726 return 0;
728 fail: rt2560_free_rx_ring(sc, ring);
729 return error;
732 static void
733 rt2560_reset_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
735 int i;
737 for (i = 0; i < ring->count; i++) {
738 ring->desc[i].flags = htole32(RT2560_RX_BUSY);
739 ring->data[i].drop = 0;
742 bus_dmamap_sync(ring->desc_dmat, ring->desc_map, BUS_DMASYNC_PREWRITE);
744 ring->cur = ring->next = 0;
745 ring->cur_decrypt = 0;
748 static void
749 rt2560_free_rx_ring(struct rt2560_softc *sc, struct rt2560_rx_ring *ring)
751 struct rt2560_rx_data *data;
753 if (ring->desc != NULL) {
754 bus_dmamap_sync(ring->desc_dmat, ring->desc_map,
755 BUS_DMASYNC_POSTWRITE);
756 bus_dmamap_unload(ring->desc_dmat, ring->desc_map);
757 bus_dmamem_free(ring->desc_dmat, ring->desc, ring->desc_map);
758 ring->desc = NULL;
761 if (ring->desc_dmat != NULL) {
762 bus_dma_tag_destroy(ring->desc_dmat);
763 ring->desc_dmat = NULL;
766 if (ring->data != NULL) {
767 int i;
769 for (i = 0; i < ring->count; i++) {
770 data = &ring->data[i];
772 if (data->m != NULL) {
773 bus_dmamap_sync(ring->data_dmat, data->map,
774 BUS_DMASYNC_POSTREAD);
775 bus_dmamap_unload(ring->data_dmat, data->map);
776 m_freem(data->m);
777 data->m = NULL;
780 if (data->map != NULL) {
781 bus_dmamap_destroy(ring->data_dmat, data->map);
782 data->map = NULL;
786 kfree(ring->data, M_DEVBUF);
787 ring->data = NULL;
790 if (ring->data_dmat != NULL) {
791 bus_dma_tag_destroy(ring->data_dmat);
792 ring->data_dmat = NULL;
796 static int
797 rt2560_media_change(struct ifnet *ifp)
799 struct rt2560_softc *sc = ifp->if_softc;
800 int error;
802 error = ieee80211_media_change(ifp);
803 if (error != ENETRESET)
804 return error;
806 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) == (IFF_UP | IFF_RUNNING))
807 rt2560_init(sc);
808 return 0;
812 * This function is called periodically (every 200ms) during scanning to
813 * switch from one channel to another.
815 static void
816 rt2560_next_scan(void *arg)
818 struct rt2560_softc *sc = arg;
819 struct ieee80211com *ic = &sc->sc_ic;
820 struct ifnet *ifp = ic->ic_ifp;
822 lwkt_serialize_enter(ifp->if_serializer);
823 if (ic->ic_state == IEEE80211_S_SCAN)
824 ieee80211_next_scan(ic);
825 lwkt_serialize_exit(ifp->if_serializer);
828 static int
829 rt2560_newstate(struct ieee80211com *ic, enum ieee80211_state nstate, int arg)
831 struct rt2560_softc *sc = ic->ic_ifp->if_softc;
832 enum ieee80211_state ostate;
833 struct ieee80211_node *ni;
834 struct mbuf *m;
835 int error = 0;
837 ostate = ic->ic_state;
838 callout_stop(&sc->scan_ch);
839 callout_stop(&sc->calib_ch);
840 ieee80211_ratectl_newstate(ic, nstate);
842 switch (nstate) {
843 case IEEE80211_S_INIT:
844 if (ostate == IEEE80211_S_RUN) {
845 /* abort TSF synchronization */
846 RAL_WRITE(sc, RT2560_CSR14, 0);
848 /* turn association led off */
849 rt2560_update_led(sc, 0, 0);
851 break;
853 case IEEE80211_S_SCAN:
854 rt2560_set_chan(sc, ic->ic_curchan);
855 callout_reset(&sc->scan_ch, (sc->sc_dwelltime * hz) / 1000,
856 rt2560_next_scan, sc);
857 break;
859 case IEEE80211_S_AUTH:
860 rt2560_set_chan(sc, ic->ic_curchan);
861 break;
863 case IEEE80211_S_ASSOC:
864 rt2560_set_chan(sc, ic->ic_curchan);
865 break;
867 case IEEE80211_S_RUN:
868 sc->sc_avgrssi = -1;
869 rt2560_set_chan(sc, ic->ic_curchan);
871 ni = ic->ic_bss;
873 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
874 rt2560_update_plcp(sc);
875 rt2560_set_basicrates(sc);
876 rt2560_set_bssid(sc, ni->ni_bssid);
879 if (ic->ic_opmode == IEEE80211_M_HOSTAP ||
880 ic->ic_opmode == IEEE80211_M_IBSS) {
881 m = ieee80211_beacon_alloc(ic, ni, &sc->sc_bo);
882 if (m == NULL) {
883 device_printf(sc->sc_dev,
884 "could not allocate beacon\n");
885 error = ENOBUFS;
886 break;
889 ieee80211_ref_node(ni);
890 error = rt2560_tx_bcn(sc, m, ni);
891 if (error != 0)
892 break;
895 /* turn assocation led on */
896 rt2560_update_led(sc, 1, 0);
898 if (ic->ic_opmode != IEEE80211_M_MONITOR)
899 rt2560_enable_tsf_sync(sc);
900 if (ic->ic_opmode == IEEE80211_M_STA) {
901 /* Clear false CCA counter */
902 RAL_READ(sc, RT2560_CNT3);
903 callout_reset(&sc->calib_ch, hz, rt2560_calibrate, sc);
905 break;
908 return (error != 0) ? error : sc->sc_newstate(ic, nstate, arg);
912 * Read 16 bits at address 'addr' from the serial EEPROM (either 93C46 or
913 * 93C66).
915 static uint16_t
916 rt2560_eeprom_read(struct rt2560_softc *sc, uint8_t addr)
918 uint32_t tmp;
919 uint16_t val;
920 int n;
922 /* clock C once before the first command */
923 RT2560_EEPROM_CTL(sc, 0);
925 RT2560_EEPROM_CTL(sc, RT2560_S);
926 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
927 RT2560_EEPROM_CTL(sc, RT2560_S);
929 /* write start bit (1) */
930 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
931 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
933 /* write READ opcode (10) */
934 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D);
935 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_D | RT2560_C);
936 RT2560_EEPROM_CTL(sc, RT2560_S);
937 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
939 /* write address (A5-A0 or A7-A0) */
940 n = (RAL_READ(sc, RT2560_CSR21) & RT2560_93C46) ? 5 : 7;
941 for (; n >= 0; n--) {
942 RT2560_EEPROM_CTL(sc, RT2560_S |
943 (((addr >> n) & 1) << RT2560_SHIFT_D));
944 RT2560_EEPROM_CTL(sc, RT2560_S |
945 (((addr >> n) & 1) << RT2560_SHIFT_D) | RT2560_C);
948 RT2560_EEPROM_CTL(sc, RT2560_S);
950 /* read data Q15-Q0 */
951 val = 0;
952 for (n = 15; n >= 0; n--) {
953 RT2560_EEPROM_CTL(sc, RT2560_S | RT2560_C);
954 tmp = RAL_READ(sc, RT2560_CSR21);
955 val |= ((tmp & RT2560_Q) >> RT2560_SHIFT_Q) << n;
956 RT2560_EEPROM_CTL(sc, RT2560_S);
959 RT2560_EEPROM_CTL(sc, 0);
961 /* clear Chip Select and clock C */
962 RT2560_EEPROM_CTL(sc, RT2560_S);
963 RT2560_EEPROM_CTL(sc, 0);
964 RT2560_EEPROM_CTL(sc, RT2560_C);
966 return val;
970 * Some frames were processed by the hardware cipher engine and are ready for
971 * transmission.
973 static void
974 rt2560_encryption_intr(struct rt2560_softc *sc)
976 struct rt2560_tx_desc *desc;
977 int hw;
979 /* retrieve last descriptor index processed by cipher engine */
980 hw = RAL_READ(sc, RT2560_SECCSR1) - sc->txq.physaddr;
981 hw /= RT2560_TX_DESC_SIZE;
983 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
984 BUS_DMASYNC_POSTREAD);
986 for (; sc->txq.next_encrypt != hw;) {
987 desc = &sc->txq.desc[sc->txq.next_encrypt];
989 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
990 (le32toh(desc->flags) & RT2560_TX_CIPHER_BUSY))
991 break;
993 /* for TKIP, swap eiv field to fix a bug in ASIC */
994 if ((le32toh(desc->flags) & RT2560_TX_CIPHER_MASK) ==
995 RT2560_TX_CIPHER_TKIP)
996 desc->eiv = bswap32(desc->eiv);
998 /* mark the frame ready for transmission */
999 desc->flags |= htole32(RT2560_TX_VALID);
1000 desc->flags |= htole32(RT2560_TX_BUSY);
1002 DPRINTFN(15, ("encryption done idx=%u\n",
1003 sc->txq.next_encrypt));
1005 sc->txq.next_encrypt =
1006 (sc->txq.next_encrypt + 1) % RT2560_TX_RING_COUNT;
1009 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1010 BUS_DMASYNC_PREWRITE);
1012 /* kick Tx */
1013 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_TX);
1016 static void
1017 rt2560_tx_intr(struct rt2560_softc *sc)
1019 struct ieee80211com *ic = &sc->sc_ic;
1020 struct ifnet *ifp = ic->ic_ifp;
1022 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1023 BUS_DMASYNC_POSTREAD);
1025 for (;;) {
1026 struct rt2560_tx_desc *desc;
1027 struct rt2560_tx_data *data;
1028 struct ieee80211_node *ni;
1029 int rateidx, data_retries, failed;
1030 struct mbuf *m;
1031 uint32_t flags;
1033 desc = &sc->txq.desc[sc->txq.next];
1034 data = &sc->txq.data[sc->txq.next];
1036 flags = le32toh(desc->flags);
1038 if ((flags & RT2560_TX_BUSY) ||
1039 (flags & RT2560_TX_CIPHER_BUSY) ||
1040 !(flags & RT2560_TX_VALID))
1041 break;
1043 rateidx = data->rateidx;
1044 ni = data->ni;
1045 m = data->m;
1047 data->ni = NULL;
1048 data->m = NULL;
1050 failed = 0;
1051 switch (flags & RT2560_TX_RESULT_MASK) {
1052 case RT2560_TX_SUCCESS:
1053 DPRINTFN(10, ("data frame sent successfully\n"));
1054 ifp->if_opackets++;
1055 data_retries = 0;
1056 break;
1058 case RT2560_TX_SUCCESS_RETRY:
1059 data_retries = (flags >> 5) & 0x7;
1060 DPRINTFN(9, ("data frame sent after %u retries\n",
1061 data_retries));
1062 ifp->if_opackets++;
1063 break;
1065 case RT2560_TX_FAIL_RETRY:
1066 DPRINTFN(9, ("sending data frame failed (too much "
1067 "retries)\n"));
1068 ifp->if_oerrors++;
1069 data_retries = 7;
1070 failed = 1;
1071 break;
1073 case RT2560_TX_FAIL_INVALID:
1074 case RT2560_TX_FAIL_OTHER:
1075 default:
1076 data_retries = 7;
1077 failed = 1;
1078 device_printf(sc->sc_dev, "sending data frame failed "
1079 "0x%08x\n", flags);
1080 ifp->if_oerrors++;
1081 break;
1084 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1085 BUS_DMASYNC_POSTWRITE);
1086 bus_dmamap_unload(sc->txq.data_dmat, data->map);
1088 if (rateidx >= 0) {
1089 struct ieee80211_ratectl_res res;
1091 res.rc_res_tries = data_retries + 1;
1092 res.rc_res_rateidx = rateidx;
1093 ieee80211_ratectl_tx_complete(ni, m->m_pkthdr.len,
1094 &res, 1, data_retries, 0, failed);
1097 m_freem(m);
1098 ieee80211_free_node(ni);
1100 /* descriptor is no longer valid */
1101 desc->flags &= ~htole32(RT2560_TX_VALID);
1103 DPRINTFN(15, ("tx done idx=%u\n", sc->txq.next));
1105 sc->txq.queued--;
1106 sc->txq.next = (sc->txq.next + 1) % RT2560_TX_RING_COUNT;
1109 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1110 BUS_DMASYNC_PREWRITE);
1112 if (sc->txq.queued == 0 && sc->prioq.queued == 0)
1113 sc->sc_tx_timer = 0;
1115 if (sc->txq.queued < RT2560_TX_RING_COUNT - 1) {
1116 sc->sc_flags &= ~RT2560_FLAG_DATA_OACT;
1117 if ((sc->sc_flags &
1118 (RT2560_FLAG_DATA_OACT | RT2560_FLAG_PRIO_OACT)) == 0)
1119 ifp->if_flags &= ~IFF_OACTIVE;
1120 rt2560_start(ifp);
1124 static void
1125 rt2560_prio_intr(struct rt2560_softc *sc)
1127 struct ieee80211com *ic = &sc->sc_ic;
1128 struct ifnet *ifp = ic->ic_ifp;
1129 struct rt2560_tx_desc *desc;
1130 struct rt2560_tx_data *data;
1132 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1133 BUS_DMASYNC_POSTREAD);
1135 for (;;) {
1136 desc = &sc->prioq.desc[sc->prioq.next];
1137 data = &sc->prioq.data[sc->prioq.next];
1139 if ((le32toh(desc->flags) & RT2560_TX_BUSY) ||
1140 !(le32toh(desc->flags) & RT2560_TX_VALID))
1141 break;
1143 switch (le32toh(desc->flags) & RT2560_TX_RESULT_MASK) {
1144 case RT2560_TX_SUCCESS:
1145 DPRINTFN(10, ("mgt frame sent successfully\n"));
1146 break;
1148 case RT2560_TX_SUCCESS_RETRY:
1149 DPRINTFN(9, ("mgt frame sent after %u retries\n",
1150 (le32toh(desc->flags) >> 5) & 0x7));
1151 break;
1153 case RT2560_TX_FAIL_RETRY:
1154 DPRINTFN(9, ("sending mgt frame failed (too much "
1155 "retries)\n"));
1156 break;
1158 case RT2560_TX_FAIL_INVALID:
1159 case RT2560_TX_FAIL_OTHER:
1160 default:
1161 device_printf(sc->sc_dev, "sending mgt frame failed "
1162 "0x%08x\n", le32toh(desc->flags));
1165 bus_dmamap_sync(sc->prioq.data_dmat, data->map,
1166 BUS_DMASYNC_POSTWRITE);
1167 bus_dmamap_unload(sc->prioq.data_dmat, data->map);
1168 m_freem(data->m);
1169 data->m = NULL;
1171 KASSERT(data->ni == NULL, ("mgmt node is not empty\n"));
1173 /* descriptor is no longer valid */
1174 desc->flags &= ~htole32(RT2560_TX_VALID);
1176 DPRINTFN(15, ("prio done idx=%u\n", sc->prioq.next));
1178 sc->prioq.queued--;
1179 sc->prioq.next = (sc->prioq.next + 1) % RT2560_PRIO_RING_COUNT;
1182 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1183 BUS_DMASYNC_PREWRITE);
1185 if (sc->txq.queued == 0 && sc->prioq.queued == 0)
1186 sc->sc_tx_timer = 0;
1188 if (sc->prioq.queued < RT2560_PRIO_RING_COUNT) {
1189 sc->sc_flags &= ~RT2560_FLAG_PRIO_OACT;
1190 if ((sc->sc_flags &
1191 (RT2560_FLAG_DATA_OACT | RT2560_FLAG_PRIO_OACT)) == 0)
1192 ifp->if_flags &= ~IFF_OACTIVE;
1193 rt2560_start(ifp);
1198 * Some frames were processed by the hardware cipher engine and are ready for
1199 * transmission to the IEEE802.11 layer.
1201 static void
1202 rt2560_decryption_intr(struct rt2560_softc *sc)
1204 struct ieee80211com *ic = &sc->sc_ic;
1205 struct ifnet *ifp = ic->ic_ifp;
1206 struct rt2560_rx_desc *desc;
1207 struct rt2560_rx_data *data;
1208 bus_addr_t physaddr;
1209 struct ieee80211_frame *wh;
1210 struct ieee80211_node *ni;
1211 struct mbuf *mnew, *m;
1212 int hw, error;
1214 /* retrieve last decriptor index processed by cipher engine */
1215 hw = RAL_READ(sc, RT2560_SECCSR0) - sc->rxq.physaddr;
1216 hw /= RT2560_RX_DESC_SIZE;
1218 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1219 BUS_DMASYNC_POSTREAD);
1221 for (; sc->rxq.cur_decrypt != hw;) {
1222 int rssi;
1224 desc = &sc->rxq.desc[sc->rxq.cur_decrypt];
1225 data = &sc->rxq.data[sc->rxq.cur_decrypt];
1227 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1228 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1229 break;
1231 if (data->drop) {
1232 ifp->if_ierrors++;
1233 goto skip;
1236 if ((le32toh(desc->flags) & RT2560_RX_CIPHER_MASK) != 0 &&
1237 (le32toh(desc->flags) & RT2560_RX_ICV_ERROR)) {
1238 ifp->if_ierrors++;
1239 goto skip;
1243 * Try to allocate a new mbuf for this ring element and load it
1244 * before processing the current mbuf. If the ring element
1245 * cannot be loaded, drop the received packet and reuse the old
1246 * mbuf. In the unlikely case that the old mbuf can't be
1247 * reloaded either, explicitly panic.
1249 mnew = m_getcl(MB_DONTWAIT, MT_DATA, M_PKTHDR);
1250 if (mnew == NULL) {
1251 ifp->if_ierrors++;
1252 goto skip;
1255 bus_dmamap_sync(sc->rxq.data_dmat, data->map,
1256 BUS_DMASYNC_POSTREAD);
1257 bus_dmamap_unload(sc->rxq.data_dmat, data->map);
1259 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1260 mtod(mnew, void *), MCLBYTES, rt2560_dma_map_addr,
1261 &physaddr, 0);
1262 if (error != 0) {
1263 m_freem(mnew);
1265 /* try to reload the old mbuf */
1266 error = bus_dmamap_load(sc->rxq.data_dmat, data->map,
1267 mtod(data->m, void *), MCLBYTES,
1268 rt2560_dma_map_addr, &physaddr, 0);
1269 if (error != 0) {
1270 /* very unlikely that it will fail... */
1271 panic("%s: could not load old rx mbuf",
1272 device_get_name(sc->sc_dev));
1274 ifp->if_ierrors++;
1275 goto skip;
1279 * New mbuf successfully loaded, update Rx ring and continue
1280 * processing.
1282 m = data->m;
1283 data->m = mnew;
1284 desc->physaddr = htole32(physaddr);
1286 /* finalize mbuf */
1287 m->m_pkthdr.rcvif = ifp;
1288 m->m_pkthdr.len = m->m_len =
1289 (le32toh(desc->flags) >> 16) & 0xfff;
1291 rssi = RT2560_RSSI(sc, desc->rssi);
1292 if (sc->sc_avgrssi < 0)
1293 sc->sc_avgrssi = rssi;
1294 else
1295 sc->sc_avgrssi = ((sc->sc_avgrssi * 7) + rssi) >> 3;
1297 if (sc->sc_drvbpf != NULL) {
1298 struct rt2560_rx_radiotap_header *tap = &sc->sc_rxtap;
1299 uint32_t tsf_lo, tsf_hi;
1301 /* get timestamp (low and high 32 bits) */
1302 tsf_hi = RAL_READ(sc, RT2560_CSR17);
1303 tsf_lo = RAL_READ(sc, RT2560_CSR16);
1305 tap->wr_tsf =
1306 htole64(((uint64_t)tsf_hi << 32) | tsf_lo);
1307 tap->wr_flags = 0;
1308 tap->wr_rate = rt2560_rxrate(desc);
1309 tap->wr_chan_freq = htole16(ic->ic_curchan->ic_freq);
1310 tap->wr_chan_flags = htole16(ic->ic_curchan->ic_flags);
1311 tap->wr_antenna = sc->rx_ant;
1312 tap->wr_antsignal = rssi;
1314 bpf_ptap(sc->sc_drvbpf, m, tap, sc->sc_rxtap_len);
1317 wh = mtod(m, struct ieee80211_frame *);
1318 ni = ieee80211_find_rxnode(ic,
1319 (struct ieee80211_frame_min *)wh);
1321 /* send the frame to the 802.11 layer */
1322 ieee80211_input(ic, m, ni, rssi, 0);
1324 /* node is no longer needed */
1325 ieee80211_free_node(ni);
1327 skip: desc->flags = htole32(RT2560_RX_BUSY);
1329 DPRINTFN(15, ("decryption done idx=%u\n", sc->rxq.cur_decrypt));
1331 sc->rxq.cur_decrypt =
1332 (sc->rxq.cur_decrypt + 1) % RT2560_RX_RING_COUNT;
1335 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1336 BUS_DMASYNC_PREWRITE);
1340 * Some frames were received. Pass them to the hardware cipher engine before
1341 * sending them to the 802.11 layer.
1343 static void
1344 rt2560_rx_intr(struct rt2560_softc *sc)
1346 struct rt2560_rx_desc *desc;
1347 struct rt2560_rx_data *data;
1349 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1350 BUS_DMASYNC_POSTREAD);
1352 for (;;) {
1353 desc = &sc->rxq.desc[sc->rxq.cur];
1354 data = &sc->rxq.data[sc->rxq.cur];
1356 if ((le32toh(desc->flags) & RT2560_RX_BUSY) ||
1357 (le32toh(desc->flags) & RT2560_RX_CIPHER_BUSY))
1358 break;
1360 data->drop = 0;
1362 if ((le32toh(desc->flags) & RT2560_RX_PHY_ERROR) ||
1363 (le32toh(desc->flags) & RT2560_RX_CRC_ERROR)) {
1365 * This should not happen since we did not request
1366 * to receive those frames when we filled RXCSR0.
1368 DPRINTFN(5, ("PHY or CRC error flags 0x%08x\n",
1369 le32toh(desc->flags)));
1370 data->drop = 1;
1373 if (((le32toh(desc->flags) >> 16) & 0xfff) > MCLBYTES) {
1374 DPRINTFN(5, ("bad length\n"));
1375 data->drop = 1;
1378 /* mark the frame for decryption */
1379 desc->flags |= htole32(RT2560_RX_CIPHER_BUSY);
1381 DPRINTFN(15, ("rx done idx=%u\n", sc->rxq.cur));
1383 sc->rxq.cur = (sc->rxq.cur + 1) % RT2560_RX_RING_COUNT;
1386 bus_dmamap_sync(sc->rxq.desc_dmat, sc->rxq.desc_map,
1387 BUS_DMASYNC_PREWRITE);
1389 /* kick decrypt */
1390 RAL_WRITE(sc, RT2560_SECCSR0, RT2560_KICK_DECRYPT);
1394 * This function is called periodically in IBSS mode when a new beacon must be
1395 * sent out.
1397 static void
1398 rt2560_beacon_expire(struct rt2560_softc *sc)
1400 struct ieee80211com *ic = &sc->sc_ic;
1401 struct rt2560_tx_data *data;
1403 if (ic->ic_opmode != IEEE80211_M_IBSS &&
1404 ic->ic_opmode != IEEE80211_M_HOSTAP)
1405 return;
1407 data = &sc->bcnq.data[sc->bcnq.next];
1409 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_POSTWRITE);
1410 bus_dmamap_unload(sc->bcnq.data_dmat, data->map);
1412 ieee80211_beacon_update(ic, data->ni, &sc->sc_bo, data->m, 1);
1414 if (ic->ic_rawbpf != NULL)
1415 bpf_mtap(ic->ic_rawbpf, data->m);
1417 rt2560_tx_bcn(sc, data->m, data->ni);
1419 DPRINTFN(15, ("beacon expired\n"));
1421 sc->bcnq.next = (sc->bcnq.next + 1) % RT2560_BEACON_RING_COUNT;
1424 /* ARGSUSED */
1425 static void
1426 rt2560_wakeup_expire(struct rt2560_softc *sc)
1428 DPRINTFN(2, ("wakeup expired\n"));
1431 static void
1432 rt2560_intr(void *arg)
1434 struct rt2560_softc *sc = arg;
1435 struct ifnet *ifp = &sc->sc_ic.ic_if;
1436 uint32_t r;
1438 /* disable interrupts */
1439 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
1441 /* don't re-enable interrupts if we're shutting down */
1442 if (!(ifp->if_flags & IFF_RUNNING))
1443 return;
1445 r = RAL_READ(sc, RT2560_CSR7);
1446 RAL_WRITE(sc, RT2560_CSR7, r);
1448 if (r & RT2560_BEACON_EXPIRE)
1449 rt2560_beacon_expire(sc);
1451 if (r & RT2560_WAKEUP_EXPIRE)
1452 rt2560_wakeup_expire(sc);
1454 if (r & RT2560_PRIO_DONE)
1455 rt2560_prio_intr(sc);
1457 if (r & (RT2560_RX_DONE | RT2560_TX_DONE | RT2560_ENCRYPTION_DONE)) {
1458 int i;
1460 for (i = 0; i < 2; ++i) {
1461 rt2560_tx_intr(sc);
1462 rt2560_encryption_intr(sc);
1466 if (r & (RT2560_DECRYPTION_DONE | RT2560_RX_DONE)) {
1467 int i;
1469 for (i = 0; i < 2; ++i) {
1470 rt2560_decryption_intr(sc);
1471 rt2560_rx_intr(sc);
1475 /* re-enable interrupts */
1476 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
1479 /* quickly determine if a given rate is CCK or OFDM */
1480 #define RAL_RATE_IS_OFDM(rate) ((rate) >= 12 && (rate) != 22)
1482 #define RAL_ACK_SIZE (sizeof(struct ieee80211_frame_ack) + IEEE80211_CRC_LEN)
1483 #define RAL_CTS_SIZE (sizeof(struct ieee80211_frame_cts) + IEEE80211_CRC_LEN)
1485 #define RT2560_TXRX_TURNAROUND 10 /* us */
1488 * This function is only used by the Rx radiotap code.
1490 static uint8_t
1491 rt2560_rxrate(struct rt2560_rx_desc *desc)
1493 if (le32toh(desc->flags) & RT2560_RX_OFDM) {
1494 /* reverse function of rt2560_plcp_signal */
1495 switch (desc->rate) {
1496 case 0xb: return 12;
1497 case 0xf: return 18;
1498 case 0xa: return 24;
1499 case 0xe: return 36;
1500 case 0x9: return 48;
1501 case 0xd: return 72;
1502 case 0x8: return 96;
1503 case 0xc: return 108;
1505 } else {
1506 if (desc->rate == 10)
1507 return 2;
1508 if (desc->rate == 20)
1509 return 4;
1510 if (desc->rate == 55)
1511 return 11;
1512 if (desc->rate == 110)
1513 return 22;
1515 return 2; /* should not get there */
1518 static uint8_t
1519 rt2560_plcp_signal(int rate)
1521 switch (rate) {
1522 /* CCK rates (returned values are device-dependent) */
1523 case 2: return 0x0;
1524 case 4: return 0x1;
1525 case 11: return 0x2;
1526 case 22: return 0x3;
1528 /* OFDM rates (cf IEEE Std 802.11a-1999, pp. 14 Table 80) */
1529 case 12: return 0xb;
1530 case 18: return 0xf;
1531 case 24: return 0xa;
1532 case 36: return 0xe;
1533 case 48: return 0x9;
1534 case 72: return 0xd;
1535 case 96: return 0x8;
1536 case 108: return 0xc;
1538 /* unsupported rates (should not get there) */
1539 default: return 0xff;
1543 static void
1544 rt2560_setup_tx_desc(struct rt2560_softc *sc, struct rt2560_tx_desc *desc,
1545 uint32_t flags, int len, int rate, int encrypt, bus_addr_t physaddr)
1547 struct ieee80211com *ic = &sc->sc_ic;
1548 uint16_t plcp_length;
1549 int remainder;
1551 desc->flags = htole32(flags);
1552 desc->flags |= htole32(len << 16);
1554 desc->physaddr = htole32(physaddr);
1555 desc->wme = htole16(
1556 RT2560_AIFSN(2) |
1557 RT2560_LOGCWMIN(3) |
1558 RT2560_LOGCWMAX(8));
1560 /* setup PLCP fields */
1561 desc->plcp_signal = rt2560_plcp_signal(rate);
1562 desc->plcp_service = 4;
1564 len += IEEE80211_CRC_LEN;
1565 if (RAL_RATE_IS_OFDM(rate)) {
1566 desc->flags |= htole32(RT2560_TX_OFDM);
1568 plcp_length = len & 0xfff;
1569 desc->plcp_length_hi = plcp_length >> 6;
1570 desc->plcp_length_lo = plcp_length & 0x3f;
1571 } else {
1572 plcp_length = (16 * len + rate - 1) / rate;
1573 if (rate == 22) {
1574 remainder = (16 * len) % 22;
1575 if (remainder != 0 && remainder < 7)
1576 desc->plcp_service |= RT2560_PLCP_LENGEXT;
1578 desc->plcp_length_hi = plcp_length >> 8;
1579 desc->plcp_length_lo = plcp_length & 0xff;
1581 if (rate != 2 && (ic->ic_flags & IEEE80211_F_SHPREAMBLE))
1582 desc->plcp_signal |= 0x08;
1585 if (!encrypt)
1586 desc->flags |= htole32(RT2560_TX_VALID);
1587 desc->flags |= encrypt ? htole32(RT2560_TX_CIPHER_BUSY)
1588 : htole32(RT2560_TX_BUSY);
1591 static int
1592 rt2560_tx_bcn(struct rt2560_softc *sc, struct mbuf *m0,
1593 struct ieee80211_node *ni)
1595 struct ieee80211com *ic = &sc->sc_ic;
1596 struct rt2560_tx_desc *desc;
1597 struct rt2560_tx_data *data;
1598 bus_addr_t paddr;
1599 int rate, error;
1601 desc = &sc->bcnq.desc[sc->bcnq.cur];
1602 data = &sc->bcnq.data[sc->bcnq.cur];
1604 rate = IEEE80211_IS_CHAN_5GHZ(ni->ni_chan) ? 12 : 2;
1606 error = bus_dmamap_load_mbuf(sc->bcnq.data_dmat, data->map, m0,
1607 rt2560_dma_map_mbuf, &paddr,
1608 BUS_DMA_NOWAIT);
1609 if (error != 0) {
1610 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1611 error);
1612 m_freem(m0);
1613 return error;
1616 if (sc->sc_drvbpf != NULL) {
1617 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1619 tap->wt_flags = 0;
1620 tap->wt_rate = rate;
1621 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1622 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1623 tap->wt_antenna = sc->tx_ant;
1625 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1628 data->m = m0;
1629 data->ni = ni;
1631 rt2560_setup_tx_desc(sc, desc, RT2560_TX_IFS_NEWBACKOFF |
1632 RT2560_TX_TIMESTAMP, m0->m_pkthdr.len, rate, 0, paddr);
1634 DPRINTFN(10, ("sending beacon frame len=%u idx=%u rate=%u\n",
1635 m0->m_pkthdr.len, sc->bcnq.cur, rate));
1637 bus_dmamap_sync(sc->bcnq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1638 bus_dmamap_sync(sc->bcnq.desc_dmat, sc->bcnq.desc_map,
1639 BUS_DMASYNC_PREWRITE);
1641 sc->bcnq.cur = (sc->bcnq.cur + 1) % RT2560_BEACON_RING_COUNT;
1643 return 0;
1646 static int
1647 rt2560_tx_mgt(struct rt2560_softc *sc, struct mbuf *m0,
1648 struct ieee80211_node *ni)
1650 struct ieee80211com *ic = &sc->sc_ic;
1651 struct rt2560_tx_desc *desc;
1652 struct rt2560_tx_data *data;
1653 struct ieee80211_frame *wh;
1654 bus_addr_t paddr;
1655 uint16_t dur;
1656 uint32_t flags = 0;
1657 int rate, error;
1659 desc = &sc->prioq.desc[sc->prioq.cur];
1660 data = &sc->prioq.data[sc->prioq.cur];
1662 rate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1664 error = bus_dmamap_load_mbuf(sc->prioq.data_dmat, data->map, m0,
1665 rt2560_dma_map_mbuf, &paddr, 0);
1666 if (error != 0) {
1667 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1668 error);
1669 ieee80211_free_node(ni);
1670 m_freem(m0);
1671 return error;
1674 if (sc->sc_drvbpf != NULL) {
1675 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1677 tap->wt_flags = 0;
1678 tap->wt_rate = rate;
1679 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1680 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1681 tap->wt_antenna = sc->tx_ant;
1683 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1686 data->m = m0;
1687 data->ni = NULL;
1689 wh = mtod(m0, struct ieee80211_frame *);
1691 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1692 flags |= RT2560_TX_ACK;
1694 dur = ieee80211_txtime(ni, RAL_ACK_SIZE, rate, ic->ic_flags) +
1695 sc->sc_sifs;
1696 *(uint16_t *)wh->i_dur = htole16(dur);
1698 /* tell hardware to add timestamp for probe responses */
1699 if ((wh->i_fc[0] & IEEE80211_FC0_TYPE_MASK) ==
1700 IEEE80211_FC0_TYPE_MGT &&
1701 (wh->i_fc[0] & IEEE80211_FC0_SUBTYPE_MASK) ==
1702 IEEE80211_FC0_SUBTYPE_PROBE_RESP)
1703 flags |= RT2560_TX_TIMESTAMP;
1706 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 0, paddr);
1708 bus_dmamap_sync(sc->prioq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1709 bus_dmamap_sync(sc->prioq.desc_dmat, sc->prioq.desc_map,
1710 BUS_DMASYNC_PREWRITE);
1712 DPRINTFN(10, ("sending mgt frame len=%u idx=%u rate=%u\n",
1713 m0->m_pkthdr.len, sc->prioq.cur, rate));
1715 /* kick prio */
1716 sc->prioq.queued++;
1717 sc->prioq.cur = (sc->prioq.cur + 1) % RT2560_PRIO_RING_COUNT;
1718 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_KICK_PRIO);
1720 ieee80211_free_node(ni);
1722 return 0;
1726 * Build a RTS control frame.
1728 static struct mbuf *
1729 rt2560_get_rts(struct rt2560_softc *sc, struct ieee80211_frame *wh,
1730 uint16_t dur)
1732 struct ieee80211_frame_rts *rts;
1733 struct mbuf *m;
1735 MGETHDR(m, MB_DONTWAIT, MT_DATA);
1736 if (m == NULL) {
1737 sc->sc_ic.ic_stats.is_tx_nobuf++;
1738 device_printf(sc->sc_dev, "could not allocate RTS frame\n");
1739 return NULL;
1742 rts = mtod(m, struct ieee80211_frame_rts *);
1744 rts->i_fc[0] = IEEE80211_FC0_VERSION_0 | IEEE80211_FC0_TYPE_CTL |
1745 IEEE80211_FC0_SUBTYPE_RTS;
1746 rts->i_fc[1] = IEEE80211_FC1_DIR_NODS;
1747 *(uint16_t *)rts->i_dur = htole16(dur);
1748 IEEE80211_ADDR_COPY(rts->i_ra, wh->i_addr1);
1749 IEEE80211_ADDR_COPY(rts->i_ta, wh->i_addr2);
1751 m->m_pkthdr.len = m->m_len = sizeof(struct ieee80211_frame_rts);
1753 return m;
1756 static int
1757 rt2560_tx_data(struct rt2560_softc *sc, struct mbuf *m0,
1758 struct ieee80211_node *ni)
1760 struct ieee80211com *ic = &sc->sc_ic;
1761 struct rt2560_tx_desc *desc;
1762 struct rt2560_tx_data *data;
1763 struct ieee80211_frame *wh;
1764 struct ieee80211_key *k;
1765 struct mbuf *mnew;
1766 bus_addr_t paddr;
1767 uint16_t dur;
1768 uint32_t flags = 0;
1769 int rate, error, ackrate, rateidx;
1771 wh = mtod(m0, struct ieee80211_frame *);
1772 if (wh->i_fc[1] & IEEE80211_FC1_WEP) {
1773 k = ieee80211_crypto_encap(ic, ni, m0);
1774 if (k == NULL) {
1775 m_freem(m0);
1776 return ENOBUFS;
1779 /* packet header may have moved, reset our local pointer */
1780 wh = mtod(m0, struct ieee80211_frame *);
1783 ieee80211_ratectl_findrate(ni, m0->m_pkthdr.len, &rateidx, 1);
1784 rate = IEEE80211_RS_RATE(&ni->ni_rates, rateidx);
1786 ackrate = ieee80211_ack_rate(ni, rate);
1789 * IEEE Std 802.11-1999, pp 82: "A STA shall use an RTS/CTS exchange
1790 * for directed frames only when the length of the MPDU is greater
1791 * than the length threshold indicated by [...]" ic_rtsthreshold.
1793 if (!IEEE80211_IS_MULTICAST(wh->i_addr1) &&
1794 m0->m_pkthdr.len > ic->ic_rtsthreshold) {
1795 struct mbuf *m;
1796 uint16_t dur;
1797 int rtsrate;
1799 rtsrate = IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan) ? 12 : 2;
1800 dur = ieee80211_txtime(ni, m0->m_pkthdr.len + IEEE80211_CRC_LEN,
1801 rate, ic->ic_flags) +
1802 ieee80211_txtime(ni, RAL_CTS_SIZE, rtsrate, ic->ic_flags)+
1803 ieee80211_txtime(ni, RAL_ACK_SIZE, ackrate, ic->ic_flags)+
1804 3 * sc->sc_sifs;
1806 m = rt2560_get_rts(sc, wh, dur);
1808 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1809 data = &sc->txq.data[sc->txq.cur_encrypt];
1811 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map,
1812 m, rt2560_dma_map_mbuf, &paddr, 0);
1813 if (error != 0) {
1814 device_printf(sc->sc_dev,
1815 "could not map mbuf (error %d)\n", error);
1816 m_freem(m);
1817 m_freem(m0);
1818 return error;
1821 /* avoid multiple free() of the same node for each fragment */
1822 ieee80211_ref_node(ni);
1824 data->m = m;
1825 data->ni = ni;
1826 data->rateidx = -1; /* don't count RTS */
1828 rt2560_setup_tx_desc(sc, desc, RT2560_TX_ACK |
1829 RT2560_TX_MORE_FRAG, m->m_pkthdr.len, rtsrate, 1, paddr);
1831 bus_dmamap_sync(sc->txq.data_dmat, data->map,
1832 BUS_DMASYNC_PREWRITE);
1834 sc->txq.queued++;
1835 sc->txq.cur_encrypt =
1836 (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1839 * IEEE Std 802.11-1999: when an RTS/CTS exchange is used, the
1840 * asynchronous data frame shall be transmitted after the CTS
1841 * frame and a SIFS period.
1843 flags |= RT2560_TX_LONG_RETRY | RT2560_TX_IFS_SIFS;
1846 data = &sc->txq.data[sc->txq.cur_encrypt];
1847 desc = &sc->txq.desc[sc->txq.cur_encrypt];
1849 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map, m0,
1850 rt2560_dma_map_mbuf, &paddr, 0);
1851 if (error != 0 && error != EFBIG) {
1852 device_printf(sc->sc_dev, "could not map mbuf (error %d)\n",
1853 error);
1854 m_freem(m0);
1855 return error;
1857 if (error != 0) {
1858 mnew = m_defrag(m0, MB_DONTWAIT);
1859 if (mnew == NULL) {
1860 device_printf(sc->sc_dev,
1861 "could not defragment mbuf\n");
1862 m_freem(m0);
1863 return ENOBUFS;
1865 m0 = mnew;
1867 error = bus_dmamap_load_mbuf(sc->txq.data_dmat, data->map,
1868 m0, rt2560_dma_map_mbuf, &paddr,
1870 if (error != 0) {
1871 device_printf(sc->sc_dev,
1872 "could not map mbuf (error %d)\n", error);
1873 m_freem(m0);
1874 return error;
1877 /* packet header may have moved, reset our local pointer */
1878 wh = mtod(m0, struct ieee80211_frame *);
1881 if (sc->sc_drvbpf != NULL) {
1882 struct rt2560_tx_radiotap_header *tap = &sc->sc_txtap;
1884 tap->wt_flags = 0;
1885 tap->wt_rate = rate;
1886 tap->wt_chan_freq = htole16(ic->ic_curchan->ic_freq);
1887 tap->wt_chan_flags = htole16(ic->ic_curchan->ic_flags);
1888 tap->wt_antenna = sc->tx_ant;
1890 bpf_ptap(sc->sc_drvbpf, m0, tap, sc->sc_txtap_len);
1893 data->m = m0;
1894 data->ni = ni;
1895 data->rateidx = rateidx;
1897 if (!IEEE80211_IS_MULTICAST(wh->i_addr1)) {
1898 flags |= RT2560_TX_ACK;
1899 dur = ieee80211_txtime(ni, RAL_ACK_SIZE, ackrate, ic->ic_flags)+
1900 sc->sc_sifs;
1901 *(uint16_t *)wh->i_dur = htole16(dur);
1904 rt2560_setup_tx_desc(sc, desc, flags, m0->m_pkthdr.len, rate, 1, paddr);
1906 bus_dmamap_sync(sc->txq.data_dmat, data->map, BUS_DMASYNC_PREWRITE);
1907 bus_dmamap_sync(sc->txq.desc_dmat, sc->txq.desc_map,
1908 BUS_DMASYNC_PREWRITE);
1910 DPRINTFN(10, ("sending data frame len=%u idx=%u rate=%u\n",
1911 m0->m_pkthdr.len, sc->txq.cur_encrypt, rate));
1913 /* kick encrypt */
1914 sc->txq.queued++;
1915 sc->txq.cur_encrypt = (sc->txq.cur_encrypt + 1) % RT2560_TX_RING_COUNT;
1916 RAL_WRITE(sc, RT2560_SECCSR1, RT2560_KICK_ENCRYPT);
1918 return 0;
1921 static void
1922 rt2560_start(struct ifnet *ifp)
1924 struct rt2560_softc *sc = ifp->if_softc;
1925 struct ieee80211com *ic = &sc->sc_ic;
1926 struct mbuf *m0;
1927 struct ether_header *eh;
1928 struct ieee80211_node *ni;
1930 /* prevent management frames from being sent if we're not ready */
1931 if (!(ifp->if_flags & IFF_RUNNING))
1932 return;
1934 for (;;) {
1935 IF_POLL(&ic->ic_mgtq, m0);
1936 if (m0 != NULL) {
1937 if (sc->prioq.queued >= RT2560_PRIO_RING_COUNT) {
1938 ifp->if_flags |= IFF_OACTIVE;
1939 sc->sc_flags |= RT2560_FLAG_PRIO_OACT;
1940 break;
1942 IF_DEQUEUE(&ic->ic_mgtq, m0);
1944 ni = (struct ieee80211_node *)m0->m_pkthdr.rcvif;
1945 m0->m_pkthdr.rcvif = NULL;
1947 if (ic->ic_rawbpf != NULL)
1948 bpf_mtap(ic->ic_rawbpf, m0);
1950 if (rt2560_tx_mgt(sc, m0, ni) != 0)
1951 break;
1953 } else {
1954 if (ic->ic_state != IEEE80211_S_RUN)
1955 break;
1956 m0 = ifq_poll(&ifp->if_snd);
1957 if (m0 == NULL)
1958 break;
1959 if (sc->txq.queued >= RT2560_TX_RING_COUNT - 1) {
1960 ifp->if_flags |= IFF_OACTIVE;
1961 sc->sc_flags |= RT2560_FLAG_DATA_OACT;
1962 break;
1964 m0 = ifq_dequeue(&ifp->if_snd, m0);
1966 if (m0->m_len < sizeof (struct ether_header) &&
1967 !(m0 = m_pullup(m0, sizeof (struct ether_header))))
1968 continue;
1970 eh = mtod(m0, struct ether_header *);
1971 ni = ieee80211_find_txnode(ic, eh->ether_dhost);
1972 if (ni == NULL) {
1973 m_freem(m0);
1974 continue;
1976 BPF_MTAP(ifp, m0);
1978 m0 = ieee80211_encap(ic, m0, ni);
1979 if (m0 == NULL) {
1980 ieee80211_free_node(ni);
1981 continue;
1984 if (ic->ic_rawbpf != NULL)
1985 bpf_mtap(ic->ic_rawbpf, m0);
1987 if (rt2560_tx_data(sc, m0, ni) != 0) {
1988 ieee80211_free_node(ni);
1989 ifp->if_oerrors++;
1990 break;
1994 sc->sc_tx_timer = 5;
1995 ifp->if_timer = 1;
1999 static void
2000 rt2560_watchdog(struct ifnet *ifp)
2002 struct rt2560_softc *sc = ifp->if_softc;
2003 struct ieee80211com *ic = &sc->sc_ic;
2005 ifp->if_timer = 0;
2007 if (sc->sc_tx_timer > 0) {
2008 if (--sc->sc_tx_timer == 0) {
2009 device_printf(sc->sc_dev, "device timeout\n");
2010 rt2560_init(sc);
2011 ifp->if_oerrors++;
2012 return;
2014 ifp->if_timer = 1;
2017 ieee80211_watchdog(ic);
2021 * This function allows for fast channel switching in monitor mode (used by
2022 * net-mgmt/kismet). In IBSS mode, we must explicitly reset the interface to
2023 * generate a new beacon frame.
2025 static int
2026 rt2560_reset(struct ifnet *ifp)
2028 struct rt2560_softc *sc = ifp->if_softc;
2029 struct ieee80211com *ic = &sc->sc_ic;
2031 if (ic->ic_opmode != IEEE80211_M_MONITOR)
2032 return ENETRESET;
2034 rt2560_set_chan(sc, ic->ic_curchan);
2036 return 0;
2039 static int
2040 rt2560_ioctl(struct ifnet *ifp, u_long cmd, caddr_t data, struct ucred *cr)
2042 struct rt2560_softc *sc = ifp->if_softc;
2043 struct ieee80211com *ic = &sc->sc_ic;
2044 int error = 0;
2046 switch (cmd) {
2047 case SIOCSIFFLAGS:
2048 if (ifp->if_flags & IFF_UP) {
2049 if (ifp->if_flags & IFF_RUNNING)
2050 rt2560_update_promisc(sc);
2051 else
2052 rt2560_init(sc);
2053 } else {
2054 if (ifp->if_flags & IFF_RUNNING)
2055 rt2560_stop(sc);
2057 break;
2059 default:
2060 error = ieee80211_ioctl(ic, cmd, data, cr);
2063 if (error == ENETRESET) {
2064 if ((ifp->if_flags & (IFF_UP | IFF_RUNNING)) ==
2065 (IFF_UP | IFF_RUNNING) &&
2066 (ic->ic_roaming != IEEE80211_ROAMING_MANUAL))
2067 rt2560_init(sc);
2068 error = 0;
2071 return error;
2074 static void
2075 rt2560_bbp_write(struct rt2560_softc *sc, uint8_t reg, uint8_t val)
2077 uint32_t tmp;
2078 int ntries;
2080 for (ntries = 0; ntries < 100; ntries++) {
2081 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2082 break;
2083 DELAY(1);
2085 if (ntries == 100) {
2086 device_printf(sc->sc_dev, "could not write to BBP\n");
2087 return;
2090 tmp = RT2560_BBP_WRITE | RT2560_BBP_BUSY | reg << 8 | val;
2091 RAL_WRITE(sc, RT2560_BBPCSR, tmp);
2093 DPRINTFN(15, ("BBP R%u <- 0x%02x\n", reg, val));
2095 /* XXX */
2096 if (reg == 17) {
2097 DPRINTF(("%s record bbp17 %#x\n", __func__, val));
2098 sc->sc_bbp17 = val;
2102 static uint8_t
2103 rt2560_bbp_read(struct rt2560_softc *sc, uint8_t reg)
2105 uint32_t val;
2106 int ntries;
2108 for (ntries = 0; ntries < 100; ntries++) {
2109 if (!(RAL_READ(sc, RT2560_BBPCSR) & RT2560_BBP_BUSY))
2110 break;
2111 DELAY(1);
2113 if (ntries == 100) {
2114 device_printf(sc->sc_dev, "could not read from BBP\n");
2115 return 0;
2118 val = RT2560_BBP_BUSY | reg << 8;
2119 RAL_WRITE(sc, RT2560_BBPCSR, val);
2121 for (ntries = 0; ntries < 100; ntries++) {
2122 val = RAL_READ(sc, RT2560_BBPCSR);
2123 if (!(val & RT2560_BBP_BUSY))
2124 return val & 0xff;
2125 DELAY(1);
2128 device_printf(sc->sc_dev, "could not read from BBP\n");
2129 return 0;
2132 static void
2133 rt2560_rf_write(struct rt2560_softc *sc, uint8_t reg, uint32_t val)
2135 uint32_t tmp;
2136 int ntries;
2138 for (ntries = 0; ntries < 100; ntries++) {
2139 if (!(RAL_READ(sc, RT2560_RFCSR) & RT2560_RF_BUSY))
2140 break;
2141 DELAY(1);
2143 if (ntries == 100) {
2144 device_printf(sc->sc_dev, "could not write to RF\n");
2145 return;
2148 tmp = RT2560_RF_BUSY | RT2560_RF_20BIT | (val & 0xfffff) << 2 |
2149 (reg & 0x3);
2150 RAL_WRITE(sc, RT2560_RFCSR, tmp);
2152 /* remember last written value in sc */
2153 sc->rf_regs[reg] = val;
2155 DPRINTFN(15, ("RF R[%u] <- 0x%05x\n", reg & 0x3, val & 0xfffff));
2158 static void
2159 rt2560_set_chan(struct rt2560_softc *sc, struct ieee80211_channel *c)
2161 struct ieee80211com *ic = &sc->sc_ic;
2162 uint8_t power, tmp;
2163 u_int i, chan;
2165 chan = ieee80211_chan2ieee(ic, c);
2166 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
2167 return;
2169 if (IEEE80211_IS_CHAN_2GHZ(c))
2170 sc->sc_curtxpow = sc->txpow[chan - 1];
2171 else
2172 sc->sc_curtxpow = 31;
2174 if (ic->ic_txpowlimit > sc->sc_curtxpow)
2175 ic->ic_txpowlimit = sc->sc_curtxpow;
2176 else
2177 sc->sc_curtxpow = ic->ic_txpowlimit;
2178 ic->ic_bss->ni_txpower = sc->sc_curtxpow;
2180 power = sc->sc_curtxpow;
2182 DPRINTFN(2, ("setting channel to %u, txpower to %u\n", chan, power));
2184 switch (sc->rf_rev) {
2185 case RT2560_RF_2522:
2186 rt2560_rf_write(sc, RAL_RF1, 0x00814);
2187 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2522_r2[chan - 1]);
2188 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2189 break;
2191 case RT2560_RF_2523:
2192 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2193 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2523_r2[chan - 1]);
2194 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x38044);
2195 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2196 break;
2198 case RT2560_RF_2524:
2199 rt2560_rf_write(sc, RAL_RF1, 0x0c808);
2200 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2524_r2[chan - 1]);
2201 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2202 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2203 break;
2205 case RT2560_RF_2525:
2206 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2207 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_hi_r2[chan - 1]);
2208 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2209 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2211 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2212 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525_r2[chan - 1]);
2213 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2214 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00280 : 0x00286);
2215 break;
2217 case RT2560_RF_2525E:
2218 rt2560_rf_write(sc, RAL_RF1, 0x08808);
2219 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2525e_r2[chan - 1]);
2220 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2221 rt2560_rf_write(sc, RAL_RF4, (chan == 14) ? 0x00286 : 0x00282);
2222 break;
2224 case RT2560_RF_2526:
2225 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_hi_r2[chan - 1]);
2226 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2227 rt2560_rf_write(sc, RAL_RF1, 0x08804);
2229 rt2560_rf_write(sc, RAL_RF2, rt2560_rf2526_r2[chan - 1]);
2230 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x18044);
2231 rt2560_rf_write(sc, RAL_RF4, (chan & 1) ? 0x00386 : 0x00381);
2232 break;
2234 /* dual-band RF */
2235 case RT2560_RF_5222:
2236 for (i = 0; rt2560_rf5222[i].chan != chan; i++);
2238 rt2560_rf_write(sc, RAL_RF1, rt2560_rf5222[i].r1);
2239 rt2560_rf_write(sc, RAL_RF2, rt2560_rf5222[i].r2);
2240 rt2560_rf_write(sc, RAL_RF3, power << 7 | 0x00040);
2241 rt2560_rf_write(sc, RAL_RF4, rt2560_rf5222[i].r4);
2242 break;
2245 if (ic->ic_state != IEEE80211_S_SCAN) {
2246 /* set Japan filter bit for channel 14 */
2247 tmp = rt2560_bbp_read(sc, 70);
2249 tmp &= ~RT2560_JAPAN_FILTER;
2250 if (chan == 14)
2251 tmp |= RT2560_JAPAN_FILTER;
2253 rt2560_bbp_write(sc, 70, tmp);
2255 /* clear CRC errors */
2256 RAL_READ(sc, RT2560_CNT0);
2259 sc->sc_sifs = IEEE80211_IS_CHAN_5GHZ(c) ? IEEE80211_DUR_OFDM_SIFS
2260 : IEEE80211_DUR_SIFS;
2264 * Refer to IEEE Std 802.11-1999 pp. 123 for more information on TSF
2265 * synchronization.
2267 static void
2268 rt2560_enable_tsf_sync(struct rt2560_softc *sc)
2270 struct ieee80211com *ic = &sc->sc_ic;
2271 uint16_t logcwmin, preload;
2272 uint32_t tmp;
2274 /* first, disable TSF synchronization */
2275 RAL_WRITE(sc, RT2560_CSR14, 0);
2277 tmp = 16 * ic->ic_bss->ni_intval;
2278 RAL_WRITE(sc, RT2560_CSR12, tmp);
2280 RAL_WRITE(sc, RT2560_CSR13, 0);
2282 logcwmin = 5;
2283 preload = (ic->ic_opmode == IEEE80211_M_STA) ? 384 : 1024;
2284 tmp = logcwmin << 16 | preload;
2285 RAL_WRITE(sc, RT2560_BCNOCSR, tmp);
2287 /* finally, enable TSF synchronization */
2288 tmp = RT2560_ENABLE_TSF | RT2560_ENABLE_TBCN;
2289 if (ic->ic_opmode == IEEE80211_M_STA)
2290 tmp |= RT2560_ENABLE_TSF_SYNC(1);
2291 else
2292 tmp |= RT2560_ENABLE_TSF_SYNC(2) |
2293 RT2560_ENABLE_BEACON_GENERATOR;
2294 RAL_WRITE(sc, RT2560_CSR14, tmp);
2296 DPRINTF(("enabling TSF synchronization\n"));
2299 static void
2300 rt2560_update_plcp(struct rt2560_softc *sc)
2302 struct ieee80211com *ic = &sc->sc_ic;
2304 /* no short preamble for 1Mbps */
2305 RAL_WRITE(sc, RT2560_PLCP1MCSR, 0x00700400);
2307 if (!(ic->ic_flags & IEEE80211_F_SHPREAMBLE)) {
2308 /* values taken from the reference driver */
2309 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380401);
2310 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x00150402);
2311 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b8403);
2312 } else {
2313 /* same values as above or'ed 0x8 */
2314 RAL_WRITE(sc, RT2560_PLCP2MCSR, 0x00380409);
2315 RAL_WRITE(sc, RT2560_PLCP5p5MCSR, 0x0015040a);
2316 RAL_WRITE(sc, RT2560_PLCP11MCSR, 0x000b840b);
2319 DPRINTF(("updating PLCP for %s preamble\n",
2320 (ic->ic_flags & IEEE80211_F_SHPREAMBLE) ? "short" : "long"));
2324 * This function can be called by ieee80211_set_shortslottime(). Refer to
2325 * IEEE Std 802.11-1999 pp. 85 to know how these values are computed.
2327 static void
2328 rt2560_update_slot(struct ifnet *ifp)
2330 struct rt2560_softc *sc = ifp->if_softc;
2331 struct ieee80211com *ic = &sc->sc_ic;
2332 uint8_t slottime;
2333 uint16_t tx_sifs, tx_pifs, tx_difs, eifs;
2334 uint32_t tmp;
2336 #ifdef foo
2337 slottime = (ic->ic_flags & IEEE80211_F_SHSLOT) ? 9 : 20;
2338 #else
2340 * Setting slot time according to "short slot time" capability
2341 * in beacon/probe_resp seems to cause problem to acknowledge
2342 * certain AP's data frames transimitted at CCK/DS rates: the
2343 * problematic AP keeps retransmitting data frames, probably
2344 * because MAC level acks are not received by hardware.
2345 * So we cheat a little bit here by claiming we are capable of
2346 * "short slot time" but setting hardware slot time to the normal
2347 * slot time. ral(4) does not seem to have trouble to receive
2348 * frames transmitted using short slot time even if hardware
2349 * slot time is set to normal slot time. If we didn't use this
2350 * trick, we would have to claim that short slot time is not
2351 * supported; this would give relative poor TX performance
2352 * (-1Mb~-2Mb lower) and the _whole_ BSS would stop using short
2353 * slot time.
2355 slottime = (ic->ic_curmode == IEEE80211_MODE_11A) ? 9 : 20;
2356 #endif
2358 /* update the MAC slot boundaries */
2359 tx_sifs = sc->sc_sifs - RT2560_TXRX_TURNAROUND;
2360 tx_pifs = tx_sifs + slottime;
2361 tx_difs = tx_sifs + 2 * slottime;
2362 eifs = (ic->ic_curmode == IEEE80211_MODE_11B) ? 364 : 60;
2364 tmp = RAL_READ(sc, RT2560_CSR11);
2365 tmp = (tmp & ~0x1f00) | slottime << 8;
2366 RAL_WRITE(sc, RT2560_CSR11, tmp);
2368 tmp = tx_pifs << 16 | tx_sifs;
2369 RAL_WRITE(sc, RT2560_CSR18, tmp);
2371 tmp = eifs << 16 | tx_difs;
2372 RAL_WRITE(sc, RT2560_CSR19, tmp);
2374 DPRINTF(("setting slottime to %uus\n", slottime));
2377 static void
2378 rt2560_set_basicrates(struct rt2560_softc *sc)
2380 struct ieee80211com *ic = &sc->sc_ic;
2382 /* update basic rate set */
2383 if (ic->ic_curmode == IEEE80211_MODE_11B) {
2384 /* 11b basic rates: 1, 2Mbps */
2385 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x3);
2386 } else if (IEEE80211_IS_CHAN_5GHZ(ic->ic_curchan)) {
2387 /* 11a basic rates: 6, 12, 24Mbps */
2388 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x150);
2389 } else {
2390 /* 11g basic rates: 1, 2, 5.5, 11, 6, 12, 24Mbps */
2391 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x15f);
2395 static void
2396 rt2560_update_led(struct rt2560_softc *sc, int led1, int led2)
2398 uint32_t tmp;
2400 /* set ON period to 70ms and OFF period to 30ms */
2401 tmp = led1 << 16 | led2 << 17 | 70 << 8 | 30;
2402 RAL_WRITE(sc, RT2560_LEDCSR, tmp);
2405 static void
2406 rt2560_set_bssid(struct rt2560_softc *sc, uint8_t *bssid)
2408 uint32_t tmp;
2410 tmp = bssid[0] | bssid[1] << 8 | bssid[2] << 16 | bssid[3] << 24;
2411 RAL_WRITE(sc, RT2560_CSR5, tmp);
2413 tmp = bssid[4] | bssid[5] << 8;
2414 RAL_WRITE(sc, RT2560_CSR6, tmp);
2416 DPRINTF(("setting BSSID to %6D\n", bssid, ":"));
2419 static void
2420 rt2560_set_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2422 uint32_t tmp;
2424 tmp = addr[0] | addr[1] << 8 | addr[2] << 16 | addr[3] << 24;
2425 RAL_WRITE(sc, RT2560_CSR3, tmp);
2427 tmp = addr[4] | addr[5] << 8;
2428 RAL_WRITE(sc, RT2560_CSR4, tmp);
2430 DPRINTF(("setting MAC address to %6D\n", addr, ":"));
2433 static void
2434 rt2560_get_macaddr(struct rt2560_softc *sc, uint8_t *addr)
2436 uint32_t tmp;
2438 tmp = RAL_READ(sc, RT2560_CSR3);
2439 addr[0] = tmp & 0xff;
2440 addr[1] = (tmp >> 8) & 0xff;
2441 addr[2] = (tmp >> 16) & 0xff;
2442 addr[3] = (tmp >> 24);
2444 tmp = RAL_READ(sc, RT2560_CSR4);
2445 addr[4] = tmp & 0xff;
2446 addr[5] = (tmp >> 8) & 0xff;
2449 static void
2450 rt2560_update_promisc(struct rt2560_softc *sc)
2452 struct ifnet *ifp = sc->sc_ic.ic_ifp;
2453 uint32_t tmp;
2455 tmp = RAL_READ(sc, RT2560_RXCSR0);
2457 tmp &= ~RT2560_DROP_NOT_TO_ME;
2458 if (!(ifp->if_flags & IFF_PROMISC))
2459 tmp |= RT2560_DROP_NOT_TO_ME;
2461 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2463 DPRINTF(("%s promiscuous mode\n", (ifp->if_flags & IFF_PROMISC) ?
2464 "entering" : "leaving"));
2467 static const char *
2468 rt2560_get_rf(int rev)
2470 switch (rev) {
2471 case RT2560_RF_2522: return "RT2522";
2472 case RT2560_RF_2523: return "RT2523";
2473 case RT2560_RF_2524: return "RT2524";
2474 case RT2560_RF_2525: return "RT2525";
2475 case RT2560_RF_2525E: return "RT2525e";
2476 case RT2560_RF_2526: return "RT2526";
2477 case RT2560_RF_5222: return "RT5222";
2478 default: return "unknown";
2482 static void
2483 rt2560_read_config(struct rt2560_softc *sc)
2485 uint16_t val;
2486 int i, find_bbp17 = 0;
2488 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG0);
2489 sc->rf_rev = (val >> 11) & 0x7;
2490 sc->hw_radio = (val >> 10) & 0x1;
2491 sc->led_mode = (val >> 6) & 0x7;
2492 sc->rx_ant = (val >> 4) & 0x3;
2493 sc->tx_ant = (val >> 2) & 0x3;
2494 sc->nb_ant = val & 0x3;
2496 /* read default values for BBP registers */
2497 for (i = 0; i < 16; i++) {
2498 val = rt2560_eeprom_read(sc, RT2560_EEPROM_BBP_BASE + i);
2499 if (val == 0xffff || val == 0)
2500 continue;
2501 sc->bbp_prom[i].reg = val >> 8;
2502 sc->bbp_prom[i].val = val & 0xff;
2503 DPRINTF(("rom bbp reg:%u val:%#x\n",
2504 sc->bbp_prom[i].reg, sc->bbp_prom[i].val));
2506 if (sc->bbp_prom[i].reg == 17) {
2507 if (sc->bbp_prom[i].val > 6)
2508 sc->sc_bbp17_dynmin = sc->bbp_prom[i].val - 6;
2509 else
2510 sc->sc_bbp17_dynmin = 0;
2511 find_bbp17 = 1;
2515 sc->sc_bbp17_dynmax = RT2560_RXSNS_DYNMAX;
2516 if (!find_bbp17)
2517 sc->sc_bbp17_dynmin = sc->sc_bbp17_dynmax - 6;
2519 /* read Tx power for all b/g channels */
2520 for (i = 0; i < 14 / 2; i++) {
2521 val = rt2560_eeprom_read(sc, RT2560_EEPROM_TXPOWER + i);
2522 sc->txpow[i * 2] = val & 0xff;
2523 sc->txpow[i * 2 + 1] = val >> 8;
2525 for (i = 0; i < 14; ++i) {
2526 if (sc->txpow[i] > 31)
2527 sc->txpow[i] = RT2560_DEFAULT_TXPOWER;
2528 DPRINTF(("tx power chan %d: %u\n", i + 1, sc->txpow[i]));
2531 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CALIBRATE);
2532 if ((val & 0xff) == 0xff)
2533 sc->rssi_corr = RT2560_DEFAULT_RSSI_CORR;
2534 else
2535 sc->rssi_corr = val & 0xff;
2536 DPRINTF(("rssi correction %d, calibrate 0x%02x\n",
2537 sc->rssi_corr, val));
2539 val = rt2560_eeprom_read(sc, RT2560_EEPROM_CONFIG1);
2540 if (val == 0xffff)
2541 val = 0;
2542 if ((val & 0x2) == 0 && sc->asic_rev >= RT2560_ASICREV_D) {
2543 DPRINTF(("capable of RX sensitivity calibration\n"));
2544 sc->sc_flags |= RT2560_FLAG_RXSNS;
2548 static int
2549 rt2560_bbp_init(struct rt2560_softc *sc)
2551 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2552 int i, ntries;
2554 /* wait for BBP to be ready */
2555 for (ntries = 0; ntries < 100; ntries++) {
2556 if (rt2560_bbp_read(sc, RT2560_BBP_VERSION) != 0)
2557 break;
2558 DELAY(1);
2560 if (ntries == 100) {
2561 device_printf(sc->sc_dev, "timeout waiting for BBP\n");
2562 return EIO;
2565 /* initialize BBP registers to default values */
2566 for (i = 0; i < N(rt2560_def_bbp); i++) {
2567 rt2560_bbp_write(sc, rt2560_def_bbp[i].reg,
2568 rt2560_def_bbp[i].val);
2571 /* initialize BBP registers to values stored in EEPROM */
2572 for (i = 0; i < 16; i++) {
2573 if (sc->bbp_prom[i].reg == 0 && sc->bbp_prom[i].val == 0)
2574 break;
2575 rt2560_bbp_write(sc, sc->bbp_prom[i].reg, sc->bbp_prom[i].val);
2577 /* Set rx sensitivity to user specified value */
2578 rt2560_bbp_write(sc, 17, sc->sc_rxsns);
2580 return 0;
2581 #undef N
2584 static void
2585 rt2560_set_txantenna(struct rt2560_softc *sc, int antenna)
2587 uint32_t tmp;
2588 uint8_t tx;
2590 tx = rt2560_bbp_read(sc, RT2560_BBP_TX) & ~RT2560_BBP_ANTMASK;
2591 if (antenna == 1)
2592 tx |= RT2560_BBP_ANTA;
2593 else if (antenna == 2)
2594 tx |= RT2560_BBP_ANTB;
2595 else
2596 tx |= RT2560_BBP_DIVERSITY;
2598 /* need to force I/Q flip for RF 2525e and 5222 */
2599 if (sc->rf_rev == RT2560_RF_2525E || sc->rf_rev == RT2560_RF_5222)
2600 tx |= RT2560_BBP_FLIPIQ;
2602 rt2560_bbp_write(sc, RT2560_BBP_TX, tx);
2604 /* update values for CCK and OFDM in BBPCSR1 */
2605 tmp = RAL_READ(sc, RT2560_BBPCSR1) & ~0x00070007;
2606 tmp |= (tx & 0x7) << 16 | (tx & 0x7);
2607 RAL_WRITE(sc, RT2560_BBPCSR1, tmp);
2610 static void
2611 rt2560_set_rxantenna(struct rt2560_softc *sc, int antenna)
2613 uint8_t rx;
2615 rx = rt2560_bbp_read(sc, RT2560_BBP_RX) & ~RT2560_BBP_ANTMASK;
2616 if (antenna == 1)
2617 rx |= RT2560_BBP_ANTA;
2618 else if (antenna == 2)
2619 rx |= RT2560_BBP_ANTB;
2620 else
2621 rx |= RT2560_BBP_DIVERSITY;
2623 /* need to force no I/Q flip for RF 2525e */
2624 if (sc->rf_rev == RT2560_RF_2525E)
2625 rx &= ~RT2560_BBP_FLIPIQ;
2627 rt2560_bbp_write(sc, RT2560_BBP_RX, rx);
2630 static void
2631 rt2560_init(void *priv)
2633 #define N(a) (sizeof (a) / sizeof ((a)[0]))
2634 struct rt2560_softc *sc = priv;
2635 struct ieee80211com *ic = &sc->sc_ic;
2636 struct ifnet *ifp = ic->ic_ifp;
2637 uint32_t tmp;
2638 int i;
2640 rt2560_stop(sc);
2642 /* setup tx rings */
2643 tmp = RT2560_PRIO_RING_COUNT << 24 |
2644 RT2560_ATIM_RING_COUNT << 16 |
2645 RT2560_TX_RING_COUNT << 8 |
2646 RT2560_TX_DESC_SIZE;
2648 /* rings must be initialized in this exact order */
2649 RAL_WRITE(sc, RT2560_TXCSR2, tmp);
2650 RAL_WRITE(sc, RT2560_TXCSR3, sc->txq.physaddr);
2651 RAL_WRITE(sc, RT2560_TXCSR5, sc->prioq.physaddr);
2652 RAL_WRITE(sc, RT2560_TXCSR4, sc->atimq.physaddr);
2653 RAL_WRITE(sc, RT2560_TXCSR6, sc->bcnq.physaddr);
2655 /* setup rx ring */
2656 tmp = RT2560_RX_RING_COUNT << 8 | RT2560_RX_DESC_SIZE;
2658 RAL_WRITE(sc, RT2560_RXCSR1, tmp);
2659 RAL_WRITE(sc, RT2560_RXCSR2, sc->rxq.physaddr);
2661 /* initialize MAC registers to default values */
2662 for (i = 0; i < N(rt2560_def_mac); i++)
2663 RAL_WRITE(sc, rt2560_def_mac[i].reg, rt2560_def_mac[i].val);
2665 IEEE80211_ADDR_COPY(ic->ic_myaddr, IF_LLADDR(ifp));
2666 rt2560_set_macaddr(sc, ic->ic_myaddr);
2668 /* set basic rate set (will be updated later) */
2669 RAL_WRITE(sc, RT2560_ARSP_PLCP_1, 0x153);
2671 rt2560_update_slot(ifp);
2672 rt2560_update_plcp(sc);
2673 rt2560_update_led(sc, 0, 0);
2675 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2676 RAL_WRITE(sc, RT2560_CSR1, RT2560_HOST_READY);
2678 if (rt2560_bbp_init(sc) != 0) {
2679 rt2560_stop(sc);
2680 return;
2683 rt2560_set_txantenna(sc, sc->tx_ant);
2684 rt2560_set_rxantenna(sc, sc->rx_ant);
2686 /* set default BSS channel */
2687 rt2560_set_chan(sc, ic->ic_curchan);
2689 /* kick Rx */
2690 tmp = RT2560_DROP_PHY_ERROR | RT2560_DROP_CRC_ERROR;
2691 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2692 tmp |= RT2560_DROP_CTL | RT2560_DROP_VERSION_ERROR;
2693 if (ic->ic_opmode != IEEE80211_M_HOSTAP)
2694 tmp |= RT2560_DROP_TODS;
2695 if (!(ifp->if_flags & IFF_PROMISC))
2696 tmp |= RT2560_DROP_NOT_TO_ME;
2698 RAL_WRITE(sc, RT2560_RXCSR0, tmp);
2700 /* clear old FCS and Rx FIFO errors */
2701 RAL_READ(sc, RT2560_CNT0);
2702 RAL_READ(sc, RT2560_CNT4);
2704 /* clear any pending interrupts */
2705 RAL_WRITE(sc, RT2560_CSR7, 0xffffffff);
2707 /* enable interrupts */
2708 RAL_WRITE(sc, RT2560_CSR8, RT2560_INTR_MASK);
2710 ifp->if_flags &= ~IFF_OACTIVE;
2711 ifp->if_flags |= IFF_RUNNING;
2713 /* XXX */
2714 if (ic->ic_flags & IEEE80211_F_PRIVACY) {
2715 int i;
2717 ic->ic_flags &= ~IEEE80211_F_DROPUNENC;
2718 for (i = 0; i < IEEE80211_WEP_NKID; ++i) {
2719 struct ieee80211_key *wk = &ic->ic_nw_keys[i];
2721 if (wk->wk_keylen == 0)
2722 continue;
2723 if (wk->wk_flags & IEEE80211_KEY_XMIT)
2724 wk->wk_flags |= IEEE80211_KEY_SWCRYPT;
2728 sc->sc_avgrssi = -1;
2730 if (ic->ic_opmode != IEEE80211_M_MONITOR) {
2731 if (ic->ic_roaming != IEEE80211_ROAMING_MANUAL)
2732 ieee80211_new_state(ic, IEEE80211_S_SCAN, -1);
2733 } else {
2734 ieee80211_new_state(ic, IEEE80211_S_RUN, -1);
2736 #undef N
2739 void
2740 rt2560_stop(void *priv)
2742 struct rt2560_softc *sc = priv;
2743 struct ieee80211com *ic = &sc->sc_ic;
2744 struct ifnet *ifp = ic->ic_ifp;
2746 ieee80211_new_state(ic, IEEE80211_S_INIT, -1);
2748 sc->sc_tx_timer = 0;
2749 sc->sc_flags &= ~(RT2560_FLAG_DATA_OACT | RT2560_FLAG_PRIO_OACT);
2750 ifp->if_timer = 0;
2751 ifp->if_flags &= ~(IFF_RUNNING | IFF_OACTIVE);
2753 /* abort Tx */
2754 RAL_WRITE(sc, RT2560_TXCSR0, RT2560_ABORT_TX);
2756 /* disable Rx */
2757 RAL_WRITE(sc, RT2560_RXCSR0, RT2560_DISABLE_RX);
2759 /* reset ASIC (imply reset BBP) */
2760 RAL_WRITE(sc, RT2560_CSR1, RT2560_RESET_ASIC);
2761 RAL_WRITE(sc, RT2560_CSR1, 0);
2763 /* disable interrupts */
2764 RAL_WRITE(sc, RT2560_CSR8, 0xffffffff);
2766 /* reset Tx and Rx rings */
2767 rt2560_reset_tx_ring(sc, &sc->txq);
2768 rt2560_reset_tx_ring(sc, &sc->atimq);
2769 rt2560_reset_tx_ring(sc, &sc->prioq);
2770 rt2560_reset_tx_ring(sc, &sc->bcnq);
2771 rt2560_reset_rx_ring(sc, &sc->rxq);
2774 static void
2775 rt2560_dma_map_mbuf(void *arg, bus_dma_segment_t *seg, int nseg,
2776 bus_size_t map_size __unused, int error)
2778 if (error)
2779 return;
2781 KASSERT(nseg == 1, ("too many dma segments\n"));
2782 *((bus_addr_t *)arg) = seg->ds_addr;
2785 static void *
2786 rt2560_ratectl_attach(struct ieee80211com *ic, u_int rc)
2788 struct rt2560_softc *sc = ic->ic_if.if_softc;
2790 switch (rc) {
2791 case IEEE80211_RATECTL_SAMPLE:
2792 return &sc->sc_sample_param;
2793 case IEEE80211_RATECTL_ONOE:
2794 return &sc->sc_onoe_param;
2795 case IEEE80211_RATECTL_NONE:
2796 /* This could only happen during detaching */
2797 return NULL;
2798 default:
2799 panic("unknown rate control algo %u\n", rc);
2800 return NULL;
2804 static void
2805 rt2560_calib_rxsensitivity(struct rt2560_softc *sc, uint32_t false_cca)
2807 #define MID_RX_SENSITIVITY (RT2560_RXSNS_DYNMAX + 1)
2809 int rssi_dbm;
2811 if (sc->sc_ic.ic_state != IEEE80211_S_RUN)
2812 return;
2814 rssi_dbm = sc->sc_avgrssi + RT2560_NOISE_FLOOR;
2815 DPRINTF(("rssi dbm %d\n", rssi_dbm));
2818 * Rx sensitivity is reduced, if bbp17 is increased, and vice versa.
2819 * Lower rx sensitivity could do a better job of reducing false CCA,
2820 * but on the other hand roaming range is decreased.
2823 if (rssi_dbm < -80) {
2824 /* Signal is too weak */
2825 return;
2826 } else if (rssi_dbm >= -74) {
2827 uint8_t bbp17;
2829 if (rssi_dbm >= -58)
2830 bbp17 = RT2560_RXSNS_MAX;
2831 else
2832 bbp17 = MID_RX_SENSITIVITY;
2833 if (sc->sc_bbp17 != bbp17)
2834 rt2560_bbp_write(sc, 17, bbp17);
2835 return;
2838 /* RSSI is [-80,74)dBm, if we reach here */
2840 if (sc->sc_bbp17 > MID_RX_SENSITIVITY) {
2841 rt2560_bbp_write(sc, 17, MID_RX_SENSITIVITY);
2842 return;
2846 * Dynamic rx sensitivity tuning to keep balance between number
2847 * of false CCA per second and roaming range:
2848 * Reduce rx sensitivity if false CCA is too high.
2849 * If false CCA is relatively low, rx sensitivity is increased to
2850 * extend roaming range.
2852 if (false_cca > 512 && sc->sc_bbp17 < sc->sc_bbp17_dynmax)
2853 rt2560_bbp_write(sc, 17, sc->sc_bbp17 + 1);
2854 else if (false_cca < 100 && sc->sc_bbp17 > sc->sc_bbp17_dynmin)
2855 rt2560_bbp_write(sc, 17, sc->sc_bbp17 - 1);
2857 #undef MID_RX_SENSITIVITY
2860 static void
2861 rt2560_calibrate(void *xsc)
2863 struct rt2560_softc *sc = xsc;
2864 struct ifnet *ifp = &sc->sc_ic.ic_if;
2865 uint32_t false_cca;
2867 lwkt_serialize_enter(ifp->if_serializer);
2869 false_cca = RAL_READ(sc, RT2560_CNT3) & 0xffff;
2870 DPRINTF(("false CCA %u\n", false_cca));
2872 if (sc->sc_calib_rxsns)
2873 rt2560_calib_rxsensitivity(sc, false_cca);
2875 callout_reset(&sc->calib_ch, hz, rt2560_calibrate, sc);
2877 lwkt_serialize_exit(ifp->if_serializer);
2880 static int
2881 rt2560_sysctl_rxsns(SYSCTL_HANDLER_ARGS)
2883 struct rt2560_softc *sc = arg1;
2884 struct ifnet *ifp = &sc->sc_ic.ic_if;
2885 int error = 0, v;
2887 lwkt_serialize_enter(ifp->if_serializer);
2889 v = sc->sc_rxsns;
2890 error = sysctl_handle_int(oidp, &v, 0, req);
2891 if (error || req->newptr == NULL)
2892 goto back;
2893 if (v < sc->sc_bbp17_dynmin || v > RT2560_RXSNS_MAX) {
2894 error = EINVAL;
2895 goto back;
2898 if (sc->sc_rxsns != v) {
2900 * Adjust bbp17 iff ral(4) is up and running (i.e. hardware
2901 * is initialized)and rx sensitivity calibration is _not_
2902 * enabled.
2904 if ((ifp->if_flags & IFF_RUNNING) && !sc->sc_calib_rxsns)
2905 rt2560_bbp_write(sc, 17, v);
2906 sc->sc_rxsns = v;
2908 back:
2909 lwkt_serialize_exit(ifp->if_serializer);
2910 return error;